-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
215 lines (199 loc) · 7.65 KB
/
board.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
let gotMoves = false
let possableMoves = []
let holdingPiece;
class Board {
constructor(matrix) {
this.matrix = []
if (matrix instanceof Array) {
for (let row=0; row<matrix.length; row++) {
let arr = []
for (let col=0; col<matrix.length; col++) {
let piece = matrix[row][col];
if (piece == null) {
arr.push(null)
} else {
arr.push(Object.assign(Object.create(Object.getPrototypeOf(piece)), piece)) // clones object insted of getting a refrence
}
}
this.matrix.push(arr)
}
this.isMain = false
}
if (typeof matrix === "boolean") {
this.isMain = matrix;
}
}
init() {
for (let i = 0; i < 8; i++) {
this.matrix.push([null, null, null, null, null, null, null, null])
}
let startingPos =
[['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if (startingPos[row][col] != '') {
let color = startingPos[row][col].split('')[0];
let type = startingPos[row][col].split('')[1]
switch (type) {
case 'p':
this.matrix[row][col] = new Pawn(color, type, row, col);
break;
case 'k':
this.matrix[row][col] = new King(color, type, row, col);
break;
case 'b':
this.matrix[row][col] = new Bishop(color, type, row, col);
break;
case 'q':
this.matrix[row][col] = new Queen(color, type, row, col);
break;
case 'r':
this.matrix[row][col] = new Rook(color, type, row, col);
break;
case 'n':
this.matrix[row][col] = new Knight(color, type, row, col);
break;
}
}
}
}
}
show() {
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if ((row + col) % 2 == 0) {
fill(255)
} else {
fill(117, 150, 84)
}
rect(SPACING * row, SPACING * col, SPACING, SPACING);
}
}
for (let row = 0; row < this.matrix.length; row++) {
for (let col = 0; col < this.matrix.length; col++) {
if (this.matrix[row][col] instanceof Piece) {
this.matrix[row][col].show()
}
}
}
if (isHoldingPiece) {
if(!gotMoves) {
holdingPiece = BOARD.matrix[holdingPieceRow][holdingPieceCol]
possableMoves = holdingPiece.getMoves(BOARD.matrix);
gotMoves = true;
}
fill(0, 50)
for (let move of possableMoves) {
circle(move[1]*SPACING+(SPACING/2), move[0]*SPACING+(SPACING/2), SPACING*.69)
}
holdingPiece.show()
} else {
gotMoves = false;
}
}
findKing(color) {
for (let row = 0; row < this.matrix.length; row++) {
for (let col = 0; col < this.matrix.length; col++) {
if (this.matrix[row][col] != null && this.matrix[row][col].color == color && this.matrix[row][col].type == 'k') {
return [row, col]
}
}
}
}
movePutsKingInCheck(piece, landingRow, landingCol) {
console.log('checking for check')
// console.log(piece, landingRow, landingCol)
this.move(piece.matrixRow, piece.matrixCol, landingRow, landingCol)
// console.log("in movePuts... ", true, JSON.stringify(BOARD.matrix))
let oppMoves = this.getOpponentMoves(piece.color);
let kingLocation = this.findKing(piece.color)
// console.log(kingLocation)
for (let i = 0; i < oppMoves.length; i++) {
if (oppMoves[i][0] == kingLocation[0] && oppMoves[i][1] == kingLocation[1]) {
console.log("opponent can play " + oppMoves[i] + " and take your king")
return true
}
}
return false;
}
getOpponentMoves(color) {
console.log('checking oppponent moves')
let moves = [];
for (let row = 0; row < this.matrix.length; row++) {
for (let col = 0; col < this.matrix.length; col++) {
let piece = this.matrix[row][col];
if (piece != null && piece.color != color) {
for(let m of piece.getMoves(this.matrix)) { // get all moves for every piece
// console.log(piece, m)
moves.push(m);
}
}
}
}
// console.log(moves)
return moves
}
canCastle(color, isKingSide) {
let moves = this.getOpponentMoves(color);
let squares = [] // holds squares the king must move thru in order to castle
if (color == 'w') {
squares.push([7, 4]);
if (isKingSide) {
squares.push([7, 5]);
squares.push([7, 6]);
} else {
squares.push([7, 3]);
squares.push([7, 2]);
}
} else {
squares.push([0, 4])
if (isKingSide) {
squares.push([0, 5]);
squares.push([0, 6]);
} else {
squares.push([0, 3]);
squares.push([0, 2]);
}
}
for (let i = 0; i < moves.length; i++) {
for (let j = 0; j < squares.length; j++) {
if (moves[i][0] == squares[j][0] && moves[i][1] == squares[j][1]) {
return false
}
}
}
return true;
}
move(startingRow, startingCol, landingRow, landingCol) {
// console.log('moveing from ', startingRow, startingCol, " to ", landingRow, landingCol)
// console.log(JSON.stringify(this.matrix))
// console.log("before move", this.isMain, JSON.stringify(this.matrix))
let piece = this.matrix[startingRow][startingCol]
console.log("moveing ", piece, ' to ', landingRow, landingCol, ' on board ', this.isMain)
piece.matrixRow = landingRow;
piece.matrixCol = landingCol;
piece.hasMoved = true;
this.matrix[startingRow][startingCol] = null;
this.matrix[landingRow][landingCol] = piece;
// console.log("after move", this.isMain, JSON.stringify(this.matrix))
}
castle(kx, ky, rx, ry, isKingSide) {
// console.log(rx, ry)
// console.log(kx, ky)
if (isKingSide) {
BOARD.move(kx, ky, kx, ky + 2);
BOARD.move(rx, ry, rx, ry - 2)
} else {
BOARD.move(kx, ky, kx, ky - 2);
BOARD.move(rx, ry, rx, ry + 3)
}
console.log('castles')
}
}