-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisPanel.java
More file actions
326 lines (316 loc) · 10.2 KB
/
TetrisPanel.java
File metadata and controls
326 lines (316 loc) · 10.2 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import java.awt.*;
import java.awt.event.*;
import java.util.Queue;
import javax.swing.*;
/**
* Represent the Tetris game Panel
* @author Acosta and Chen
* @version 1.7 4/8/14
*/
public class TetrisPanel extends JPanel
{
final int WIDTH = 12, HEIGHT = 22;
final int SPACE = 0, BLOCK = 1, WALL = 9;
final int SIDE = 25;//each grid is of SIDE pixels wide and high.
private Queue<SinglePiece> pieceQueue;//queue of the pieces to be displayed
private SinglePiece next, secondNext, thirdNext, fourthNext;//represent the coming pieces
private int x, y;//represent the upper-left grid of the piece as its coordinate
private int shape;//represent the shape of the current piece
private int status;//represent the status of the piece
private Color colorOfThisPiece;//represent the color of the current piece
private int[][] board = new int[WIDTH+1][HEIGHT+1];//the board is WIDTH grids wide & HEIGHT grids high
private int foresee;//pieces the player wants to foresee
private int score=0, highScore=500;
private int remaining;//number of pieces remained
private Timer timer;//the timer to control the dropping down of the piece
/**
* Constructor, create the board, the piece queue, and start the first piece
* @param foresee how many pieces the player wants to foresee
*/
public TetrisPanel(int foresee)
{
this.foresee = foresee;
timer = new Timer(1000, new TimerListener()); timer.start();//handle timer listener
setFocusable(true); requestFocusInWindow();
addKeyListener(new KeyboardListener());//handle key listener
newBoard();
newPieceQueue();
newPiece();
}
/**
* Paint the display of the panel.
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//draw the current piece:
g.setColor(colorOfThisPiece);
for (int k = 0; k < 16; k++)
if (SinglePiece.PIECES[shape][status][k] == BLOCK)
g.fillRect((k%4+1 + x)*SIDE+51, (k/4 + y)*SIDE+1, SIDE-1, SIDE-1);
//draw the current board:
g.setColor(Color.GRAY);
for (int j = 0; j < HEIGHT; j++)
for (int i = 0; i < WIDTH; i++)
{
if (board[i][j] == BLOCK)
g.fillRect(i*SIDE+51, j*SIDE+1, SIDE-2, SIDE-2);
if (board[i][j] == WALL)
g.drawRect(i*SIDE+51, j*SIDE+1, SIDE-2, SIDE-2);
}
//draw score:
g.setColor(Color.BLUE);
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
g.drawString("Score = "+score, WIDTH*SIDE+75, SIDE);
g.drawString("High Score = "+highScore, WIDTH*SIDE+75, 2*SIDE);
//draw coming piece(s):
g.drawString("Remaining Pieces: "+remaining,WIDTH*SIDE+75,4*SIDE);
if (foresee == 0) return;//foresee no piece.
g.drawString("What's Coming:",WIDTH*SIDE+75,5*SIDE);
g.setColor(Color.DARK_GRAY);
for (int k = 0; k < 16; k++)
if (next != null &&
SinglePiece.PIECES[next.getShape()][next.getStatus()][k] == BLOCK)
g.fillRect((k%4+1)*SIDE+WIDTH*SIDE+85, (k/4)*SIDE+6*SIDE, SIDE-1, SIDE-1);
if (foresee == 1) return;//foresee 1 piece; otherwise foresee 4 pieces
g.setColor(Color.DARK_GRAY);
for (int k = 0; k < 16; k++)
if (secondNext != null &&
SinglePiece.PIECES[secondNext.getShape()][secondNext.getStatus()][k] == BLOCK)
g.fillRect((k%4+1)*SIDE+WIDTH*SIDE+85, (k/4)*SIDE+10*SIDE+1, SIDE-1, SIDE-1);
g.setColor(Color.GRAY);
for (int k = 0; k < 16; k++)
if (thirdNext != null &&
SinglePiece.PIECES[thirdNext.getShape()][thirdNext.getStatus()][k] == BLOCK)
g.fillRect((k%4+1)*SIDE+WIDTH*SIDE+85, (k/4)*SIDE+14*SIDE+2, SIDE-1, SIDE-1);
g.setColor(Color.LIGHT_GRAY);
for (int k = 0; k < 16; k++)
if (fourthNext != null &&
SinglePiece.PIECES[fourthNext.getShape()][fourthNext.getStatus()][k] == BLOCK)
g.fillRect((k%4+1)*SIDE+WIDTH*SIDE+85, (k/4)*SIDE+18*SIDE+3, SIDE-1, SIDE-1);
}
/**
* Create a new game board (field)
*/
private void newBoard()
{
//Draw the empty game board
for (int i = 0; i < WIDTH; i++)
for (int j = 0; j < HEIGHT; j++)
board[i][j] = SPACE;
//Draw the bottom floor
for (int i = 0; i < WIDTH; i++)
board[i][HEIGHT-1] = WALL;
//Draw two side wall
for (int j = 0; j < HEIGHT; j++)
{
board[0][j] = WALL;
board[WIDTH-1][j] = WALL;
}
}
/**
* Create a new queue of pieces using PieceGenerator class.
*/
private void newPieceQueue()
{
PieceGenerator pg = new PieceGenerator();
pieceQueue = pg.getPieceQueue();
remaining = pieceQueue.size();
next = pieceQueue.poll();
secondNext = pieceQueue.poll();
thirdNext = pieceQueue.poll();
fourthNext = pieceQueue.poll();
}
/**
* Display the next piece on the top of the board.
*/
private void newPiece()
{
if (next == null)
{//Game Over when all pieces were polled out
timer.stop();
JOptionPane.showMessageDialog(null,"GAME FINISHED! Your score:"+score,
"Message", JOptionPane.INFORMATION_MESSAGE);
newGame();//Start a new game.
}
x = 4; y = 0;//each new piece starts at coordinate (4,0)
if (gameOver(x, y, next.getShape(), next.getStatus()))
{//Game Over when a new piece cannot enter in the board
timer.stop();
JOptionPane.showMessageDialog(null,"GAME OVER! Your score:"+score,
"Message", JOptionPane.INFORMATION_MESSAGE);
newGame();//Start a new game.
}
Color[] colorChoices = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,
Color.CYAN, Color.BLUE, Color.PINK, Color.MAGENTA};
colorOfThisPiece = colorChoices[(int)(Math.random()*colorChoices.length)];
shape = next.getShape();
status = next.getStatus();
next = secondNext;
secondNext = thirdNext;
thirdNext = fourthNext;
fourthNext = pieceQueue.poll();
remaining--;
}
/**
* Rotate the piece.
*/
private void rotate()
{
int temp = (status+1) % 4;//store the next status of this shape to temp
if (isValid(x,y,shape,temp))//if the rotation is valid
status = temp;//update the status
repaint();
}
/**
* Move the piece to the left.
*/
private void left()
{
if (isValid(x-1,y,shape,status))//if moving the piece to left is valid
x--;//move the piece to left
repaint();
}
/**
* Move the piece to the right.
*/
private void right()
{
if (isValid(x+1,y,shape,status))//if moving the piece to right is valid
x++;//move the piece to right
repaint();
}
/**
* Drop the piece down.
*/
private void down()
{
if (isValid(x,y+1,shape,status))//if moving the piece down is valid
y++;//move the piece down to one grid
else//if the piece cannot be moved down any more
{
updateBoard(x,y,shape,status);//add the piece to the board, remove, and score if needed
newPiece();//initiate the next piece
}
repaint();
}
/**
* Decide a new position for the piece is valid or not.
* @param x x_coordinate of the new position
* @param y y_coordinate of the new position
* @param shape the shape of the piece
* @param status the (new) status of the piece
* @return true if the new position is valid.
*/
private boolean isValid(int x, int y, int shape, int status)
{
for (int j=0; j<4; j++)
for (int i=0; i<4; i++)
if ( ((SinglePiece.PIECES[shape][status][j*4+i]==BLOCK)&&(board[x+i+1][y+j]==BLOCK)) ||
((SinglePiece.PIECES[shape][status][j*4+i]==BLOCK)&&(board[x+i+1][y+j]==WALL)) )
return false;
return true;
}
/**
* Called when a piece hit the ground (board), clear the filled row (if needed) and update the score.
* @param x x_coordinate of the piece to be updated to board
* @param y y_coordinate of the piece to be updated to board
* @param shape the shape of the piece to be updated to board
* @param status the status of the piece to be updated to board
*/
private void updateBoard(int x, int y, int shape, int status)
{
int k = 0;//k iterates all 16 grids of the piece
//update the 4*4 grid of the piece to the board
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
{
if (board[x+j+1][y+i] == SPACE) board[x+j+1][y+i] = SinglePiece.PIECES[shape][status][k];
k++;
}
//remove the full row(s)
int count = 0, numOfFull = 0;
for (int j=0; j<HEIGHT; j++)
{
for (int i=0; i<WIDTH; i++)
if (board[i][j] == BLOCK)
count++;
if (count == WIDTH-2)//this row is filled full
numOfFull++;
for (int n=j; n>0; n--)
for (int m=0; m<WIDTH-1; m++)
bhttp://www.webassign.net/oard[m][n]=board[m][n-1];
count = 0;
}
//update score if removing row(s) happened
if (numOfFull==0) score ++;
if (numOfFull==1) score += 10;
if (numOfFull==2) score += 30;//bonus for two rows removing
if (numOfFull==3) score += 60;//bonus for three rows removing
if (numOfFull==4) score += 100;//bonus for four rows removing
if (score>highScore) highScore = score;
}
/**
* Called when a new piece entered the board to see if the game is over due to its entrance
* @param x x_coordinate of the piece
* @param y y_coordinate of the piece
* @param shape the shape of the piece
* @param status the status of the piece
* @return true if the game is over due to this new piece's entrance
*/
private boolean gameOver(int x, int y, int shape, int status)
{
if (!isValid(x,y,shape,status))//if the new piece cannot be initialized at initial coordinate (4,0)
return true;
return false;
}
/**
* Renew the board, the queue of the pieces, and score to start a new game.
*/
private void newGame(){
newBoard();
newPieceQueue();
score = 0;
timer.start();
}
/**
* ActionListener for the timer
* @author Acosta and Chen
* @version 1.2 4/6/14
*/
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
down();
}
}
/**
* KeyListener for the control of the piece
* @author Acosta and Chen
* @version 1.4 4/7/14
*/
private class KeyboardListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_UP:
rotate();
break;
case KeyEvent.VK_DOWN:
down();
break;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
}