-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
106 lines (92 loc) · 3.58 KB
/
script.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
// Wait for the DOM to be fully loaded before executing code
document.addEventListener('DOMContentLoaded', () => {
let board = null; // Initialize the chessboard
const game = new Chess(); // Create new Chess.js game instance
const moveHistory = document.getElementById('move-history'); // Get move history container
let moveCount = 1; // Initialize the move count
let userColor = 'w'; // Initialize the user's color as white
// Function to make a random move for the computer
const makeRandomMove = () => {
const possibleMoves = game.moves();
if (game.game_over()) {
alert("Checkmate!");
} else {
const randomIdx = Math.floor(Math.random() * possibleMoves.length);
const move = possibleMoves[randomIdx];
game.move(move);
board.position(game.fen());
recordMove(move, moveCount); // Record and display the move with move count
moveCount++; // Increament the move count
}
};
// Function to record and display a move in the move history
const recordMove = (move, count) => {
const formattedMove = count % 2 === 1 ? `${Math.ceil(count / 2)}. ${move}` : `${move} -`;
moveHistory.textContent += formattedMove + ' ';
moveHistory.scrollTop = moveHistory.scrollHeight; // Auto-scroll to the latest move
};
// Function to handle the start of a drag position
const onDragStart = (source, piece) => {
// Allow the user to drag only their own pieces based on color
return !game.game_over() && piece.search(userColor) === 0;
};
// Function to handle a piece drop on the board
const onDrop = (source, target) => {
const move = game.move({
from: source,
to: target,
promotion: 'q',
});
if (move === null) return 'snapback';
window.setTimeout(makeRandomMove, 250);
recordMove(move.san, moveCount); // Record and display the move with move count
moveCount++;
};
// Function to handle the end of a piece snap animation
const onSnapEnd = () => {
board.position(game.fen());
};
// Configuration options for the chessboard
const boardConfig = {
showNotation: true,
draggable: true,
position: 'start',
onDragStart,
onDrop,
onSnapEnd,
moveSpeed: 'fast',
snapBackSpeed: 500,
snapSpeed: 100,
};
// Initialize the chessboard
board = Chessboard('board', boardConfig);
// Event listener for the "Play Again" button
document.querySelector('.play-again').addEventListener('click', () => {
game.reset();
board.start();
moveHistory.textContent = '';
moveCount = 1;
userColor = 'w';
});
// Event listener for the "Set Position" button
document.querySelector('.set-pos').addEventListener('click', () => {
const fen = prompt("Enter the FEN notation for the desired position!");
if (fen !== null) {
if (game.load(fen)) {
board.position(fen);
moveHistory.textContent = '';
moveCount = 1;
userColor = 'w';
} else {
alert("Invalid FEN notation. Please try again.");
}
}
});
// Event listener for the "Flip Board" button
document.querySelector('.flip-board').addEventListener('click', () => {
board.flip();
makeRandomMove();
// Toggle user's color after flipping the board
userColor = userColor === 'w' ? 'b' : 'w';
});
});