-
Notifications
You must be signed in to change notification settings - Fork 2
/
Piece.java
304 lines (263 loc) · 9.69 KB
/
Piece.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
import java.awt.Point;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.StringTokenizer;
/**
* An immutable representation of a tetris piece in a particular rotation.
* Each piece is defined by the blocks that make up its body.
* See the lab document for an overview.
*
* @author Nick Parlante
* @version 1.0, Mar 1, 2001
*/
public final class Piece {
/*
* Implementation hints:
* -The starter code does a few simple things for you
* -The attributes of the Point class are x and y and are public
* -Do not assume there are 4 points in the body -- use array.length
* to keep the code general
*/
private Point[] body;
private int[] skirt;
private int width;
private int height;
private Piece next; // "next" rotation
static private Piece[] pieces; // singleton array of first rotations
/**
* Defines a new piece given the Points that make up its body.
* Makes its own copy of the array and the Points inside it.
* Does not set up the rotations.
*
* This constructor is PRIVATE -- if a client
* wants a piece object, they must use Piece.getPieces().
*
* @param points array of points that make up this Piece's body
*/
private Piece(Point[] points)
{
// initialize next to null; it will be initialized in the pieceRow method
this.next = null;
// TODO: copy the points array and copy the Point elements in the array
// Note: this.body = points copies the reference to the array referenced by points;
// it does not create a new array of references to Point objects
// Note: this.body[i] = points[i] (or Arrays.copyOf) copies a reference to a Point
// object; it does not create a new Point object with the same x and y attributes
// as the element in the points array
// TODO: initialize the width instance variable with the width of the piece
// TODO: initialize the height instance variable with the height of the piece
// TODO: initialize the skirt instance variable
// Note: carefully read and description of the skirt in the lab document;
// this is the most challenging algorithm in this constructor
// skirt psuedocode
// create a new skirt array with the appropriate number of elements
// initialize each element in the skirt to a "large" value (e.g., height)
// for each point in the body:
// get the x value
// get the y value
// check if the y value is less than the value in the skirt
// at the index equal to the x value
// if so, update the value in the skirt
}
/**
* Returns the width of the piece measured in blocks.
*
* @return the width of the piece measured in blocks
*/
public int getWidth()
{
return this.width;
}
/**
* Returns the height of the piece measured in blocks.
*
* @return the height of the piece measured in blocks
*/
public int getHeight()
{
return this.height;
}
/**
* Returns a reference to this piece's body.
* The caller should not modify this array.
*
* @return a reference to this piece's body
*/
public Point[] getBody()
{
return this.body;
}
/**
* Returns a reference to this piece's skirt.
* For each x value across the piece, the skirt gives the lowest y value
* in the body.
* This useful for computing where the piece will land.
* The caller should not modify this array.
*
* @return a reference to this piece's skirt
*/
public int[] getSkirt()
{
return this.skirt;
}
/**
* Returns a reference to a Piece that is 90 degrees counter-clockwise
* rotated from this Piece.
*
* Implementation:
* The Piece class pre-computes all the rotations once.
* This method just hops from one pre-computed rotation to the next in
* constant time.
*
* @return a reference to a Piece that is 90 degrees counter-clockwise
* rotated from this Piece
*/
public Piece nextRotation()
{
return this.next;
}
/**
* Returns true if two pieces are the same -- their bodies contain the same
* points.
* Interestingly, this is not the same as having exactly the same body
* arrays, since the points may not be in the same order in the bodies.
* Used internally to detect if two rotations are effectively the same.
*
* @param other the object with which to test equality
* @return true if two pieces are the same
*/
@Override
public boolean equals(Object other)
{
// self check
if(this == other)
{
return true;
}
// null check
if(other == null)
{
return false;
}
// type check and cast
if(this.getClass() != other.getClass())
{
return false;
}
Piece otherPiece = (Piece)other;
// field comparison
Set<Point> thisSet = new HashSet<Point>(Arrays.asList(this.body));
Set<Point> otherSet = new HashSet<Point>(Arrays.asList(otherPiece.body));
return thisSet.equals(otherSet);
}
/**
* Returns the attributes of the Piece as a String
*
* @return the attributes of the Piece as a String
*/
@Override
public String toString()
{
String str = "";
// TODO: build a string that contains all of the attributes of this Piece
return str;
}
/**
* Returns an array containing the first rotation of each of the 7 standard
* tetris pieces. The next (counterclockwise) rotation can be obtained
* from each piece with the {@link #nextRotation()} method. In this way,
* the client can iterate through all the rotations until eventually
* getting back to the first rotation.
*
* @return an array containing the first rotation of each of the 7 standard
* tetris pieces
*/
public static Piece[] getPieces() {
if(Piece.pieces != null)
{
return Piece.pieces;
}
Piece.pieces = new Piece[]
{
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 0 1 0 2 0 3"))), // 0
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 0 1 0 2 1 0"))), // 1
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 1 0 1 1 1 2"))), // 2
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 1 0 1 1 2 1"))), // 3
Piece.pieceRow(new Piece(Piece.parsePoints("0 1 1 1 1 0 2 0"))), // 4
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 0 1 1 0 1 1"))), // 5
Piece.pieceRow(new Piece(Piece.parsePoints("0 0 1 0 1 1 2 0"))), // 6
};
return Piece.pieces;
}
/**
* Computes all the rotations of the specified Piece and connects them by
* their next attributes
*
* @param piece the specified piece from which to compute all the
* rotations
* @return a reference to the specified piece
*/
private static Piece pieceRow(Piece firstPiece)
{
Piece piece = firstPiece;
System.out.println("\nfirst piece: " + piece);
// maximum of 4 rotations until we are back at the first piece (we may break earlier)
for( int j = 0; j < 4; j++)
{
// copy the points from the specified piece before transforming
Point[] rotatedPoints = new Point[piece.getBody().length];
for(int i = 0; i < rotatedPoints.length; i++)
{
rotatedPoints[i] = new Point(piece.getBody()[i]);
}
// TODO: step 1: reflect across the line y = x
// TODO: step 2: reflect across y axis
// TODO: step 3: translate right
// create the rotated piece, update next, prepare for nextIteration
Piece rotatedPiece = new Piece(rotatedPoints);
System.out.println(rotatedPiece);
// check if we are back to the original piece
if(rotatedPiece.equals(firstPiece))
{
// the previous piece links back to the original piece
piece.next = firstPiece;
break;
}
else
{
// update the next attribute and prepare for the next rotation
piece.next = rotatedPiece;
piece = rotatedPiece;
}
}
return firstPiece;
}
/**
* Given a string of x,y pairs ("0 0 0 1 0 2 1 0"), parses the points into
* a Point[] array.
*/
private static Point[] parsePoints(String string)
{
ArrayList<Point> points = new ArrayList<>();
StringTokenizer tok = new StringTokenizer(string);
try
{
while(tok.hasMoreTokens())
{
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
points.add(new Point(x, y));
}
}
catch (NumberFormatException e)
{
throw new RuntimeException("Could not parse x,y string:" + string);
}
// Make an array out of the Vector
Point[] array = new Point[points.size()];
array = points.toArray(array);
return(array);
}
}