-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTetrimino.java
More file actions
207 lines (182 loc) · 4.41 KB
/
Tetrimino.java
File metadata and controls
207 lines (182 loc) · 4.41 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
package tetris;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
@SuppressWarnings("serial")
/**
* Represents one tetris 'piece', which consists of four blocks.
*
* @author dev
*
*/
public class Tetrimino extends JPanel {
private int active;
int xPos, yPos;
private ArrayList<Block[][]> configurations;
protected CollisionChecker collisionChecker;
public static Tetrimino getNext() {
switch ((int) (Math.random() * 7)) {
case 0:
return new Tetrimino('i');
case 1:
return new Tetrimino('l');
case 2:
return new Tetrimino('j');
case 3:
return new Tetrimino('o');
case 4:
return new Tetrimino('s');
case 5:
return new Tetrimino('z');
case 6:
return new Tetrimino('t');
default:
return null;
}
}
private Tetrimino(char type) {
active = 0;
xPos = (Board.WIDTH / 2) - 1;
yPos = 0;
configurations = BlockConfiguration.getForType(type);
for (Block[][] conf : configurations) {
setBlockColors(conf, getColor(type));
}
// this.setBackground(Color.BLACK);
this.setOpaque(false);
this.setLayout(null);
updatePosition(0, 0);
updateContained();
validate();
repaint();
}
public void setCollisionChecker(CollisionChecker c) {
this.collisionChecker = c;
}
private void updateContained() {
Block[][] conf = getConfiguration();
this.setBounds(Board.BLOCKSIZE * xPos, Board.BLOCKSIZE * yPos,
conf.length * Board.BLOCKSIZE, conf[0].length * Board.BLOCKSIZE);
this.setPreferredSize(new Dimension(conf.length * Board.BLOCKSIZE,
conf[0].length * Board.BLOCKSIZE));
this.removeAll();
updateBlocks();
validate();
repaint();
}
private void setBlockColors(Block[][] conf, Color c) {
for (int i = 0; i < conf.length; i++) {
for (int j = 0; j < conf[i].length; j++) {
if (conf[i][j] != null) {
conf[i][j].setColor(c);
}
}
}
}
private Color getColor(char type) {
switch (type) {
case 'i':
return Color.CYAN;
case 'j':
return Color.MAGENTA;
case 'l':
return Color.ORANGE;
case 's':
return Color.BLUE;
case 'z':
return Color.GREEN;
case 'o':
return Color.RED;
case 't':
return Color.YELLOW;
default:
return Color.BLACK;
}
}
public Block[][] getConfiguration() {
return configurations.get(active);
}
public Block[][] getNextLeftRotation(){
if (active - 1 >= 0) {
return configurations.get(active - 1);
} else {
return configurations.get(configurations.size() - 1);
}
}
public Block[][] getNextRightRotation(){
if (1 + active < configurations.size()) {
return configurations.get(1 + active);
} else {
return configurations.get(0);
}
}
public void rotateRight() {
Block[][] conf = getNextRightRotation();
if (withinBoundaries(conf) && !collisionChecker.collision(conf)) {
active = ++active < configurations.size() ? active : 0;
updateContained();
}
}
public void rotateLeft() {
Block[][] conf = getNextLeftRotation();
if (withinBoundaries(conf) && !collisionChecker.collision(conf)) {
active = --active >= 0 ? active : configurations.size() - 1;
updateContained();
}
}
public void shiftRight() {
Block[][] conf = getConfiguration();
xPos++;
if (canShiftRight(conf) && !collisionChecker.collision(conf)) {
updatePosition(1, 0);
} else {
xPos--;
}
}
public void shiftLeft() {
Block[][] conf = getConfiguration();
xPos--;
if (canShiftLeft(conf) && !collisionChecker.collision(conf)) {
updatePosition(-1, 0);
} else {
xPos++;
}
}
public void shiftDown() {
yPos++;
updatePosition(0, 1);
}
public void updatePosition(int dx, int dy) {
this.setLocation(0 + (Board.BLOCKSIZE * xPos),
0 + (Board.BLOCKSIZE * yPos));
}
public void updateBlocks() {
Block[][] conf = getConfiguration();
for (int i = 0; i < conf.length; i++) {
for (int j = 0; j < conf[i].length; j++) {
if (conf[i][j] != null) {
conf[i][j].setLocation(Board.BLOCKSIZE * i, Board.BLOCKSIZE
* j);
this.add(conf[i][j]);
}
}
}
}
public boolean withinBoundaries(Block[][] conf) {
return xPos + conf.length - 1 < Board.WIDTH && xPos >= 0;
}
public boolean canShiftRight(Block[][] conf) {
return xPos + conf.length - 1 < Board.WIDTH;
}
public boolean canShiftLeft(Block[][] conf) {
return xPos >= 0;
}
public boolean canShiftDown() {
yPos++;
boolean canShift = yPos < Board.HEIGHT
&& !collisionChecker.collision(getConfiguration());
yPos--;
return canShift;
}
}