Repository navigation
Expand file tree
/
Copy pathBoard.java
More file actions
163 lines (123 loc) · 5.07 KB
/
Copy pathBoard.java
File metadata and controls
163 lines (123 loc) · 5.07 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
152
153
154
155
156
157
158
159
160
161
162
163
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Board {
private Map<Integer, List<Square>> board;
static final int CHESS_CONSTANT = 8;
static final int WHITE_PAWN_RANK = 2;
static final int BLACK_PAWN_RANK = 7;
private static final int WHITE_MAJOR_PIECE_RANK = 1;
private static final int BLACK_MAJOR_PIECE_RANK = Board.CHESS_CONSTANT;
public static boolean whiteKingSideCastlePossible = true;
public static boolean whiteQueenSideCasltePossible = true;
public static boolean blackKingSideCastlePossible = true;
public static boolean blackQueenSideCastlePossible = true;
public Board() {
this.board = new HashMap<>();
this.initEmptyBoard();
}
public final void initBoard() {
for (int i = 1; i <= Board.CHESS_CONSTANT; i++) {
if (i == Board.WHITE_PAWN_RANK) {
this.board.put(i, this.initPawns(COLORS.WHITE));
} else if (i == Board.BLACK_PAWN_RANK) {
this.board.put(i, this.initPawns(COLORS.BLACK));
} else if (i == Board.WHITE_MAJOR_PIECE_RANK) {
this.board.put(i, this.initMinorAndMajorPieces(COLORS.WHITE));
} else if (i == Board.BLACK_MAJOR_PIECE_RANK) {
this.board.put(i, this.initMinorAndMajorPieces(COLORS.BLACK));
} else {
List<Square> rank = new ArrayList<>();
for (int j = 1; j <= Board.CHESS_CONSTANT; j++) {
rank.add(new Square());
}
this.board.put(i, rank);
}
}
}
private final List<Square> initPawns(COLORS color) {
List<Square> rank = new ArrayList<>();
for (int i = 1; i <= Board.CHESS_CONSTANT; i++) {
Square square = new Square(new Piece(PIECES.PAWN, color));
rank.add(square);
}
return rank;
}
public final void initEmptyBoard() {
for (int i = 1; i <= Board.CHESS_CONSTANT; i++) {
List<Square> rank = new ArrayList<>();
for (int j = 0; j < Board.CHESS_CONSTANT; j++) {
rank.add(new Square());
}
this.board.put(i, rank);
}
}
private final List<Square> initMinorAndMajorPieces(COLORS color) {
return List.of(
new Square(new Piece(PIECES.ROOK, color)),
new Square(new Piece(PIECES.KNIGHT, color)),
new Square(new Piece(PIECES.BISHOP, color)),
new Square(new Piece(PIECES.QUEEN, color)),
new Square(new Piece(PIECES.KING, color)),
new Square(new Piece(PIECES.BISHOP, color)),
new Square(new Piece(PIECES.KNIGHT, color)),
new Square(new Piece(PIECES.ROOK, color)));
}
public final Piece getPiece(Positon positon) {
return this.board.get(positon.getRank()).get(positon.getIndex()).getPiece();
}
public final void move(Board board, Positon from, Positon to) throws InvalidPosition, IllegalMove {
from.validate();
to.validate();
ValidateMoves.validate(board, from, to);
Piece piece = this.getPiece(from);
this.board.get(from.getRank()).get(from.getIndex()).setPiece(null);
this.board.get(to.getRank()).get(to.getIndex()).setPiece(piece);
if (piece.getName() == PIECES.PAWN) {
if (piece.getColor() == COLORS.WHITE && to.getRank() == Board.CHESS_CONSTANT) {
Utils.promotion(board, to);
} else if (piece.getColor() == COLORS.WHITE && to.getRank() == 1) {
Utils.promotion(board, to);
}
}
}
public Map<Integer, List<Square>> getMap() {
return this.board;
}
public final void print() {
System.out.println();
System.out.println();
for (int i = Board.CHESS_CONSTANT; i >= 1; i--) {
List<Square> rank = this.board.get(i);
System.out.print(i + " | ");
for (int j = 0; j < Board.CHESS_CONSTANT; j++) {
System.out.printf("%-15s", rank.get(j));
}
System.out.println();
System.out.println(" | ");
}
System.out.print(0 + " | ");
char ch = 'a';
for (int j = 0; j < Board.CHESS_CONSTANT; j++) {
System.out.printf("%-15s", " " + ch++ + " ");
}
System.out.println();
System.out.println();
}
public void place(String squareName, Piece piece) {
try {
Utils.validateSquare(squareName);
} catch (InvalidSquare e) {
System.out.println(e.getMessage());
return;
}
Positon positon = Utils.convert(squareName);
this.board.get(positon.getRank()).get(positon.getIndex()).setPiece(piece);
}
public void informalMove(Positon from, Positon to) {
Piece piece = this.getPiece(from);
this.board.get(from.getRank()).get(from.getIndex()).setPiece(null);
this.board.get(to.getRank()).get(to.getIndex()).setPiece(piece);
}
}