-
Notifications
You must be signed in to change notification settings - Fork 1
/
EdgePiece.java
65 lines (57 loc) · 1.84 KB
/
EdgePiece.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
public class EdgePiece extends Piece {
public final static int EDGE_NUM_OF_ZEROS = 1;
public String type;
// Constructor
public EdgePiece(int numOfPiece, int topSlice, int rightSlice, int bottomSlice, int leftSlice) {
super(numOfPiece, topSlice, rightSlice, bottomSlice, leftSlice);
checkIfEdgePiece();
setType();
}
// Checks if a piece is a Edge Piece
public void checkIfEdgePiece() {
int numOfZeroSlices = 0;
if (getTopSlice() == 0)
numOfZeroSlices++;
if (getRightSlice() == 0)
numOfZeroSlices++;
if (getBottomSlice() == 0)
numOfZeroSlices++;
if (getLeftSlice() == 0)
numOfZeroSlices++;
if (numOfZeroSlices != EDGE_NUM_OF_ZEROS)
throw new IllegalArgumentException("The Piece number " + getNumOfPiece() + " is not an invalid "
+ getClass().getCanonicalName() + ".\nIt has only " + numOfZeroSlices
+ " \"Zero slices\" and it should have " + EDGE_NUM_OF_ZEROS);
}
// Sets the type of the Edge Piece
@Override
public void setType() {
if (getTopSlice() == 0)
type = PIECE_TYPE[TOP];
if (getRightSlice() == 0)
type = PIECE_TYPE[RIGHT];
if (getBottomSlice() == 0)
type = PIECE_TYPE[BOTTOM];
if (getLeftSlice() == 0)
type = PIECE_TYPE[LEFT];
}
// Gets the type of the Edge Piece
public String getType() {
return this.type;
}
// Clones the Edge Piece
@Override
public EdgePiece clone() throws CloneNotSupportedException {
return (EdgePiece) super.clone();
}
// Change the orientation of a piece
@Override
public void changeOrientationOfAPiece(int orientation) {
if (!(4 <= orientation && orientation <= 7)) {
throw new IllegalArgumentException(
"The orientation of an Edge Piece should be in range of 4-7 (Top, Right, Bottom, Left");
}
while (!getType().equals(PIECE_TYPE[orientation]))
rotateClockwise();
}
}