-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchess.js
416 lines (357 loc) · 13 KB
/
chess.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//chess.js
import { chess } from './chesslib.js'
import { PlayStockfish } from './playstockfish.js'
class ChessUI {
constructor(chessGame) {
this.chessGame = chessGame
this.selectedPiece = null
this.chessboardElement = document.getElementById('chessboard')
this.chessPieceMap = {
K: 'wK',
Q: 'wQ',
R: 'wR',
B: 'wB',
N: 'wN',
P: 'wP',
k: 'bK',
q: 'bQ',
r: 'bR',
b: 'bB',
n: 'bN',
p: 'bP',
}
this.gameOver = false // Track if the game is over
this.isFlipped = false // Track if the board is flipped
this.isBotGame = false // Track if it's a bot game
this.stockfishAI = null // Placeholder for the Stockfish AI
this.initializeBoard()
this.flipChessboard()
this.showGameModeSelection()
}
initializeBoard() {
this.renderBoard()
}
showGameModeSelection() {
const startGameButton = document.getElementById('startGame')
startGameButton.addEventListener('click', () => {
const gameModeModal = new bootstrap.Modal(
document.getElementById('gameModeModal'),
)
gameModeModal.show()
const stockfishStrengthInput =
document.getElementById('stockfishStrength')
// Update Stockfish Level when input changes
stockfishStrengthInput.addEventListener('input', () => {
this.updateStockfishLevel(stockfishStrengthInput.value)
})
document
.getElementById('playerVsPlayer')
.addEventListener('click', () => {
this.startPlayerVsPlayerGame()
this.isBotGame = false // Player vs Player
gameModeModal.hide()
})
document
.getElementById('playerVsBotWhite')
.addEventListener('click', () => {
this.startPlayerVsBotGame('w')
this.isBotGame = true // Player vs Stockfish (White)
this.updateStockfishLevel(stockfishStrengthInput.value) // Update Stockfish level
gameModeModal.hide()
})
document
.getElementById('playerVsBotBlack')
.addEventListener('click', () => {
this.startPlayerVsBotGame('b')
this.isBotGame = true // Stockfish (Black)
this.updateStockfishLevel(stockfishStrengthInput.value) // Update Stockfish level
gameModeModal.hide()
})
})
}
updateStockfishLevel(level) {
if (this.stockfishAI) {
this.stockfishAI.stockfishLevel = level // Update the level in PlayStockfish
this.stockfishAI.stockfish.postMessage(
`setoption name Skill Level value ${level}`,
) // Optionally, post the new level to Stockfish
}
}
startPlayerVsPlayerGame() {
this.isBotGame = false // Set to false for player vs player
console.log('Starting Player vs Player Game...')
// Additional logic to reset board and start the game can go here.
}
// Update the existing startPlayerVsBotGame method
startPlayerVsBotGame(playerColor) {
console.log(`Starting Player vs Bot Game... Player: ${playerColor}`)
this.stockfishAI = new PlayStockfish(this.chessGame, this) // Pass the ChessUI instance
if (playerColor === 'b') {
this.stockfishAI.requestStockfishMove() // If the player is black, request the first move for Stockfish.
}
}
// Method called after player's move to request Stockfish's move
stockfishMove() {
if (this.isBotGame && this.chessGame.currentPlayerTurn === 'b') {
this.stockfishAI.onPlayerMove(this.gameOver)
} else if (this.isBotGame && this.chessGame.currentPlayerTurn === 'w') {
this.stockfishAI.onPlayerMove(this.gameOver)
}
}
flipChessboard() {
const flipButton = document.getElementById('flipBoard')
flipButton.addEventListener('click', () => {
this.isFlipped = !this.isFlipped
this.renderBoard()
})
}
renderBoard() {
this.chessboardElement.innerHTML = '' // Clear previous board
for (let row = 0; row < 8; row++) {
for (let column = 0; column < 8; column++) {
const renderedRow = this.isFlipped ? 7 - row : row
const renderedColumn = this.isFlipped ? 7 - column : column
this.createSquare(renderedRow, renderedColumn)
}
}
}
createSquare(row, column) {
const squareElement = document.createElement('div')
squareElement.classList.add(
'square',
(row + column) % 2 === 0 ? 'white' : 'black',
)
squareElement.dataset.row = row
squareElement.dataset.col = column
// Create a text node to display the row, column
const coordText = document.createElement('span')
coordText.classList.add('coords')
coordText.textContent = `(${row}, ${column})`
squareElement.appendChild(coordText)
const chessPiece = this.chessGame.board[row][column]
if (chessPiece) {
this.createPieceElement(squareElement, chessPiece, row, column)
}
squareElement.addEventListener('click', () => this.movePiece(row, column))
this.chessboardElement.appendChild(squareElement)
}
createPieceElement(squareElement, piece, row, column) {
const pieceImageElement = document.createElement('img')
pieceImageElement.src = `images/${this.chessPieceMap[piece]}.svg`
pieceImageElement.alt = piece
pieceImageElement.classList.add('piece')
pieceImageElement.addEventListener('click', (event) => {
event.stopPropagation()
this.handlePieceClick(row, column)
})
squareElement.appendChild(pieceImageElement)
}
handlePieceClick(row, column) {
const piece = this.chessGame.board[row][column]
if (this.isSelectedPiece(row, column)) {
this.deselectPiece()
return
}
if (this.isValidPieceSelection(piece)) {
this.selectPiece(row, column)
}
}
isSelectedPiece(row, column) {
return (
this.selectedPiece &&
this.selectedPiece[0] === row &&
this.selectedPiece[1] === column
)
}
isValidPieceSelection(piece) {
if (!piece) return false // No piece in the square
const pieceColor = piece === piece.toUpperCase() ? 'w' : 'b' // Determine the color of the piece
return pieceColor === this.chessGame.currentPlayerTurn // Return true only if it's the correct player's turn
}
selectPiece(row, column) {
if (this.selectedPiece) {
this.deselectPiece() // Deselect if already selected
}
this.selectedPiece = [row, column]
this.highlightSelectedPiece(row, column)
this.highlightValidMoves(row, column)
}
deselectPiece() {
if (this.selectedPiece) {
const [row, column] = this.selectedPiece
this.updateSquareHighlight(row, column, false)
}
this.selectedPiece = null
this.clearHighlights()
}
highlightSelectedPiece(row, column) {
this.updateSquareHighlight(row, column, true)
}
updateSquareHighlight(row, column, isHighlighted) {
const selectedSquare = this.chessboardElement.querySelector(
`.square[data-row='${row}'][data-col='${column}']`,
)
selectedSquare.classList.toggle('selected-square', isHighlighted)
}
highlightValidMoves(startX, startY) {
const validMoves = this.chessGame.getValidMoves([startX, startY])
validMoves.forEach(([endX, endY]) => {
const square = this.chessboardElement.querySelector(
`.square[data-row='${endX}'][data-col='${endY}']`,
)
this.createHighlightCircle(square)
})
}
createHighlightCircle(square) {
const highlightCircle = document.createElement('div')
highlightCircle.classList.add('highlight-circle')
square.appendChild(highlightCircle)
}
clearHighlights() {
const highlightedCircles =
this.chessboardElement.querySelectorAll('.highlight-circle')
highlightedCircles.forEach((circle) => circle.remove())
}
movePiece(row, column) {
if (!this.selectedPiece || this.gameOver) return // No piece selected or game is over
const [startX, startY] = this.selectedPiece
// Check for castling
if (this.chessGame.isCastlingMove([startX, startY], [row, column])) {
try {
this.chessGame.castle([startX, startY], [row, column])
this.renderBoard() // Refresh the board
this.deselectPiece()
this.stockfishMove() // Request Stockfish move if it's a bot game
return
} catch (error) {
alert(error.message) // Notify user about the invalid castling
console.log(error)
this.deselectPiece() // Deselect the piece
return
}
}
const isValidMove = this.chessGame.isValidMove(
[startX, startY],
[row, column],
)
if (isValidMove) {
try {
// Check for draw by threefold repetition before executing the move
if (this.chessGame.isDrawByRepetition === true) {
setTimeout(() => {
alert('Draw by 3-fold repetition!')
}, 350) // 1000 milliseconds = 1 seconds
this.gameOver = true
return
}
// Check if move leads to a draw by the fifty-move rule
if (this.chessGame.isDrawByFiftyMoveRule()) {
setTimeout(() => {
alert('Draw by the fifty-move rule!')
}, 350)
this.gameOver = true
return
}
// Check for en-passant
if (
this.chessGame.enPassantTarget &&
this.chessGame.enPassantTarget[0] === row &&
this.chessGame.enPassantTarget[1] === column
) {
const capturedRow = startX
const capturedCol = column
// Remove the pawn from the board at en-passant target
this.chessGame.board[capturedRow][capturedCol] = null
}
this.executeMove(startX, startY, row, column)
this.stockfishMove() // Request Stockfish move if it's a bot game
// Check for checkmate after the move
if (this.chessGame.isCheckmate()) {
setTimeout(() => {
alert(
`${
this.chessGame.currentPlayerTurn === 'w' ? 'Black' : 'White'
} wins by checkmate!`,
)
}, 350)
this.gameOver = true
return
}
// Check for stalemate after the move
if (this.chessGame.isStalemate()) {
setTimeout(() => {
alert('Draw by Stalemate!')
}, 350)
this.gameOver = true
return
}
// Check for insufficient material after the move
if (this.chessGame.isDrawByInsufficientMaterial()) {
setTimeout(() => {
alert('Draw by insufficient material!')
}, 350)
this.gameOver = true
return
}
} catch (error) {
alert(error.message) // Notify user about the invalid move
console.log(error)
this.deselectPiece()
}
} else {
this.deselectPiece()
}
}
animateMove(pieceSquare, targetSquare) {
// Get the bounding rectangles for the start and target positions
const targetRect = targetSquare.getBoundingClientRect()
const startRect = pieceSquare.getBoundingClientRect()
// Temporarily remove the piece from the DOM
pieceSquare.parentElement.removeChild(pieceSquare)
// Append the piece to the target square to set initial position
targetSquare.appendChild(pieceSquare)
// Set the initial position using transforms
pieceSquare.style.position = 'absolute' // Position the piece to allow transforms
// Position the piece at the starting square for animation
pieceSquare.style.left = `${startRect.left - targetRect.left}px`
pieceSquare.style.top = `${startRect.top - targetRect.top}px`
pieceSquare.style.zIndex = 10 // Bring piece on top of everything
// Apply the translation to move the piece to the target square
requestAnimationFrame(() => {
pieceSquare.style.transition = 'transform 0.3s ease-in-out'
pieceSquare.style.transform = `translate(${
targetRect.left - startRect.left
}px, ${targetRect.top - startRect.top}px)`
})
// Reset the piece properties after the animation completes
setTimeout(() => {
// Reset the piece properties after the animation
pieceSquare.style.position = '' // Restore position
pieceSquare.style.zIndex = '' // Reset z-index
pieceSquare.style.transition = '' // Reset transition
pieceSquare.style.transform = '' // Reset transform
this.deselectPiece()
this.renderBoard()
}, 300) // Wait for the animation duration before executing the move
}
executeMove(startX, startY, row, column) {
const pieceSquare = this.chessboardElement.querySelector(
`.square[data-row='${startX}'][data-col='${startY}'] .piece`,
)
// Calculate the target square's position
const targetSquare = this.chessboardElement.querySelector(
`.square[data-row='${row}'][data-col='${column}']`,
)
// Ensure that we already have the piece to move, and not create duplicates
if (!pieceSquare) {
console.error('No piece found at the starting square.')
return
}
// Call the animateMove method to handle the animation
this.animateMove(pieceSquare, targetSquare)
// Move the piece in the chessGame model
this.chessGame.makeMove([startX, startY], [row, column])
}
}
// Initialize the Chess UI
const chessUI = new ChessUI(chess)