-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisPiece.java
More file actions
54 lines (47 loc) · 1.29 KB
/
TetrisPiece.java
File metadata and controls
54 lines (47 loc) · 1.29 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
/**
* The TetrisPiece abstract class represents a piece made of 4 TetrisBlocks. It
* maintains 4 rotations (0 degrees, 90 degrees, 180 degrees and 270 degrees),
* with each being a 4x4 grid with certain filled squares.
*
* @author Ching Ching Huang
*
*/
public abstract class TetrisPiece {
/**
* First dimension is 4 rotations. Second and third create 4x4 grid.
*/
protected boolean[][][] filledSquares;
protected int pieceRotation;// maintain current rotation
/**
* Constructor does nothing.
*/
public TetrisPiece() {
pieceRotation = 0;
filledSquares = new boolean[4][4][4];
}
// Rotate the piece clockwise by 90 degrees.
public void rotateCW() {
pieceRotation = (pieceRotation + 1) % 4;
}
// Rotate the piece counter clockwise by 90 degrees.
public void rotateCCW() {
pieceRotation = (pieceRotation - 1) % 4;
if (pieceRotation < 0) {
pieceRotation = 3;
}
}
// Get the rotation of this piece.
public int getPieceRotation() {
return pieceRotation;
}
// Checks if there is a TetrisBlock at the (row, col) position for the
// rotation rot,
public boolean isFilled(int rot, int row, int col) {
// return true if there is a block in the position for that rotation
if (filledSquares[rot][row][col] == true) {
return true;
} else {
return false;
}
}
}