-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
269 lines (240 loc) · 9.02 KB
/
Board.java
File metadata and controls
269 lines (240 loc) · 9.02 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Queue;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author UNA
*/
public class Board extends JPanel implements ActionListener {
private int rows = 22;
private int cols = 10;
private int score = 0;
private int blockSize = 45;
private int extraSpace = blockSize;
private int currX = 225;
private int currY = 0;
private Timer timer;
ArrayList<Piece> next = new ArrayList<>();
private Piece[][] board = new Piece[cols][rows];
Piece piece = new Piece();
int[][] pieceCoords = new int[4][2];
public Board() {
setFocusable(true);
setPreferredSize(new Dimension((cols * blockSize) + 350, (rows * blockSize)));
piece.newPiece();
pieceCoords = piece.getPieceCoords();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
board[i][j] = new Piece();
}
}
for (int i = 0; i < 4; i++) {
next.add(new Piece());
next.get(i).newPiece();
}
//music();
timer = new Timer(600, this);
timer.start();
addKeyListener(new KeyPress());
}
public void music() {
try {
String music = "C:\\Users\\thema_000\\Documents\\NetBeansProjects\\TetrisJava\\src\\resources\\Tetris.wav";
AudioInputStream in = AudioSystem.getAudioInputStream(new File(music));
Clip clip = AudioSystem.getClip();
clip.open(in);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception e) {
System.out.println(e);
}
}
public void start() {
timer.start();
}
public void place(int currX, int nextY) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
board[(currX / blockSize) + pieceCoords[i][0]][((nextY - blockSize) / blockSize) + pieceCoords[i][1]].setIsFilled(true);
board[(currX / blockSize) + pieceCoords[i][0]][((nextY - blockSize) / blockSize) + pieceCoords[i][1]].setTypePiece(piece.getTypePiece());
}
}
checkRowFilled();
this.currX = 225;
piece.newPiece();
pieceCoords = piece.getPieceCoords();
}
public boolean moveRight(int nextX) {
for (int i = 0; i < 4; i++) {
if (nextX + (pieceCoords[i][0] * blockSize) >= cols * blockSize || board[(nextX / blockSize) + (pieceCoords[i][0])][(currY / blockSize) + pieceCoords[i][1]].isIsFilled() == true) {
return false;
}
}
repaint();
currX = nextX;
return true;
}
public boolean moveLeft(int nextX) {
for (int i = 0; i < 4; i++) {
if (nextX + (pieceCoords[i][0] * blockSize) < 0 || board[(nextX / blockSize) + (pieceCoords[i][0])][(currY / blockSize) + pieceCoords[i][1]].isIsFilled() == true) {
return false;
}
}
repaint();
currX = nextX;
return true;
}
public boolean moveDown(int nextY) {
for (int i = 0; i < 4; i++) {
if (nextY >= (rows * blockSize) - (pieceCoords[i][1] * blockSize) || board[(currX / blockSize) + pieceCoords[i][0]][nextY / blockSize + (pieceCoords[i][1])].isIsFilled() == true) {
currY = 0;
piecePlaced();
place(currX, nextY);
for (int j = 0; j < 4; j++) {
if (board[(currX / blockSize) + pieceCoords[i][0]][(currY / blockSize) + pieceCoords[i][1]].isIsFilled() == true) {
JOptionPane.showMessageDialog(this, "Game over!\n Your score was: " + score);
System.exit(0);
}
}
repaint();
return false;
}
}
currY = nextY;
repaint();
return true;
}
public boolean rotate(Piece rotate) {
for (int i = 0; i < 4; i++) {
if ((currX / blockSize) - rotate.getPieceCoords()[i][0] <= 0 || (currX / blockSize) + rotate.getPieceCoords()[i][0] >= cols) {
return false;
} else if ((currY / blockSize) + rotate.getPieceCoords()[i][1] < 0 || (currY / blockSize) + rotate.getPieceCoords()[i][1] >= rows) {
return false;
} else if (board[(currX / blockSize) + rotate.getPieceCoords()[i][0]][(currY / blockSize) + rotate.getPieceCoords()[i][1]].isIsFilled() == true) {
return false;
}
}
piece = rotate;
pieceCoords = piece.getPieceCoords();
repaint();
return true;
}
public void paint(Graphics g) {
g.clearRect(0, 0, (cols * blockSize) + 350, (rows * blockSize));
g.setColor(new Color(226, 226, 226));
g.fillRect(0, 0, (cols * blockSize), rows * blockSize);
g.fillRect((cols * blockSize) + extraSpace + 50, 0, 200, 50);
g.fillRect((cols * blockSize) + extraSpace + 50, 75, 200, 800);
g.setColor(Color.black);
g.setFont(new Font("Serif", Font.PLAIN, 20));
g.drawString("SCORE:", (cols * blockSize) + extraSpace + 115, 20);
g.drawString(Integer.toString(score), (cols * blockSize) + extraSpace + 115, 40);
g.drawString("NEXT", (cols * blockSize) + extraSpace + 115, 110);
for (int i = 0; i < next.size(); i++) {
for (int j = 0; j < 4; j++) {
g.fillRect((cols * blockSize) + (next.get(i).getPieceCoords()[i][0] * blockSize) + 145, 150 + (next.get(i).getPieceCoords()[i][1] * blockSize), blockSize, blockSize);
}
}
int curr = 0;
// for (int i = 0; i < rows + 1; i++) {
// g.drawLine(0, curr, (cols * blockSize), curr);
// curr += extraSpace;
// }
// curr = extraSpace;
// for (int i = 0; i < cols; i++) {
// g.drawLine(curr, 0, curr, (rows * blockSize));
// curr += extraSpace;
// }
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
if (board[i][j].isIsFilled() == true) {
g.setColor(board[i][j].getColors()[board[i][j].getTypePiece()]);
g.fillRect(i * blockSize, j * blockSize, blockSize, blockSize);
}
}
}
g.setColor(piece.getColors()[piece.getTypePiece()]);
for (int i = 0; i < 4; i++) {
g.fillRect(currX + (pieceCoords[i][0] * blockSize), currY + (pieceCoords[i][1] * blockSize), blockSize, blockSize);
}
}
public void piecePlaced() {
score += 50;
}
public void checkRowFilled() {
int rowsFilled = 0;
int highestRow = 0;
for (int i = rows - 1; i >= 0; i--) {
int counter = 0;
for (int j = cols - 1; j >= 0; j--) {
if (board[j][i].isIsFilled() == false) {
break;
} else {
counter++;
}
}
if (counter == cols) {
highestRow = i;
for (int j = 0; j < cols; j++) {
board[j][i].setIsFilled(false);
}
rowsFilled++;
}
}
if (rowsFilled != 0) {
for (int i = cols - 1; i >= 0; i--) {
for (int j = highestRow; j >= 0; j--) {
if (board[i][j].isIsFilled() == true) {
board[i][j + rowsFilled] = board[i][j];
board[i][j] = new Piece();
}
}
}
}
rowFilled(rowsFilled);
}
public void rowFilled(int numRows) {
score += (1000 * (numRows == 4 ? 5 : numRows));
}
public void actionPerformed(ActionEvent e) {
moveDown(currY + blockSize);
}
class KeyPress extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_RIGHT:
moveRight(currX + blockSize);
break;
case KeyEvent.VK_LEFT:
moveLeft(currX - blockSize);
break;
case KeyEvent.VK_DOWN:
moveDown(currY + blockSize);
break;
case KeyEvent.VK_UP:
rotate(piece.rotate());
}
}
}
}