-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
138 lines (103 loc) · 3.91 KB
/
Copy pathBoard.java
File metadata and controls
138 lines (103 loc) · 3.91 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
package chess;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
public class Board {
// The board class, which will be responsible for tracking the pieces, updating and resetting if requested
private Map<String, Piece> boardLayout;
public Board() {
boardLayout = new HashMap<>();
}
public void setBeginningPositions() {
boardLayout.clear();
// Pawn setup
for (char i = 'a'; i <= 'h'; i++) {
boardLayout.put(i + "2", new Pawn(i, 2, false));
boardLayout.put(i + "7", new Pawn(i, 7, true));
}
// Rook setup
boardLayout.put("a1", new Rook('a', 1, false));
boardLayout.put("h1", new Rook('h', 1, false));
boardLayout.put("a8", new Rook('a', 8, true));
boardLayout.put("h8", new Rook('h', 8, true));
// Knight setup
boardLayout.put("b1", new Knight('b', 1, false));
boardLayout.put("g1", new Knight('g', 1, false));
boardLayout.put("b8", new Knight('b', 8, true));
boardLayout.put("g8", new Knight('g', 8, true));
// Bishop setup
boardLayout.put("c1", new Bishop('c', 1, false));
boardLayout.put("f1", new Bishop('f', 1, false));
boardLayout.put("c8", new Bishop('c', 8, true));
boardLayout.put("f8", new Bishop('f', 8, true));
// Queen setup
boardLayout.put("d1", new Queen('d', 1, false));
boardLayout.put("d8", new Queen('d', 8, true));
// King setup
boardLayout.put("e1", new King('e', 1, false));
boardLayout.put("e8", new King('e', 8, true));
}
public boolean pathClear(char startFile, int startRank, char newFile, int newRank) {
int fileDirection = Integer.compare(newFile, startFile);
int rankDirection = Integer.compare(newRank, startRank);
char netFile = (char)(startFile + fileDirection);
int netRank = startRank + rankDirection;
while (!(netFile == newFile && netRank == newRank)) {
if (!isEmpty(netFile, netRank)) return false;
netFile += fileDirection;
netRank += rankDirection;
}
return true;
}
public Piece getPiece(char file, int rank) {
return boardLayout.get(file + String.valueOf(rank));
}
public ArrayList<Piece> getAllPieces() {
return new ArrayList<>(boardLayout.values());
}
public boolean isEmpty(char file, int rank) {
return !boardLayout.containsKey(file + String.valueOf(rank));
}
public void setPiece(char file, int rank, Piece piece) {
if (piece == null) {
boardLayout.remove(file + String.valueOf(rank)); // clear the square
} else {
boardLayout.put(file + String.valueOf(rank), piece);
piece.setNewPosition(file, rank);
}
}
public Piece locateKing(Chess.Player player) {
for (Piece piece : boardLayout.values()) {
if (piece instanceof King &&
piece.isBlack == (player == Chess.Player.black)) {
return piece;
}
}
return null;
}
public Board copy() {
Board newBoard = new Board();
for (Map.Entry<String, Piece> entry : this.boardLayout.entrySet()) {
Piece piece = entry.getValue();
if (piece != null) {
Piece cloned = piece.clone();
newBoard.boardLayout.put(entry.getKey(), cloned);
}
}
return newBoard;
}
public ArrayList<ReturnPiece> returningPieces() {
ArrayList<ReturnPiece> pieces = new ArrayList<>();
for (Piece piece: boardLayout.values()) {
ReturnPiece pieceToReturn = new ReturnPiece();
pieceToReturn.pieceFile = ReturnPiece.PieceFile.valueOf(
String.valueOf(piece.getFile()).toLowerCase()
);
pieceToReturn.pieceRank = piece.getRank();
String typeStr = (piece.isBlack ? "B" : "W") + piece.getSymbol();
pieceToReturn.pieceType = ReturnPiece.PieceType.valueOf(typeStr);
pieces.add(pieceToReturn);
}
return pieces;
}
}