-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.java
272 lines (215 loc) · 6.18 KB
/
GameState.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
//This class is used to represent the current state of the Tetris game.
//This class handles scorekeeping and keeps track of the game logic.
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class GameState
{
private int score;
private int linesCleared;
private int level;
private PieceFactory p;
private Tetromino currentPiece;
private Tetromino nextPiece;
private Timer blockTimer;
public TimeHandler blockTimeHandler;
private Timer timeElapsed;
private int time;
private int[][] grid = new int[20][10];
private GameGUI gui;
private OnDeckGrid onDeck;
// initializes state of game
public GameState()
{
score = 0;
time = 0;
linesCleared = 0;
level = 1;
blockTimeHandler = new TimeHandler();
blockTimer = new Timer(800, blockTimeHandler);
TimeElapsedHandler timeElapsedHandler = new TimeElapsedHandler();
timeElapsed = new Timer(1000, timeElapsedHandler);
}
public void startTimer() {
gui = GameGUI.getInstance();
onDeck = gui.getOnDeck();
p = new PieceFactory();
linesCleared = 0;
gui.setLinesClearedLabel(""+linesCleared);
score = 0;
gui.setScoreLabel(""+score);
gui.getBoard().reset();
gui.getOnDeck().reset();
level = 1;
gui.setLevelLabel(""+level);
time = 0;
gui.setTimeLabel(""+time);
grid = new int[20][10];
currentPiece = p.getPiece(PieceType.getRandom());
nextPiece = p.getPiece(PieceType.getRandom());
currentPiece.drawPiece();
nextPiece.drawOnDeck();
blockTimer.start();
timeElapsed.start();
}
// called when a piece reaches the bottom of it's fall
public void pieceDropped()
{
score += 10;
// update grid
for (Pair p : currentPiece.getPositions())
grid[p.getY()][p.getX()] = currentPiece.blockColor;
// check for lines that need to be cleared
int c = clearLines();
// update score and GUI if lines were cleared
if (c > 0) {
gui.getBoard().updateSpace(grid);
linesCleared(c);
gui.setLinesClearedLabel(""+linesCleared);
}
//update score in GUI
gui.setScoreLabel("" + score);
// place next on deck at top of board
currentPiece = nextPiece;
if (spotOpen())
currentPiece.drawPiece();
else
gameLost();
// set up next piece on deck
nextPiece = p.getPiece(PieceType.getRandom());
nextPiece.drawOnDeck();
gui.setScoreLabel("" + score);
}
// returns true if there is a spot for the tetromino at
// the top of the board
// false if the piece can't be place, game will then end
public boolean spotOpen()
{
for (Pair p : currentPiece.getPositions())
{
if (grid[p.getY()][p.getX()] != 0)
return false;
}
return true;
}
// checks if any lines need to be cleared
// clears lines and implements naive gravity
// returns the number of lines that were cleared
public int clearLines()
{
int count = 0;
int i,j;
//loop through all rows, checking if they can be cleared
for (i=grid.length-1; i >= 0 && count < 4; i--)
{
for (j=0; j < grid[0].length; j++)
{
if (grid[i][j] == 0)
break;
}
if (j == grid[0].length) {//line should be cleared
count++;
// implement gravity
for (j=i; j > 0; j--) {
for (int k=0; k < grid[0].length; k++)
grid[j][k] = grid[j-1][k];
}
i++;
}
}
return count;
}
// update score after lines have been cleared
// update level if we have cleared some multiple of 10 lines
public void linesCleared(int n)
{
//update score based on n
switch (n)
{
case 1:
score += 40*level;
break;
case 2:
score += 100*level;
break;
case 3:
score += 300*level;
break;
case 4:
score += 1200*level;
break;
default: //error, called with invalid n
score = -1;
System.out.println("ERROR: linesCleared called with param " + n);
break;
}
// increment level every ten lines cleared, timer fires faster
int temp = linesCleared/10;
linesCleared += n;
if ( (linesCleared/10) > temp ){
level++;
gui.setLevelLabel(""+level);
blockTimer.setDelay((int)((((50 - (level*2))/60.0))*1000));
}
}
//function that gets called when the Timer fires
public class TimeHandler implements ActionListener{
public void actionPerformed( ActionEvent event )
{
// move piece down one,
// unless piece can't move down
if (currentPiece.moveDown() == false){
pieceDropped();
}
}
}
// function that gets called to keep track of total elasped time
public class TimeElapsedHandler implements ActionListener{
public void actionPerformed( ActionEvent event )
{
time++;
gui.setTimeLabel(""+time);
}
}
// called when the game has been lost
// ends play
public void gameLost()
{
blockTimer.stop();
timeElapsed.stop();
JOptionPane.showMessageDialog(null, "Game Over.\nPlease Try Again.",
"GAME OVER", JOptionPane.PLAIN_MESSAGE);
}
// prints ascii representation of grid for debugging
public void printGrid()
{
System.out.println("-------------------");
for (int i= grid.length-1 ; i >= 0; i--) {
for (int j=0; j < grid[i].length; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
System.out.println("-------------------");
}
// ****** METHODS TO MOVE TETROMINOES ******
public void moveLeft(){
currentPiece.moveLeft();
}
public void moveRight(){
currentPiece.moveRight();
}
public void rotateLeft(){
currentPiece.rotateLeft();
}
public void rotateRight(){
currentPiece.rotateRight();
}
// ****** END OF METHODS TO MOVE TETROMINOES ******
// Run this to start the program
public static void main(String[] args)
{
GameGUI gui = GameGUI.getInstance();
}
}