-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisGrid.java
62 lines (55 loc) · 1.12 KB
/
TetrisGrid.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
//
// TetrisGrid encapsulates a tetris board and has
// a clearRows() capability.
public class TetrisGrid {
private boolean[][] grid;
/**
* Constructs a new instance with the given grid.
* Does not make a copy.
* @param grid
*/
public TetrisGrid(boolean[][] grid) {
this.grid = grid;
}
/**
* Does row-clearing on the grid (see handout).
*/
public void clearRows() {
int width = grid[0].length;
int colInReduced = 0;
for (int i=0; i< width; i++) {
if (!isFilled(i)) {
copyCol(colInReduced, i);
colInReduced++;
}
}
fillWithFalses(colInReduced);
}
private void fillWithFalses(int to) {
for (; to<grid[0].length; to++) {
for (int i=0; i<grid.length; i++) {
grid[i][to] = false;
}
}
}
private void copyCol(int to, int from) {
for (int i=0; i<grid.length; i++) {
grid[i][to] = grid[i][from];
}
}
private boolean isFilled(int j) {
int height = grid.length;
for (int i = 0; i < height; i++) {
if(!grid[i][j])
return false;
}
return true;
}
/**
* Returns the internal 2d grid array.
* @return 2d grid array
*/
boolean[][] getGrid() {
return grid;
}
}