File: board.cc

package info (click to toggle)
galois 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 948 kB
  • sloc: cpp: 4,075; xml: 2,325; makefile: 146
file content (412 lines) | stat: -rw-r--r-- 8,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* board.cc -- game board for falling blocks game.  -*- C++ -*-
   Copyright (C) 2011-2015 Gerardo Ballabio

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <cstdlib>
#include <vector>
#include <gtkmm.h>
#include "board.h"
#include "grid.h"
#include "group.h"

bool game_board::fits(int b, int s, coords c) const
{
  std::vector<coords> bricks = get_bricks(blocks[b].shape[s]);
  for (std::vector<coords>::const_iterator i=bricks.begin();
       i!=bricks.end(); ++i)
    {
      coords n = c + *i;
      if (!cells.is_in(n) || cells.get(n))
	return false;
    }

  return true;
}

bool game_board::fits_b(int b, int s, coords c) const
{
  std::vector<coords> bricks = get_bricks(blocks[b].blocker[s]);
  for (std::vector<coords>::const_iterator i=bricks.begin();
       i!=bricks.end(); ++i)
    {
      coords n = c + *i;
      if (!cells.is_in(n) || cells.get(n))
	return false;
    }

  return true;
}

coords game_board::move_in(int b, int s, coords c) const
{
  // move down if necessary
  while (blocks[b].shape[s].get_y0() + c.y < cells.get_y0())
    c = g->down(c);

  // move right if necessary
  while (blocks[b].shape[s].get_x0() + c.x < cells.get_x0())
    c = g->right(c);

  // move left if necessary
  while (blocks[b].shape[s].get_x0() + blocks[b].shape[s].get_cols() + c.x >
	 cells.get_x0() + cells.get_cols())
    c = g->left(c);

  // move back if necessary
  while (blocks[b].shape[s].get_z0() + c.z < cells.get_z0())
    c = g->back(c);

  // move front if necessary
  while (blocks[b].shape[s].get_z0() + blocks[b].shape[s].get_layers() + c.z >
	 cells.get_z0() + cells.get_layers())
    c = g->front(c);

  return c;
}

void game_board::first_block()
{
  // initialize random numbers generator
  Glib::TimeVal timeval;
  timeval.assign_current_time();
  std::srand(static_cast<unsigned>(timeval.as_double() * G_USEC_PER_SEC));

  nsize = bmax;
  // choose block
  nblock = rand() % series[nsize];
  for (int i=0; i<nsize; ++i)
    nblock += series[i];
  // choose orientation
  nshape = rand() % blocks[nblock].shape.size();

  // move to active block
  next_block();
}

void game_board::land_block()
{
  // dump block onto cells grid
  std::vector<coords> bricks = get_bricks(blocks[iblock].shape[ishape]);
  for (std::vector<coords>::const_iterator i=bricks.begin();
       i!=bricks.end(); ++i)
    {
      coords n = pos + *i;
      cells.set(n, iblock);
    }
  score += score_block();

  // search for filled lines
  filled = g->check_lines(cells, empty);
}

void game_board::next_level()
{
  // increment level
  ++level;
  switch (mode1)
    {
    case 0:
      // increase falling block speed
      speed = level;
      break;
    case 1:
      // increase maximum block size
      ++bmax;
      next_blockset();
      switch (mode2)
	{
	case 0: if (bmin > 1) --bmin; break;
	case 1: break;
	case 2: ++bmin; break;
	}
      break;
    case 2:
      // increase superblock size
      ++bsuper;
      next_blockset();
      break;
    }
}

void game_board::next_blockset()
{
  // extract last blockset
  int first = blocks.size() - ((series.size() > 0) ? series.back() : 0);
  std::vector<block> v(blocks.begin() + first, blocks.end());

  // reduce it to a maximum size in order to speed up generation
  int max = g->blockset_max();
  if (static_cast<int>(v.size()) > max)
    v = random_blocks(v, max);

  // generate and append new blockset
  v = g->augment_blocks(v, cells.get_x0(), cells.get_z0(),
			cells.get_cols(), cells.get_layers());
  blocks.insert(blocks.end(), v.begin(), v.end());
  series.push_back(v.size());
}

int game_board::score_block() const
{
  return (speed + bmax * 5 + bsuper) * (bmax + bmin - 5) / (1 + empty);
}

int game_board::score_lines(int lines) const
{
  return score_block() * 20 * (1 << lines);
}

grid<bool> game_board::get_block() const
{
  grid<bool> g = blocks[iblock].shape[ishape];
  g.move(pos);
  return g;
}

grid<bool> game_board::get_shadow()
{
  // move block down until it lands
  coords c = pos;
  while (fits(iblock, ishape, g->down(c)))
    c = g->down(c);

  grid<bool> g = blocks[iblock].shape[ishape];
  g.move(c);
  return g;
}

