-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.java
More file actions
101 lines (91 loc) · 2.13 KB
/
Board.java
File metadata and controls
101 lines (91 loc) · 2.13 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
package tetris;
import java.awt.Color;
import javax.swing.JPanel;
/**
* The game board that holds all the pieces and
* blocks that have been stacked.
*
* @author dev
*
*/
@SuppressWarnings("serial")
public class Board extends JPanel {
public final static int WIDTH = 10;
public final static int HEIGHT = 24;
public final static int BLOCKSIZE = 23;
Block[][] blocks;
Tetrimino current;
public Board() {
blocks = new Block[WIDTH][HEIGHT];
this.setBackground(Color.WHITE);
this.setLayout(null);
}
public void addToBoard(Tetrimino t) {
Block[][] conf = t.getConfiguration();
for (int i = 0; i < conf.length; i++) {
for (int j = 0; j < conf[i].length; j++) {
Block b = conf[i][j];
if (b != null) {
blocks[current.xPos + i][current.yPos + j] = b;
this.add(b);
t.remove(b);
b.setLocation((current.xPos + i) * BLOCKSIZE,
(current.yPos + j) * BLOCKSIZE);
}
}
}
revalidate();
repaint();
}
public boolean setNewTetrimino(Tetrimino t) {
if (current != null) {
this.remove(current);
}
current = t;
this.add(current);
current.setCollisionChecker(new CollisionChecker() {
public boolean collision(Block[][] conf) {
for (int i = 0; i < conf.length; i++) {
for (int j = 0; j < conf[i].length; j++) {
if (conf[i][j] != null) {
if (current.xPos + i >= blocks.length
|| current.yPos + j >= blocks[current.xPos
+ i].length
|| blocks[current.xPos + i][current.yPos
+ j] != null) {
return true;
}
}
}
}
return false;
}
});
if(current.collisionChecker.collision(current.getConfiguration())){
return false;
}
repaint();
return true;
}
@Override
public void repaint() {
super.repaint();
}
public void shiftDownFrom(int row) {
for (int i = 0; i < WIDTH; i++) {
this.remove(blocks[i][row]);
blocks[i][row] = null;
}
for (int r = row; r > 0; r--) {
for (int c = 0; c < WIDTH; c++) {
blocks[c][r] = blocks[c][r - 1];
if (blocks[c][r] != null) {
blocks[c][r].setLocation(c * BLOCKSIZE,
r * BLOCKSIZE);
}
}
}
revalidate();
repaint();
}
}