-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
119 lines (98 loc) · 3.08 KB
/
Copy pathBoard.java
File metadata and controls
119 lines (98 loc) · 3.08 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
/**
* Board
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A board class representing the chess board
*/
public class Board {
private Piece[][] gameBoard;
public Board() {
gameBoard = new Piece[8][8];
createBoard();
}
/**
* create the bard and include the piece on it
*/
public void createBoard() {
Piece rook = new Rook('r');
Piece knight = new Knight('n');
Piece bishop = new Bishop('b');
Piece king = new King('k');
Piece queen = new Queen('q');
Piece pawn = new Pawn('p');
gameBoard[0] = new Piece[]{rook, knight, bishop, king, queen, bishop, knight, rook};
gameBoard[1] = new Piece[]{pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn};
for (int i = 2; i <= 5; i++) {
for (int j = 0; j < gameBoard.length; j++) {
gameBoard[i][j] = null;
}
}
rook = new Rook('R');
knight = new Knight('N');
bishop = new Bishop('B');
king = new King('K');
queen = new Queen('Q');
pawn = new Pawn('P');
gameBoard[6] = new Piece[]{pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn};
gameBoard[7] = new Piece[]{rook, knight, bishop, king, queen, bishop, knight, rook};
}
/**
* get a piece on the board
*
* @param row the row of the piece needed
* @param col the col of the piece needed
* @return Piece-the piece that needs to be gotten
*/
public Piece getPiece(int row, int col) {
return gameBoard[row][col];
}
/**
* set a piece on the board
*
* @param row the row of the piece needed to be set
* @param col the col of the piece needed to be set
*/
public void setPiece(Piece piece, int row, int col) {
gameBoard[row][col] = piece;
}
/**
* update the board after a move has been made
*
* @param move the move made that needs to be updated on the board
*/
public void updateBoard(Move move) {
Piece oldPiece = gameBoard[move.getRow()][move.getCol()];
if (oldPiece.getCaptured() != null) {
gameBoard[move.getRow()][move.getCol()] = null;
} else {
gameBoard[move.getRow()][move.getCol()] = gameBoard[move.getNewRow()][move.getNewCol()];
}
gameBoard[move.getNewRow()][move.getNewCol()] = oldPiece;
}
/**
* get the game board
*
* @return Piece[][]-the game board array
*/
public Piece[][] getGameBoard() {
return gameBoard;
}
/**
* checks if a king has been captured by any piece on the board
*
* @return boolean-returns true if the king has been captured and false otherwise
*/
public boolean capturedKing() {
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard.length; j++) {
if (gameBoard[i][j] != null) {
if (gameBoard[i][j].capturedKing()) {
return true;
}
}
}
}
return false;
}
}