-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
200 lines (160 loc) · 4.35 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
// Board.java
/**
CS108 Tetris Board.
Represents a Tetris board -- essentially a 2-d grid
of booleans. Supports tetris pieces and row clearing.
Has an "undo" feature that allows clients to add and remove pieces efficiently.
Does not do any drawing or have any idea of pixels. Instead,
just represents the abstract 2-d board.
*/
public class Board {
// Some ivars are stubbed out for you:
private int width;
private int height;
private boolean[][] grid;
private boolean DEBUG = true;
boolean committed;
// Here a few trivial methods are provided:
/**
Creates an empty board of the given width and height
measured in blocks.
*/
public Board(int width, int height) {
this.width = width;
this.height = height;
grid = new boolean[width][height];
committed = true;
// YOUR CODE HERE
}
/**
Returns the width of the board in blocks.
*/
public int getWidth() {
return width;
}
/**
Returns the height of the board in blocks.
*/
public int getHeight() {
return height;
}
/**
Returns the max column height present in the board.
For an empty board this is 0.
*/
public int getMaxHeight() {
return 0; // YOUR CODE HERE
}
/**
Checks the board for internal consistency -- used
for debugging.
*/
public void sanityCheck() {
if (DEBUG) {
// YOUR CODE HERE
}
}
/**
Given a piece and an x, returns the y
value where the piece would come to rest
if it were dropped straight down at that x.
<p>
Implementation: use the skirt and the col heights
to compute this fast -- O(skirt length).
*/
public int dropHeight(Piece piece, int x) {
return 0; // YOUR CODE HERE
}
/**
Returns the height of the given column --
i.e. the y value of the highest block + 1.
The height is 0 if the column contains no blocks.
*/
public int getColumnHeight(int x) {
return 0; // YOUR CODE HERE
}
/**
Returns the number of filled blocks in
the given row.
*/
public int getRowWidth(int y) {
return 0; // YOUR CODE HERE
}
/**
Returns true if the given block is filled in the board.
Blocks outside of the valid width/height area
always return true.
*/
public boolean getGrid(int x, int y) {
return false; // YOUR CODE HERE
}
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.
Returns PLACE_OK for a regular placement, or PLACE_ROW_FILLED
for a regular placement that causes at least one row to be filled.
<p>Error cases:
A placement may fail in two ways. First, if part of the piece may falls out
of bounds of the board, PLACE_OUT_BOUNDS is returned.
Or the placement may collide with existing blocks in the grid
in which case PLACE_BAD is returned.
In both error cases, the board may be left in an invalid
state. The client can use undo(), to recover the valid, pre-place state.
*/
public int place(Piece piece, int x, int y) {
// flag !committed problem
if (!committed) throw new RuntimeException("place commit problem");
int result = PLACE_OK;
// YOUR CODE HERE
return result;
}
/**
Deletes rows that are filled all the way across, moving
things above down. Returns the number of rows cleared.
*/
public int clearRows() {
int rowsCleared = 0;
// YOUR CODE HERE
sanityCheck();
return rowsCleared;
}
/**
Reverts the board to its state before up to one place
and one clearRows();
If the conditions for undo() are not met, such as
calling undo() twice in a row, then the second undo() does nothing.
See the overview docs.
*/
public void undo() {
// YOUR CODE HERE
}
/**
Puts the board in the committed state.
*/
public void commit() {
committed = true;
}
/*
Renders the board state as a big String, suitable for printing.
This is the sort of print-obj-state utility that can help see complex
state change over time.
(provided debugging utility)
*/
public String toString() {
StringBuilder buff = new StringBuilder();
for (int y = height-1; y>=0; y--) {
buff.append('|');
for (int x=0; x<width; x++) {
if (getGrid(x,y)) buff.append('+');
else buff.append(' ');
}
buff.append("|\n");
}
for (int x=0; x<width+2; x++) buff.append('-');
return(buff.toString());
}
}