-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchess.js
More file actions
270 lines (224 loc) · 8.57 KB
/
Copy pathchess.js
File metadata and controls
270 lines (224 loc) · 8.57 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
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
const board = document.getElementById('board');
let pawnPosition = 48;
let enemyPosition = 9;
let queenPosition = null;
let rookPosition = null;
let kingPosition = null;
let tutorialPhase = 1;
let bishopPosition = null;
let knightPosition = null;
let blackRookPos = null;
let blackPawnPos = null;
function startTutorial() {
const boardContainer = document.getElementById('board-container');
boardContainer.classList.add('show');
setTimeout(() => {
createBoard();
}, 700);
// Scroll down to the board container
document.getElementById('board-container').scrollIntoView({ behavior: 'smooth' });
document.getElementById('startButton').style.display = 'none';
document.getElementById('resetButton').style.display = 'inline-block';
showPopup(`<strong>Hi! Welcome to Chess instructions!</strong><br><br>I'm Tetris Cat and I will be teaching you how to play!<br><br>Begin by moving the pawn forward!`);
}
function createBoard() {
board.innerHTML = '';
for (let i = 0; i < 64; i++) {
const square = document.createElement('div');
square.classList.add('square', (Math.floor(i / 8) + i % 8) % 2 === 0 ? 'light' : 'dark');
board.appendChild(square);
}
placePawn();
placeEnemy();
}
function placePawn() {
placePiece(pawnPosition, 'assets/chessPawnWhite.png', 'pawn');
}
function placeEnemy() {
placePiece(enemyPosition, 'assets/chessRookBlack.png', 'enemy');
}
function highlightMoves() {
clearHighlights();
const squares = document.querySelectorAll('.square');
const movePos = pawnPosition - 8;
if (movePos >= 0 && squares[movePos].innerHTML === '')
highlightSquare(movePos, 'pawn');
[pawnPosition - 9, pawnPosition - 7].forEach(pos => {
if (pos === enemyPosition) highlightSquare(pos, 'pawn', true);
});
}
function movePawn(newPos) {
pawnPosition = newPos;
updateBoard();
if (pawnPosition < 8) promotePawn();
}
function capturePiece(newPos) {
pawnPosition = newPos;
enemyPosition = null;
updateBoard();
showPopup('Great job! You captured the enemy rook!');
}
function promotePawn() {
tutorialPhase = 2;
queenPosition = pawnPosition;
pawnPosition = null;
enemyPosition = null;
setupCheckmateTutorial();
}
function setupCheckmateTutorial() {
queenPosition = 59;
rookPosition = 56;
kingPosition = 5;
updateCheckmateBoard();
showPopup('Let’s checkmate the black king! Move your queen or rook to trap the black king.');
}
function placePiece(pos, imgSrc, id) {
const squares = document.querySelectorAll('.square');
const piece = document.createElement('img');
piece.classList.add('piece');
piece.src = imgSrc;
squares[pos].appendChild(piece);
if (id === 'pawn') piece.onclick = highlightMoves;
else if (id === 'whiteQueen') piece.onclick = () => highlightMovesQueen(pos);
else if (id === 'whiteRook') piece.onclick = () => highlightMovesRook(pos);
else if (id === 'whiteBishop') piece.onclick = () => highlightMovesBishop(pos);
else if (id === 'whiteKnight') piece.onclick = () => highlightMovesKnight(pos);
}
function highlightSquare(pos, pieceId, isCapture = false) {
const squares = document.querySelectorAll('.square');
const dot = document.createElement('div');
dot.classList.add('highlight');
squares[pos].appendChild(dot);
dot.onclick = () => {
if (pieceId === 'pawn') isCapture ? capturePiece(pos) : movePawn(pos);
else moveCheckmatePiece(pieceId, pos, isCapture);
};
}
function moveCheckmatePiece(pieceId, newPos, isCapture) {
if (pieceId === 'queen') queenPosition = newPos;
else if (pieceId === 'rook') rookPosition = newPos;
else if (pieceId === 'bishop') bishopPosition = newPos;
else if (pieceId === 'knight') knightPosition = newPos;
// Remove captured black pieces
if (isCapture) {
if (newPos === kingPosition) {
kingPosition = null;
showPopup('Checkmate! You captured the black king!');
checkCaptureTutorialComplete(); // Check if we should move to the next tutorial phase
} else if (newPos === blackRookPos) {
blackRookPos = null;
showPopup('Nice! You captured the black rook!');
} else if (newPos === blackPawnPos) {
blackPawnPos = null;
showPopup('Good job! You captured the black pawn!');
}
}
if (tutorialPhase === 2) {
updateCheckmateBoard();
} else if (tutorialPhase === 3) {
updateCaptureBoard();
checkCaptureTutorialComplete();
}
}
function highlightMovesQueen(pos) {
highlightLinearMoves(pos, [-8, 8, -1, 1, -9, -7, 7, 9], 'queen');
}
function highlightMovesRook(pos) {
highlightLinearMoves(pos, [-8, 8, -1, 1], 'rook');
}
function highlightLinearMoves(pos, directions, pieceId) {
clearHighlights();
const squares = document.querySelectorAll('.square');
directions.forEach(dir => {
let newPos = pos;
while (true) {
let prevPos = newPos;
newPos += dir;
if (newPos < 0 || newPos >= 64) break;
// Prevent wrap-around horizontally
if (Math.abs((prevPos % 8) - (newPos % 8)) > 2) break;
if (newPos === kingPosition) {
highlightSquare(newPos, pieceId, true);
break;
}
if (squares[newPos].innerHTML === '') {
highlightSquare(newPos, pieceId);
} else {
break;
}
}
});
}
function checkCaptureTutorialComplete() {
if (blackRookPos === null && blackPawnPos === null) {
setupCaptureTutorial();
}
}
function updateBoard() {
document.querySelectorAll('.square').forEach(sq => sq.innerHTML = '');
if (pawnPosition !== null) placePawn();
if (enemyPosition !== null) placeEnemy();
}
function updateCheckmateBoard() {
document.querySelectorAll('.square').forEach(sq => sq.innerHTML = '');
if (queenPosition !== null) placePiece(queenPosition, 'assets/chessQueenWhite.png', 'whiteQueen');
if (rookPosition !== null) placePiece(rookPosition, 'assets/chessRookWhite.png', 'whiteRook');
if (kingPosition !== null) placePiece(kingPosition, 'assets/chessKingBlack.png', 'blackKing');
}
function setupCaptureTutorial() {
tutorialPhase = 3;
bishopPosition = 35;
knightPosition = 18;
blackRookPos = 42;
blackPawnPos = 25;
showPopup("Now let’s practice capturing with bishops and knights!<br><br>Click the bishop or knight to begin.");
}
function updateCaptureBoard() {
document.querySelectorAll('.square').forEach(sq => sq.innerHTML = '');
if (bishopPosition !== null) placePiece(bishopPosition, 'assets/chessBishopWhite.png', 'whiteBishop');
if (knightPosition !== null) placePiece(knightPosition, 'assets/chessKnightWhite.png', 'whiteKnight');
if (blackRookPos !== null) placePiece(blackRookPos, 'assets/chessRookBlack.png', 'blackRook');
if (blackPawnPos !== null) placePiece(blackPawnPos, 'assets/chessPawnBlack.png', 'blackPawn');
}
function highlightMovesBishop(pos) {
highlightLinearMoves(pos, [-9, -7, 7, 9], 'bishop');
}
function highlightMovesKnight(pos) {
clearHighlights();
const knightMoves = [-17, -15, -10, -6, 6, 10, 15, 17];
const squares = document.querySelectorAll('.square');
knightMoves.forEach(offset => {
const newPos = pos + offset;
if (newPos < 0 || newPos >= 64) return;
if (Math.abs((pos % 8) - (newPos % 8)) > 2) return;
if (newPos === blackRookPos || newPos === blackPawnPos) {
highlightSquare(newPos, 'knight', true);
} else if (squares[newPos].innerHTML === '') {
highlightSquare(newPos, 'knight');
}
});
}
function clearHighlights() {
document.querySelectorAll('.highlight').forEach(dot => dot.remove());
}
function resetBoard() {
pawnPosition = 48; enemyPosition = 9; queenPosition = rookPosition = kingPosition = null; tutorialPhase = 1;
createBoard();
}
function showPopup(text) {
document.getElementById('popup-text').innerHTML = text;
document.getElementById('Csoverlay').classList.add("show");
document.getElementById('CspopupBox').classList.add("show");
}
function closePopup() {
document.getElementById('Csoverlay').classList.remove("show");
document.getElementById('CspopupBox').classList.remove("show");
}
createBoard();
document.addEventListener("DOMContentLoaded", function() {
const loggedInUser = localStorage.getItem("loggedInUser");
const loginBtn = document.getElementById("login-btn");
if (loggedInUser) {
loginBtn.textContent = `User: ${loggedInUser}`;
}
});