-
Notifications
You must be signed in to change notification settings - Fork 2
/
Board.java
466 lines (405 loc) · 13.7 KB
/
Board.java
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import java.awt.Point;
/**
* Represents a Tetris board -- essentially a 2D grid of booleans. Supports
* tetris pieces and row clearning.
* Has an "undo" feature that allows clients to add and remove pieces efficiently.
* Does not do any drawing or have any idea of pixels. Intead, just represents
* the abstract 2D board.
* See the lab documentation for an overview.
*
* @author Nick Parlante
* @version 1.0, Mar 1, 2001
*/
/*
* NB: The board is naturally organized into rows and columns. However, pieces
* are organized on an x-y coordinate system. The conversion between rows
* and columns and y and x values can be confusing. Be careful.
*/
public class Board
{
private int width;
private int height;
private boolean[][] grid;
private int[] rowWidths;
private int[] colHeights;
private int maxHeight;
// backup data structures to support undo
private boolean[][] gridBackup;
private int[] rowWidthsBackup;
private int[] colHeightsBackup;
private int maxHeightBackup;
private boolean committed;
// set DEBUG to true while developing the lab to enable sanity checks
private boolean DEBUG = true;
/**
* Creates an empty board of the given width and height measured in blocks.
*
* @param initialWidth width of this board in units of blocks
* @param initialHeight height of this board in units of blocks
*/
public Board(int initialWidth, int initialHeight)
{
this.width = initialWidth;
this.height = initialHeight;
this.grid = new boolean[this.height][this.width];
this.rowWidths = new int[this.height];
this.colHeights = new int[this.width];
this.maxHeight = 0;
this.gridBackup = new boolean[this.height][this.width];
this.rowWidthsBackup = new int[this.height];
this.colHeightsBackup = new int[this.width];
this.maxHeightBackup = 0;
this.committed = true;
}
/**
* Returns the width of the board in blocks.
*
* @return the width of the board in blocks
*/
public int getWidth()
{
return this.width;
}
/**
* Returns the height of the board in blocks.
*
* @return the height of the board in blocks
*/
public int getHeight()
{
return this.height;
}
/**
* Returns the max column height present in the board.
* For an empty board this is 0.
*
* @return the max column height present in the board
*/
public int getMaxHeight()
{
return this.maxHeight;
}
/**
* Given a piece and a column, returns the row value where the bottom of the
* piece would come to rest if its left edge were dropped straight down
* at that column.
*
* Implementation hint: use the skirt and the column heights to compute this
* fast -- O(skirt length).
*
* @param piece the piece that will be dropped
* @param col the column in which the left-edge of the piece will be
* dropped
* @return the row value where the bottom of the piece would come to rest
*/
public int dropHeight(Piece piece, int col)
{
int maxRow = 0;
for(int i = 0; i < piece.getSkirt().length; i++)
{
int row = this.getColumnHeight(col + i) - piece.getSkirt()[i];
if(row > maxRow)
{
maxRow = row;
}
}
return maxRow;
}
/**
* Returns the height of the specified column -- i.e. the row value of the
* highest block + 1.
* The height is 0 if the column contains no blocks.
*
* @param col the column from which to compute the height
* @return the height of the specified column
*/
public int getColumnHeight(int col)
{
return this.colHeights[col];
}
/**
* Returns the number of filled blocks in the specified row.
*
* @param row the row from which to compute the width
* @return the number of filled blocks in the specified row
*/
public int getRowWidth(int row)
{
return this.rowWidths[row];
}
/**
* Returns true if the given block is filled in the board.
* Blocks outside of the valid width/height area always return true.
*
* @param col the specified column
* @param row the specified row
* @return true if the given block is filled in the board
*/
public boolean getGrid(int col, int row)
{
if(col < 0 || col > this.getWidth() || row < 0 || row > this.getHeight())
{
return true;
}
return this.grid[row][col];
}
public static final int PLACE_OK = 0;
public static final int PLACE_ROW_FILLED = 1;
public static final int PLACE_OUT_BOUNDS = 2;
public static final int PLACE_BAD = 3;
/**
* Attempts to add the body of a piece to the board.
* Copies the piece blocks into the board grid.
*
* Error cases:
* If part of the piece would fall out of bounds, the placement does
* not change the board at all, and PLACE_OUT_BOUNDS is returned.
* If the placement is "bad" --interfering with existing blocks in the
* grid -- then the placement is halted partially complete and
* PLACE_BAD is returned. An undo() will remove the bad placement.
*
* @param piece the piece to place its lower-left corner at the
* specified column and row
* @return PLACE_OK for a regular placement or PLACE_ROW_FILLED for a
* regular placement that causes at least one row to be filled.
* PLACE_OUT_BOUNDS or PLACE_BAD for error cases.
*/
public int place(Piece piece, int placeCol, int placeRow)
{
// assume that the piece is placed successfully
int status = PLACE_OK;
// place cannot be invoked on a board that has not been committed
assert(this.committed);
// backup the current state of the board
this.committed = false;
for(int row = 0; row < this.getHeight(); row++)
{
System.arraycopy(this.grid[row], 0, this.gridBackup[row],
0, this.grid[row].length);
}
System.arraycopy(this.rowWidths, 0, this.rowWidthsBackup,
0, this.rowWidths.length);
System.arraycopy(this.colHeights, 0, this.colHeightsBackup,
0, this.colHeights.length);
this.maxHeightBackup = this.maxHeight;
// check for out of bounds
if(placeCol + piece.getWidth() > getWidth() ||
placeRow + piece.getHeight() > getHeight() ||
placeCol < 0 || placeRow < 0)
{
return PLACE_OUT_BOUNDS;
}
/*
* for each point in the piece's body:
* check if it collides with another piece,
* update the row widths,
* update the column height (potentially),
* update the max height (potentially)
*/
for(Point pt : piece.getBody())
{
int pointCol = placeCol + pt.x;
int pointRow = placeRow + pt.y;
if(grid[pointRow][pointCol])
{
status = PLACE_BAD;
}
else
{
grid[pointRow][pointCol] = true;
this.rowWidths[pointRow]++;
if(pointRow + 1 > this.colHeights[pointCol])
{
this.colHeights[pointCol] = pointRow + 1;
}
if(pointRow + 1 > this.getMaxHeight())
{
this.maxHeight = pointRow + 1;
}
}
}
// check for completed rows
for(int i = 0; i < piece.getHeight() && status == PLACE_OK; i++)
{
if(this.getRowWidth(placeRow + i) == this.getWidth())
{
status = PLACE_ROW_FILLED;
}
}
sanityCheck();
return status;
}
/**
* Deletes rows that are filled all the way across, moving blocks above down.
* Returns true if any row clearing happened.
*
* Implementation hints:
* This is complicated.
* Ideally, you want to copy each row down to its correct location in one pass.
* Note that more than one row may be filled.
* Remember to update the column heights and max height if needed.
*
* @return true if any row clearing happened
*/
public boolean clearRows()
{
int toRow = -1;
int fromRow = 0;
int maxHeightAdj = 0;
// find the first filled row
for(int row = 0; row < this.getHeight(); row++)
{
if(this.getRowWidth(row) == this.getWidth())
{
toRow = row;
fromRow = toRow + 1;
maxHeightAdj++;
// find the next unfilled row
while(fromRow < this.getMaxHeight() &&
this.getRowWidth(fromRow) == this.getWidth())
{
fromRow++;
maxHeightAdj++;
}
break;
}
}
if(toRow == -1)
{
sanityCheck();
return false;
}
// if there is a row to clear...
while(fromRow < this.getMaxHeight())
{
this.grid[toRow] = this.grid[fromRow];
this.rowWidths[toRow] = this.rowWidths[fromRow];
toRow++;
fromRow++;
// find the next unfilled row
while(fromRow < this.getMaxHeight() &&
this.getRowWidth(fromRow) == this.getWidth())
{
fromRow++;
maxHeightAdj++;
}
}
// copy blank rows
for( ; toRow < this.getHeight(); toRow++)
{
this.grid[toRow] = new boolean[this.getWidth()];
this.rowWidths[toRow] = 0;
}
// update column heights
for( int col = 0; col < this.getWidth(); col++)
{
while(this.colHeights[col] > 0 &&
! this.grid[this.colHeights[col] - 1][col])
{
this.colHeights[col]--;
}
}
// update max height
this.maxHeight -= maxHeightAdj;
sanityCheck();
return true;
}
/**
* If a place() happens, optionally followed by a clearRows(), a subsequent
* undo() reverts the board to its state before the place(). If the
* conditions for undo() are not met, such as calling undo() twice in a
* row, then the second undo() does nothing.
* See the lab document.
*/
public void undo()
{
if(! this.committed)
{
int[] tempHeights = this.colHeights;
this.colHeights = this.colHeightsBackup;
this.colHeightsBackup = tempHeights;
int[] tempWidths = this.rowWidths;
this.rowWidths = this.rowWidthsBackup;
this.rowWidthsBackup = tempWidths;
for(int row = 0; row < this.getHeight(); row++)
{
boolean[] tempRow = this.grid[row];
this.grid[row] = this.gridBackup[row];
this.gridBackup[row] = tempRow;
}
this.maxHeight = this.maxHeightBackup;
this.committed = true;
}
sanityCheck();
}
/**
* Puts the board in the committed state.
* See the overview docs.
*/
public void commit()
{
this.committed = true;
}
/**
* Checks the board for internal consistency -- used for debugging.
*/
public void sanityCheck()
{
if (DEBUG)
{
if(this.grid.length != this.getHeight())
{
throw new RuntimeException("grid height != board height");
}
if(this.grid[0].length != this.getWidth())
{
throw new RuntimeException("grid width != board width");
}
// check row widths
for(int row = 0; row < this.getHeight(); row++)
{
int width = 0;
for(int col = 0; col < this.getWidth(); col++)
{
if(this.grid[row][col])
{
width++;
}
}
if(this.rowWidths[row] != width)
{
throw new RuntimeException("row widths inconsistent");
}
}
// check column heights
for(int col = 0; col < this.getWidth(); col++)
{
int height = 0;
for(int row = 0; row < this.getHeight(); row++)
{
if(this.grid[row][col])
{
height = row + 1;
}
}
if(this.colHeights[col] != height)
{
throw new RuntimeException("column heights inconsistent");
}
}
// check max height
int maxHeight = 0;
for(int col = 0; col < this.getWidth(); col++)
{
if(this.getColumnHeight(col) > maxHeight)
{
maxHeight = this.getColumnHeight(col);
}
}
if(this.getMaxHeight() != maxHeight)
{
throw new RuntimeException("max height inconsistent");
}
}
}
}