-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
226 lines (182 loc) · 6.12 KB
/
Copy pathscript.js
File metadata and controls
226 lines (182 loc) · 6.12 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
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
// --- WORKER SETUP ---
const AIWorker = new Worker('worker.js');
AIWorker.onmessage = function(e) {
const bestMove = e.data;
if(bestMove) {
game.move(bestMove);
renderBoard();
checkGameStatus();
}
};
// -------DOM elements------
const boardElement = document.getElementById('board');
const statusElement = document.getElementById('status');
const resetBtn = document.getElementById('resetBtn');
const modeModal = document.getElementById('mode-modal');
const difficultyModal = document.getElementById('difficulty-modal');
const colorModal = document.getElementById('color-modal');
const btnPlayer = document.getElementById('btn-player');
const btnComputer = document.getElementById('btn-computer');
const btnEasy = document.getElementById('btn-easy');
const btnMedium = document.getElementById('btn-medium');
const btnHard = document.getElementById('btn-hard');
const btnWhite = document.getElementById('btn-white');
const btnBlack = document.getElementById('btn-black');
// --- GAME STATE ---
var game = new Chess();
let gameMode = null;
let difficulty = null;
let playerSide = 'w';
let selectedSquare = null;
const pieceIcons = {
'w': { 'p': '♙', 'n': '♘', 'b': '♗', 'r': '♖', 'q': '♕', 'k': '♔' },
'b': { 'p': '♟', 'n': '♞', 'b': '♝', 'r': '♜', 'q': '♛', 'k': '♚' }
};
// --- EVENT LISTENERS ---
btnPlayer.addEventListener('click', () => {
gameMode = 'pvp';
playerSide = 'w';
closeModal(modeModal);
startGame();
});
btnComputer.addEventListener('click', () => {
gameMode ='pvc';
closeModal(modeModal);
openModal(difficultyModal);
});
btnEasy.addEventListener('click', () => selectDifficulty(1));
btnMedium.addEventListener('click', () => selectDifficulty(2));
btnHard.addEventListener('click', () => selectDifficulty(3));
btnWhite.addEventListener('click', () => {
playerSide = 'w';
closeModal(colorModal);
startGame();
});
btnBlack.addEventListener('click', () => {
playerSide = 'b';
closeModal(colorModal);
startGame();
});
resetBtn.addEventListener('click', () => {
location.reload();
});
// --- CORE FUNCTIONS ---
function openModal(modal) { modal.classList.remove('hidden'); }
function closeModal(modal) { modal.classList.add('hidden'); }
function selectDifficulty(level) {
difficulty = level;
closeModal(difficultyModal);
openModal(colorModal);
}
function startGame() {
game.reset();
boardElement.classList.add('active');
if (playerSide === 'b') {
boardElement.classList.add('flipped');
setTimeout(makeComputerMove, 500);
} else {
boardElement.classList.remove('flipped');
}
renderBoard();
}
function renderBoard() {
boardElement.innerHTML = '';
const boardData = game.board();
if (gameMode === 'pvc' && game.turn() !== playerSide) {
boardElement.style.pointerEvents = 'none';
} else {
boardElement.style.pointerEvents = 'auto';
}
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.classList.add('square');
if ((row + col) % 2 === 0) square.classList.add('light');
else square.classList.add('dark');
const piece = boardData[row][col];
if (piece) {
square.innerText = pieceIcons[piece.color][piece.type];
square.dataset.color = piece.color;
}
square.addEventListener('click', () => handleSquareClick(row, col));
const squareId = getSquareId(row, col);
if(selectedSquare === squareId) square.classList.add('selected');
boardElement.appendChild(square);
}
}
updateStatus();
}
function handleSquareClick(row, col) {
if(gameMode === 'pvc' && game.turn() !== playerSide) return;
const clickedSqaureId = getSquareId(row, col);
if(!selectedSquare) {
const piece = game.get(clickedSqaureId);
if(piece && piece.color === game.turn()) {
selectedSquare = clickedSqaureId;
renderBoard();
}
return;
}
const move = game.move({
from: selectedSquare,
to: clickedSqaureId,
promotion: 'q'
});
if(move) {
selectedSquare = null;
renderBoard();
const isGameOver = checkGameStatus();
if(!isGameOver && gameMode === 'pvc') {
setTimeout(makeComputerMove, 250);
}
} else {
const piece = game.get(clickedSqaureId);
if(piece && piece.color === game.turn()) {
selectedSquare = clickedSqaureId;
renderBoard();
} else {
selectedSquare = null;
renderBoard();
}
}
}
function makeComputerMove() {
if(difficulty === 1) makeRandomMove();
else {
const depth = difficulty === 2 ? 2 : 3;
AIWorker.postMessage({
fen : game.fen(),
depth: depth,
color: game.turn()
});
}
}
function makeRandomMove() {
const possibleMoves = game.moves();
if(possibleMoves.length == 0) return;
const randomIndex = Math.floor(Math.random() * possibleMoves.length);
game.move(possibleMoves[randomIndex]);
renderBoard();
checkGameStatus();
}
function checkGameStatus() {
if (game.game_over()) {
let msg = '';
if (game.in_checkmate()) msg = 'Game Over: Checkmate!';
else if (game.in_draw()) msg = 'Game Over: Draw!';
else msg = 'Game Over!';
statusElement.innerText = msg;
setTimeout(() => alert(msg), 500);
return true;
}
return false;
}
function updateStatus() {
if(game.game_over()) return;
statusElement.innerText = (game.turn() === 'w') ? "White's Turn" : "Black's Turn";
}
function getSquareId(row, col) {
const cols = 'abcdefgh';
const rows = '87654321';
return cols[col] + rows[row];
}