-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.java
More file actions
151 lines (139 loc) · 3.61 KB
/
Piece.java
File metadata and controls
151 lines (139 loc) · 3.61 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
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
/**
* Represents a tile on the board.
*/
enum Tile {
EMPTY,
BLACK,
WHITE
}
/**
* Represents a piece on the board.
*/
public class Piece {
Tile pieceType;
/**
* Creates an empty piece.
*/
public Piece() {
this.pieceType = Tile.EMPTY;
}
/**
* Creates a piece of a given type.
*
* @param piece The type of piece to create.
*/
public Piece(Tile piece) {
this.pieceType = piece;
}
/**
* Sets the type of this piece.
*
* @param piece The type of piece to set this piece to.
*/
public void setPiece(Tile piece) {
this.pieceType = piece;
}
/**
* Gets the type of this piece.
*
* @return The type of this piece.
*/
public Tile getType() {
return pieceType;
}
/**
* Checks if this piece is black.
*
* @return True if this piece is black, false otherwise.
*/
public boolean isBlack() {
return pieceType == Tile.BLACK;
}
/**
* Checks if this piece is white.
*
* @return True if this piece is white, false otherwise.
*/
public boolean isWhite() {
return pieceType == Tile.WHITE;
}
/**
* Checks if this piece is empty.
*
* @return True if this piece is empty, false otherwise.
*/
public boolean isEmpty() {
return pieceType == Tile.EMPTY;
}
/**
* Checks if this piece is opposite to another given piece.
*
* @param other The other given piece to check against.
* @return True if this piece is opposite to another given piece, false otherwise.
*/
public boolean isOppositeTo(Piece other) {
return (this.isBlack() && other.isWhite()) || (this.isWhite() && other.isBlack());
}
/**
* Gets the opposite of this piece (black becomes white and vice versa).
*
* @return The opposite of this piece (black becomes white and vice versa).
*/
public Piece getOpposite() {
if (this.isBlack()) {
return new Piece(Tile.WHITE);
}
else if (this.isWhite()) {
return new Piece(Tile.BLACK);
}
else {
return new Piece();
}
}
/**
* Returns a string representation of this object.
*
* @return A string representation of this object.
*/
public String toString() {
switch (pieceType) {
case BLACK:
return " o ";
case WHITE:
return " @ ";
default:
return " ";
}
}
/**
* Returns a hash code value for the object.
*
* @return A hash code value for the object.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pieceType == null) ? 0 : pieceType.hashCode());
return result;
}
/**
* Indicates whether some other object is "equal to" this one.
*
* @param obj The reference object with which to compare.
* @return True if this object is the same as the obj argument; false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Piece other = (Piece) obj;
if (pieceType != other.pieceType)
return false;
return true;
}
}