-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.java
275 lines (256 loc) · 5.94 KB
/
Block.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
/*
* Name: LI Chaohui
* Student ID: 55202995
* Lab section: T01
*
*/
public abstract class Block{
Cell[] cells = new Cell[4];
char shape;
int status;//0, 1, 2, 3 (shape 'O' block only has status 0)
public Block(Cell c1, Cell c2, Cell c3, Cell c4, char shape, int status) {
cells[0] = c1;
cells[1] = c2;
cells[2] = c3;
cells[3] = c4;
this.shape = shape;
this.status = status;
}
/*
*get and set methods
*/
public Cell[] getCells() {
return cells;
}
public char getShape() {
return shape;
}
public int getStatus() {
return status;
}
public BlockColor getColor() {
return cells[0].getColor();
}
public void setStatus(int status) {
if(shape == 'O')
this.status = 0;
else
this.status = status;
}
/*
* move methods for Block
*/
public void moveleft() {
for(int i = 0; i < 4; i++)
cells[i].left();
}
public void moveright(){
for(int i = 0; i < 4; i++) {
cells[i].right();
}
}
public void movedown(){
for(int i = 0; i < 4; i++)
cells[i].down();
}
//rotate method
public void rotate() throws OutOfBoardException {
switch(shape) {
//"I' shape block
case 'I':
/*
* compute the average row and col numbers
* in order to find the center for rotation
*/
int row=0, col=0;
for(int i=0; i<4; i++) {
row += cells[i].getRow();
col += cells[i].getCol();
}
row = Math.floorDiv(row, 4);
col = Math.floorDiv(col, 4);
if(status == 0) {
for(int i=0; i<4; i++) {
cells[i].setRow(row-1+i);
cells[i].setCol(col+1);
}
}
else if(status == 1) {
for(int i=0; i<4; i++) {
cells[i].setRow(row+1);
cells[i].setCol(col-2+i);
}
}
else if(status == 2) {
for(int i=0; i<4; i++) {
cells[i].setRow(row-2+i);
cells[i].setCol(col);
}
}
else if(status == 3) {
for(int i=0; i<4; i++) {
cells[i].setRow(row);
cells[i].setCol(col-1+i);
}
}
break;
//'O' shape block doesn't rotate
case 'O':
break;
//Other shapes: 3*3 matrix
default:{
int matrix[][] = new int[3][3];
//initialize the matrix
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
matrix[i][j] = 0;
//find the center cell
int r=0, c=0;
for(int i=0; i<4; i++) {
r += cells[i].getRow();
c += cells[i].getCol();
}
if(shape=='S' || shape=='Z') {
if(status == 0) {
r = r/4 + 1;
c /= 4;
}
else if(status == 1 || status == 2) {
r /= 4;
c /= 4;
}
else if(status == 3) {
r /= 4;
c = Math.floorDiv(c, 4) + 1;
}
}
else {
r = (int) (Math.round(r/4.0));
c = (int) (Math.round(c/4.0));
}
//position the center into matrix[1][1]
int d1 = r - 1;
int d2 = c - 1;//offsets for row and col numbers
for(int i=0; i<4; i++) {
int a = cells[i].getRow();
int b = cells[i].getCol();
matrix[a-d1][b-d2] = 1;
}
//rotate the current block clockwise by 90¡ã
int temp[][] = new int[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++) {
temp[i][j] = matrix[2-j][i];
}
//figure out the rotated cells
int index = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++)
if(temp[i][j] == 1) {
cells[index].setRow(i + d1);
cells[index++].setCol(j + d2);
}
}
}
break;
}
status++;
if(status >= 4)
status = 0;
if(shape == 'O')
status = 0;
}
/*
* generate various blocks randomly
* set the initial status as 0
*/
public static Block randomBlock() {
switch((int)(Math.random()*7)) {
case 0:
return new IBlock();
case 1:
return new JBlock();
case 2:
return new LBlock();
case 3:
return new OBlock();
case 4:
return new SBlock();
case 5:
return new TBlock();
case 6:
return new ZBlock();
}
return null;
}
/*
* rewrite toString() in Block
*/
public String toString() {
String str="[";
for(int i=0; i<3; i++) {
str += cells[i].toString() +"; ";
}
str += cells[3].toString()+": "+this.shape+", "+this.status+"]";
return str;
}
}
/*
* Subclasses for all shapes of blocks
*/
class IBlock extends Block {
public IBlock(){
super(new Cell(0,3,BlockColor.I_COLOR),
new Cell(0,4,BlockColor.I_COLOR),
new Cell(0,5,BlockColor.I_COLOR),
new Cell(0,6,BlockColor.I_COLOR), 'I', 0);
}
}
class JBlock extends Block {
public JBlock(){
super(new Cell(0,3,BlockColor.J_COLOR),
new Cell(0,4,BlockColor.J_COLOR),
new Cell(0,5,BlockColor.J_COLOR),
new Cell(-1,3,BlockColor.J_COLOR), 'J', 0);
}
}
class LBlock extends Block {
public LBlock(){
super(new Cell(0,3,BlockColor.L_COLOR),
new Cell(0,4,BlockColor.L_COLOR),
new Cell(0,5,BlockColor.L_COLOR),
new Cell(-1,5,BlockColor.L_COLOR), 'L', 0);
}
}
class OBlock extends Block {
public OBlock(){
super(new Cell(0,4,BlockColor.O_COLOR),
new Cell(-1,4,BlockColor.O_COLOR),
new Cell(-1,5,BlockColor.O_COLOR),
new Cell(0,5,BlockColor.O_COLOR), 'O', 0);
}
}
class SBlock extends Block {
public SBlock(){
super(new Cell(0,3,BlockColor.S_COLOR),
new Cell(0,4,BlockColor.S_COLOR),
new Cell(-1,4,BlockColor.S_COLOR),
new Cell(-1,5,BlockColor.S_COLOR), 'S', 0);
}
}
class TBlock extends Block {
public TBlock(){
super(new Cell(0,3,BlockColor.T_COLOR),
new Cell(0,4,BlockColor.T_COLOR),
new Cell(0,5,BlockColor.T_COLOR),
new Cell(-1,4,BlockColor.T_COLOR), 'T', 0);
}
}
class ZBlock extends Block {
public ZBlock(){
super(new Cell(-1,3,BlockColor.Z_COLOR),
new Cell(0,4,BlockColor.Z_COLOR),
new Cell(-1,4,BlockColor.Z_COLOR),
new Cell(0,5,BlockColor.Z_COLOR), 'Z', 0);
}
}