void game_board::start_game(const group *gg, int width, int depth,
			    int m1, int m2, int s, int min, int max, int e)
{
  if (game)
    return;

  // delete previous group, if any
  if (g)
    delete g;

  // set spatial group
  g = gg;
  // generate game board
  cells = g->make_board(width, depth);
  // generate block set
  std::vector<block> v;
  blocks.resize(0);
  series.resize(0);
  for (int i=0; i<=max; ++i)
    next_blockset();
  if (min > max)
    min = max;
  bmin = min;
  bmax = nsize = nsuper = max;
  isuper = bsuper = 0;
  empty = e;
  // start game
  mode1 = m1;
  if (mode1 == 2)
    {
      bsuper = max + 1;
      next_blockset();
    }
  mode2 = m2;
  speed = s;
  level = (mode1 == 0) ? speed : 1;
  lines = score = 0;
  filled.resize(0);
  game = true;

  first_block();
}

bool game_board::rotate(int n)
{
  int rotations = blocks[iblock].rotate.size() / blocks[iblock].shape.size();
  if (n >= rotations)
    return false; // do nothing

  int nshape = blocks[iblock].rotate[ishape * rotations + n];
  coords m = move_in(iblock, nshape, pos);
  if (!fits(iblock, nshape, m))
    return false; // reject move

  // accept move
  pos = m;
  ishape = nshape;
  return true;
}

bool game_board::reflect()
{
  int nshape = blocks[iblock].reflect[ishape];
  coords m = move_in(iblock, nshape, pos);
  if (!fits(iblock, nshape, m))
    return false; // reject move

  // accept move
  pos = m;
  ishape = nshape;
  return true;
}

bool game_board::left()
{
  coords c = g->left(pos);
  if (!fits(iblock, ishape, c))
    return false;

  pos = c;
  return true;
}

bool game_board::right()
{
  coords c = g->right(pos);
  if (!fits(iblock, ishape, c))
    return false;

  pos = c;
  return true;
}

bool game_board::front()
{
  coords c = g->front(pos);
  if (!fits(iblock, ishape, c))
    return false;

  pos = c;
  return true;
}

bool game_board::back()
{
  coords c = g->back(pos);
  if (!fits(iblock, ishape, c))
    return false;

  pos = c;
  return true;
}

bool game_board::down()
{
  coords c = g->down(pos);
  if (fits(iblock, ishape, c) && fits_b(iblock, ishape, pos))
    {
      // move block down and continue
      pos = c;
      return true;
    }
  else
    {
      land_block();
      return false;
    }
}

bool game_board::drop()
{
  // move block down until it lands
  while (down())
    ;

  return true;
}

void game_board::color_lines(int c)
{
  // remove them
  for (std::vector<int>::const_iterator i=filled.begin();
       i!=filled.end(); ++i)
    g->color_line(cells, *i, c);
}

void game_board::remove_lines()
{
  // remove filled lines
  for (std::vector<int>::const_iterator i=filled.begin();
       i!=filled.end(); ++i)
    g->remove_line(cells, *i);

  // update lines count
  lines += filled.size();
  score += score_lines(filled.size());
  const int level_lines = 10;
  if (lines / level_lines > level)
    next_level();

  // choose new block
  next_block();
}

void game_board::next_block()
{
  // new active block
  iblock = nblock;
  ishape = nshape;
  bsize = nsize;
  // set initial position
  pos = make_coords();
  if (iblock != 0)
    pos = move_in(iblock, ishape, pos);
  // if it doesn't fit, game is over
  if (!fits(iblock, ishape, pos))
    game = false;

  // next block
  int max_super = bsuper;
  if (bsuper > 18)
    {
      max_super = (55 - bsuper) / 2;
      if (max_super < 2)
	max_super = 2;
    }
  if (mode1 == 2 && ++isuper >= max_super)
    {
      // superblock
      isuper = 0;
      nsuper = nsize;
      nsize = bsuper;
    }
  else
    {
      if (mode1 == 2 && isuper == 1)
	nsize = nsuper; // resume cycle where it was left

      // cycle over block sizes (not in order)
      int plus = (bmax - bmin + 1) / 2, minus = plus * 2 + 1;
      nsize += plus;
      while (nsize > bmax)
	nsize -= minus;
      if (nsize < bmin)
	nsize += (plus > 0) ? plus : 1;
    }
  // choose block
  nblock = rand() % series[nsize];
  for (int i=0; i<nsize; ++i)
    nblock += series[i];
  // choose orientation
  nshape = rand() % blocks[nblock].shape.size();
}

void game_board::stop()
{
  if (!game)
    return;

  game = false;
  cells = grid<int>(cells.get_rows(), cells.get_cols(), cells.get_layers(),
		    cells.get_c0());
}