-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTile.java
319 lines (208 loc) · 7.02 KB
/
Tile.java
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
//Amy Wang
//Final Project Part 2 - TetrisPiece.java
//5/11/15
//A simple tetris piece, randomly generated upon construction, that exhibits certain behavior
//depending on user input passed in by the TetrisBoard program
import java.util.*;
import java.awt.*;
public class Tile
{
/* The length of one side of one of the Location objects composing the Tile
*/
private int locsize;
/* The character corresponding to one of the four convential Tetris piece shapes--
* dictates the shape of the Tile (must be either "I", "L", "O", "Z")
*/
private char tiletype;
/* The Color in which the Tile will be filled in on the TetrisBoard
*/
private Color tilecolor;
/* The Graphics object upon which the Tile will be drawn
*/
private Graphics g;
/* An array containing all the Locations currently occupied by the Tile
*/
private Location[] locations;
/* The TetrisBoard upon which the Tile is located
*/
private TetrisBoard board;
/* Constructor method; creates a new Tile with a random Color and a random type. Depending on the
* tiletype generated by generateType(random), fills in the indices of the locations array with the
* correct arrangement of Locations (one long column of Locations for 'I', an L shaped arrangement for 'L', etc.)
* @param b - the TetrisBoard upon which the Tile is located
* @param size - the length of one side of one of the Location objects composing the Tile
*/
public Tile (TetrisBoard b, int size)
{
board = b;
locsize = size;
locations = new Location[4];
Random random = new Random();
tiletype = generateType(random);
tilecolor = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
if(tiletype=='I')
{
for(int ii=0; ii<locations.length; ii++)
{
locations[ii] = new Location(locsize, 0, ii, tilecolor);
}
}
else if (tiletype=='L')
{
for(int ii=0; ii<locations.length-1; ii++)
{
locations[ii] = new Location(locsize, 0, ii, tilecolor);
}
locations[locations.length-1] = new Location(locsize, 1, locations.length-2, tilecolor);
}
else if (tiletype=='O')
{
for(int ii=0; ii<2; ii++)
{
locations[ii] = new Location(locsize, 0, ii, tilecolor);
}
for(int ii=2; ii<4; ii++)
{
locations[ii] = new Location(locsize, 1, ii-2, tilecolor);
}
}
else if (tiletype=='Z')
{
for(int ii=0; ii<2; ii++)
{
locations[ii] = new Location(locsize, 0, ii, tilecolor);
}
for(int ii=2; ii<4; ii++)
{
locations[ii] = new Location(locsize, 1, ii-1, tilecolor);
}
}
/*else
{
throw new Exception("Invalid Piece");
}*/
}
/* Returns the "locations" array composing the shape of the Tile
* @return locations - an array containing all the Locations currently occupied by the Tile
*/
public Location[] getLocations()
{
return locations;
}
/* Returns the "locations" array composing the shape of the Tile
* @return locations - an array containing all the Locations currently occupied by the Tile
*/
public Color getColor()
{
return tilecolor;
}
/* "Rotates" the piece counterclockwise 90 degrees - implementation unfinished due to lack of time :(
*/
public void rotate()
{
//Location[] templocs = locations;
}
/* Draws the Tile onto the screen by filling in each Location contained in the locations array, using
* the drawSelf(g) method found in the Location class. Also literally "fills in" the Locations by using the
* Locations.fill() method to set the "full" instance variable to be true for each Location in the array.
* @param g - the Graphics object used to draw the image onto the sceen
*/
public void drawSelf(Graphics g)
{
for(int ii=0; ii<locations.length; ii++)
{
Location l = locations[ii];
l.fill();
l.drawSelf(g);
}
}
/* Randomly generates a tile type, using a Random object and if/else logic
* to assign a character (either I, O, L, or Z) to the Tile
* @param random - a Random object used to generate a random integer used to generate a random character or type for the Tile
* @return - the character, corresponding to a certain "Tile type", that will be assigned
* to the Tile
*/
public char generateType(Random random) //maybe move to Tile method?
{
int tiletype = random.nextInt(4);
if(tiletype==0)
{
return 'I';
}
else if(tiletype==1)
{
return 'O';
}
else if(tiletype==2)
{
return 'L';
}
else
{
return 'Z';
}
}
/* "Moves" the piece by erasing each location and moving it 1 block to the left, right, or down, depending on the parameter,
* Parameter is passed-in by the TetrisBoard program, which takes in user input (i.e. key pressing) and converts it to calls on
* this method
* @param key - a character value responding to the key that was pressed - either "<", ">", "^", or "v" (borrowed shamelessly from
* the Bird class of assignment 10)
* @return - true if the Tile moved successfully, false otherwise.
*/
public boolean move(char key)
{
boolean canmove = true;
Location[] newloc = new Location[locations.length];
for(int ii=0; ii<locations.length; ii++)
{
Location temp = locations[ii];
int tempx = temp.getX();
int tempy = temp.getY();
if(key=='v')
{
tempy += 1;//adds locsize to the y value of the point--moves the location down 1 unit
}
else if(key=='^')
{
rotate();
}
else if(key=='<')
{
tempx -= 1;
}
else if(key=='>')
{
tempx += 1; //adds locsize to the y value of the point--moves the location down 1 unit
}
/* else throw exception
else
{
locations[ii] = newloc;
} */
Location loc = new Location(locsize, tempx, tempy, tilecolor);
if(!board.checkFull(loc))
{
newloc[ii] = loc;
}
else
{
canmove = false;
}
}
if(canmove)
{
locations = newloc;
}
return canmove;
}
/* Erases the Tile from the board by emptying out each of the Locations in the locations array
*/
public void eraseSelf()
{
for(int ii=0; ii<locations.length; ii++)
{
Location l = locations[ii];
l.empty();
}
}
}