1 package erland.game.tetris;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.awt.*;
22
23 /***
24 * Represents a tetris block, all Block sub classes should
25 * be derived from this
26 * @author Erland Isaksson
27 */
28 abstract class Block implements Cloneable
29 {
30 /*** rotation angle of block */
31 protected int rotation;
32 /*** x position of block(This is a block coordinate and not a pixel coordinate) */
33 protected int x;
34 /*** y position of block(This is a block coordinate and not a pixel coordinate) */
35 protected int y;
36
37 /***
38 * Get the color of the block
39 * @return The color of the block
40 */
41 public abstract Color getColor();
42
43 public Object clone() {
44 try {
45 return super.clone();
46 } catch (CloneNotSupportedException e) {
47 return null;
48 }
49 }
50 /***
51 * Get the rotation of the block
52 * @return The rotation of the block 0,1,2,3
53 */
54 public int getRotation() {
55 return rotation;
56 }
57
58 public int getX() {
59 return x;
60 }
61
62 public int getY() {
63 return y;
64 }
65 /***
66 * Initialize block
67 * @param x x position of block(Block coordinate)
68 * @param y y position of block(Block coordinate)
69 * @param rotation rotation angle of block
70 */
71 protected void init(int x, int y, int rotation)
72 {
73 this.x = x;
74 this.y = y;
75 this.rotation = rotation;
76 }
77
78 /***
79 * Move block once step down
80 * @param m The BlockMatrix in which the block resides
81 * @return true/false (Success/Failure)
82 */
83 public boolean moveDown(BlockMatrix m)
84 {
85 unset(m,x,y,rotation);
86 if(check(m,x,y+1,rotation)) {
87 set(m,x,y+1,rotation);
88 return true;
89 }else {
90 set(m,x,y,rotation);
91 return false;
92 }
93 }
94
95
96 /***
97 * Move the block one step right
98 * @param m The BlockMatrix in which the block resides
99 * @return true/false (Success/Failure)
100 */
101 public boolean moveRight(BlockMatrix m)
102 {
103 unset(m,x,y,rotation);
104 if(check(m,x+1,y,rotation)) {
105 set(m,x+1,y,rotation);
106 return true;
107 }else {
108 set(m,x,y,rotation);
109 return false;
110 }
111 }
112
113 /***
114 * Move the block one step left
115 * @param m The BlockMatrix in which the block resides
116 * @return true/false (Success/Failure)
117 */
118 public boolean moveLeft(BlockMatrix m)
119 {
120 unset(m,x,y,rotation);
121 if(check(m,x-1,y,rotation)) {
122 set(m,x-1,y,rotation);
123 return true;
124 }else {
125 set(m,x,y,rotation);
126 return false;
127 }
128 }
129
130 /***
131 * Rotate the block 90 degrees clockwize
132 * @param m The BlockMatrix in which the block resides
133 * @return true/false (Success/Failure)
134 */
135 public boolean rotateRight(BlockMatrix m)
136 {
137 unset(m,x,y,rotation);
138 if(check(m,x,y,rotation+90)) {
139 set(m,x,y,rotation+90);
140 return true;
141 }else {
142 set(m,x,y,rotation);
143 return false;
144 }
145 }
146
147 /***
148 * Rotate the block 90 degrees counter clockwize
149 * @param m The BlockMatrix in which the block resides
150 * @return true/false (Success/Failure)
151 */
152 public boolean rotateLeft(BlockMatrix m)
153 {
154 unset(m,x,y,rotation);
155 if(check(m,x,y,rotation-90)) {
156 set(m,x,y,rotation-90);
157 return true;
158 }else {
159 set(m,x,y,rotation);
160 return false;
161 }
162 }
163
164 /***
165 * Check if it is possible to put the block in the specified position and
166 * rotation angle
167 * @param m The BlockMatrix in which the block resides
168 * @param x The x position which should be checked(Block coordinate)
169 * @param y The y position which should be checked(Block coordinate)
170 * @param rotation The rotation angle which should be checked
171 * @return true/false (Success/Failure)
172 */
173 protected abstract boolean check(BlockMatrix m, int x, int y, int rotation);
174
175 /***
176 * Put the block in the specified position and rotation angle
177 * @param m The BlockMatrix in which the block resides
178 * @param x The x position (Block coordinate)
179 * @param y The y position (Block coordinate)
180 * @param rotation The rotation angle
181 */
182 protected abstract void set(BlockMatrix m, int x, int y, int rotation);
183
184 /***
185 * Remove the block from the specified position and rotation angle
186 * @param m The BlockMatrix in which the block resides
187 * @param x The x position (Block coordinate)
188 * @param y The y position (Block coordinate)
189 * @param rotation The rotation angle
190 */
191 protected abstract void unset(BlockMatrix m, int x, int y, int rotation);
192
193 }