-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.java
More file actions
68 lines (58 loc) · 2.45 KB
/
Copy pathChessBoard.java
File metadata and controls
68 lines (58 loc) · 2.45 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
public class ChessBoard {
private Piece[][] board;
public ChessBoard() {
board = new Piece[8][8];
setupPieces();
}
private void setupPieces() {
// Place Rooks.
board[0][0] = new Rook(PieceColor.BLACK, new Position(0, 0));
board[0][7] = new Rook(PieceColor.BLACK, new Position(0, 7));
board[7][0] = new Rook(PieceColor.WHITE, new Position(7, 0));
board[7][7] = new Rook(PieceColor.WHITE, new Position(7, 7));
// Place Knights.
board[0][1] = new Knight(PieceColor.BLACK, new Position(0, 1));
board[0][6] = new Knight(PieceColor.BLACK, new Position(0, 6));
board[7][1] = new Knight(PieceColor.WHITE, new Position(7, 1));
board[7][6] = new Knight(PieceColor.WHITE, new Position(7, 6));
// Place Bishops.
board[0][2] = new Bishop(PieceColor.BLACK, new Position(0, 2));
board[0][5] = new Bishop(PieceColor.BLACK, new Position(0, 5));
board[7][2] = new Bishop(PieceColor.WHITE, new Position(7, 2));
board[7][5] = new Bishop(PieceColor.WHITE, new Position(7, 5));
// Place Queens.
board[0][3] = new Queen(PieceColor.BLACK, new Position(0, 3));
board[7][3] = new Queen(PieceColor.WHITE, new Position(7, 3));
// Place Kings.
board[0][4] = new King(PieceColor.BLACK, new Position(0, 4));
board[7][4] = new King(PieceColor.WHITE, new Position(7, 4));
// Place Pawns.
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(PieceColor.BLACK, new Position(1, i));
board[6][i] = new Pawn(PieceColor.WHITE, new Position(6, i));
}
}
public boolean movePiece(Position start, Position end) {
Piece piece = board[start.getRow()][start.getCol()];
if (piece != null && piece.isValidMove(end, board)) {
// Move the piece.
board[end.getRow()][end.getCol()] = piece;
piece.setPosition(end);
board[start.getRow()][start.getCol()] = null;
return true;
}
return false;
}
public Piece[][] getBoard() {
return board;
}
public Piece getPiece(int row, int col) {
return board[row][col];
}
public void setPiece(int row, int col, Piece piece) {
board[row][col] = piece;
if(piece != null) {
piece.setPosition(new Position(row, col));
}
}
}