-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetroState.java
251 lines (217 loc) · 5.2 KB
/
TetroState.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
package animation;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JLabel;
/**
* Keeps the state of the fallen tetrominis.
*
* @author johnsonhsiung
*
*/
public class TetroState extends JLabel
{
/**
* Initializes an array of the rectangles of the grid with the color of what it
* should be drawn as.
*/
public TetroState()
{
fallenShapes = new Rectangle2D[10][22];
colorOfBlocks = new Color[10][22];
}
/**
* Paints the state of the tetris animation.
*
* @param g The graphical context.
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 22; j++)
{
if (fallenShapes[i][j] != null)
{
g2.setColor(colorOfBlocks[i][j]);
g2.fill(fallenShapes[i][j]);
g2.setColor(Color.BLACK);
g2.draw(fallenShapes[i][j]);
}
}
}
}
/**
* Adds the tetromini to the state of the tetris animation.
*
* @param tetro The tetromini to be added.
*/
public void addTetro(Tetromini tetro)
{
Rectangle2D[] tetroRectangles = new Rectangle2D[4];
tetroRectangles = tetro.getRectangles();
Color currentColor = tetro.getColor();
for (int i = 0; i < 4; i++)
{
Rectangle2D currentRect = tetroRectangles[i];
int currentGridX = Grid.getGridUnitRounded((int) currentRect.getX());
int currentGridY = Grid.getGridUnitRounded((int) currentRect.getY());
if (fallenShapes[currentGridX][currentGridY] == null)
{
fallenShapes[currentGridX][currentGridY] = currentRect;
colorOfBlocks[currentGridX][currentGridY] = currentColor;
} else
{
break;
}
}
}
/**
* Checks to see if the tetromini should stop given the current state.
*
* @param mini the mini to be checked.
* @return True if it should stop, false if it should not.
*/
public boolean shouldStop(Tetromini mini)
{
if (mini.rect[3].getMaxY() >= Grid.getPixelUnit(Grid.ROWS))
{
return true;
}
int maxYOfBlock;
int xOfBlock;
for (int i = 0; i < 4; i++)
{
maxYOfBlock = mini.getGridMaxYOfRect(i);
xOfBlock = mini.getGridXOfRect(i);
if (fallenShapes[xOfBlock][maxYOfBlock] != null)
{
return true;
}
}
return false;
}
/**
* Checks if the mini can rotate given the current state.
*
* @param mini The mini to check.
* @return True if mini can rotate. False otherwise.
*/
public boolean canRotate(Tetromini mini)
{
if (Grid.getGridUnitRounded(mini.topLeftY) > Grid.ROWS - 4)
{
return false;
}
int xStart = Grid.getGridUnitRounded(mini.topLeftX);
int yStart = Grid.getGridUnitRounded(mini.topLeftY);
int range = mini.getRotationRange();
for (int i = xStart; i < xStart + range && i < Grid.COLUMNS; i++)
{
for (int j = yStart; j < yStart + range && j < Grid.ROWS; j++)
{
if (fallenShapes[i][j] != null || i >= Grid.COLUMNS - range)
{
return false;
}
}
}
return true;
}
/**
* Checks if the mini can translate left given the current state.
*
* @param mini The tetromini to check.
* @return True if it can translate left, false otherwise.
*/
public boolean canTranslateLeft(Tetromini mini)
{
if (mini.topLeftX == 0)
{
return false;
}
int xTranslateBoundary;
int yTranslateBoundary;
for (int i = 0; i < 4; i++)
{
xTranslateBoundary = mini.getGridXOfRect(i);
yTranslateBoundary = mini.getGridYOfRect(i);
if (yTranslateBoundary < 0)
{
yTranslateBoundary = 0;
}
if (yTranslateBoundary > 18)
{
yTranslateBoundary = 19;
}
if (fallenShapes[xTranslateBoundary - 1][yTranslateBoundary] != null
|| fallenShapes[xTranslateBoundary - 1][yTranslateBoundary + 1] != null
|| fallenShapes[xTranslateBoundary - 1][yTranslateBoundary + 2] != null)
{
return false;
}
}
return true;
}
/**
* Checks if the mini can translate right given the current state.
*
* @param mini The mini to be checked.
* @return True if mini can translate right, false otherwise.
*/
public boolean canTranslateRight(Tetromini mini)
{
if (Grid.getGridUnitRounded(mini.topLeftX) >= Grid.COLUMNS - mini.getRotationRange())
{
return false;
}
int xTranslateBoundary;
int yTranslateBoundary;
for (int i = 0; i < 4; i++)
{
xTranslateBoundary = mini.getGridXOfRect(i) + 1;
yTranslateBoundary = mini.getGridYOfRect(i);
if (yTranslateBoundary < 0)
{
yTranslateBoundary = 0;
}
if (yTranslateBoundary > Grid.ROWS - 4)
{
yTranslateBoundary = Grid.ROWS - 3;
}
if (xTranslateBoundary >= Grid.COLUMNS)
{
xTranslateBoundary = Grid.COLUMNS - 1;
}
if (fallenShapes[xTranslateBoundary][yTranslateBoundary] != null
|| fallenShapes[xTranslateBoundary][yTranslateBoundary + 1] != null
|| fallenShapes[xTranslateBoundary][yTranslateBoundary + 2] != null)
{
return false;
}
}
return true;
}
/**
* Checks if the game is over.
*
* @return True if game is over, false otherwise.
*/
public boolean isGameOver()
{
for (int i = 0; i < Grid.COLUMNS; i++)
{
if (fallenShapes[i][0] != null)
{
return true;
}
}
return false;
}
private Rectangle2D[][] fallenShapes;
private Color[][] colorOfBlocks;
}