-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPiece.java
More file actions
41 lines (35 loc) · 1.23 KB
/
Piece.java
File metadata and controls
41 lines (35 loc) · 1.23 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
public abstract class Piece {
protected String position; // Current position of piece on board
protected boolean isWhite; // Indicate whether piece is white (true) or black (false)
protected String positionOfKing;
protected boolean hasMoved = false;
protected boolean justMoved = false;
// Constructor to initialize the piece's position and color
public Piece(String position, boolean isWhite) {
this.position = position;
this.isWhite = isWhite;
if (isWhite) {
positionOfKing = "E1";
} else {
positionOfKing = "E8";
}
}
// Each specific piece will implement this method to define its own movement rules
public abstract String isValidMove(String newPosition, Piece[][] board);
// Method to get position of piece
public String getPosition() {
return position;
}
public void setPosition(String newPosition) {
this.position = newPosition;
}
// Method to check if the piece is White
public boolean isWhite() {
return isWhite;
}
// Override toString for better output
@Override
public String toString() {
return (isWhite ? "White" : "Black") + " piece at " + position;
}
}