-
Notifications
You must be signed in to change notification settings - Fork 0
/
chscript.js
652 lines (568 loc) · 18.7 KB
/
chscript.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"use strict";
////////////////////////
// Selectors
const boardContainerEl = document.querySelector(".game__board--container");
const boardEl = document.querySelector(".game__board");
const gameFieldEl = document.getElementsByClassName("game__field");
const root = document.documentElement;
const beatenBoardWhiteEl = document.getElementById("beaten--0");
const beatenBoardBlackEl = document.getElementById("beaten--1");
const beatenFieldsEl = document.getElementsByClassName("beaten-pieces");
////////////////////////
const boardCoords = [];
const allPiecesCoords = [new Map(), new Map()];
const beatenFields = [[], []];
const piecesOnBeaten = [new Map(), new Map()];
const players = [0, 1];
let activePlayer = players[0];
let rivalPlayer = activePlayer === 0 ? 1 : 0;
let selectedPiece;
////////////////////////
// Classes Chess Pieces
const chessPiecesArr = [];
class ChessPiece {
hasBeenMoved = false;
constructor(namestring) {
this.namestring = namestring;
this.curCoords = this.namestring.split("-").slice(-2).join("-"); // CR: code smell , besser direkt initialisierren im konstruktor
this.imgName = this.namestring.split("-").at(0); // CR: dito
chessPiecesArr.push(this); // CR: würde ich eventuell in eine klasse Board auslagern
}
checkIfInitial() {
return this.namestring.slice(-3) === this.curCoords;
}
getPossibleMovs(playerType = "active") {
const player = playerType === "active" ? activePlayer : rivalPlayer;
const coordArr = this.curCoords.split("-").map((num) => Number(num));
const moveset = this.moveset();
return (this.possibleMovs = moveset
.map((arr) => arr.map((val, i) => val + coordArr[i]))
.map((arr) => arr.join("-"))
.filter((arr) => boardCoords.includes(arr))
.filter((arr) => !allPiecesCoords[player].has(arr)));
}
// needs to be fully implemented
// animateMovement(img, coords) {
// const curCoordsArr = this.curCoords.split("-").map((num) => Number(num));
// const destCoordsArr = coords.split("-").map((num) => Number(num));
// const xy = curCoordsArr.map((num, i) => (num + destCoordsArr[i]) * 11);
// root.style.setProperty("--move-toX", `${xy[0]}vh`);
// root.style.setProperty("--move-toY", `${xy[1]}vh`);
// img.classList.add("game__piece--moving");
// console.log(img);
// }
updateAllPiecesCoords(coords) {
allPiecesCoords[activePlayer].delete(this.curCoords);
this.curCoords = coords;
allPiecesCoords[activePlayer].set(this.curCoords, this.namestring);
}
movePiece(coords) {
const currentImg = document.getElementById(`img-${this.curCoords}`);
// this.animateMovement(currentImg, coords);
currentImg.remove();
if (allPiecesCoords[rivalPlayer].has(coords))
ChessPiece.removeBeatenPiece(coords);
placeImg(coords, this);
this.updateAllPiecesCoords(coords);
if (this instanceof Pawn) {
this.changePieceIfLastRow();
} else if (this instanceof King && !this.hasBeenMoved) {
this.determineCastling();
} else {
if (!this.hasBeenMoved) this.hasBeenMoved = true;
endMove();
}
}
static removeBeatenPiece(coords) {
const rivalPiece = getPlayersPiece(coords, "rival");
const currentImg = document.getElementById(`img-${rivalPiece.curCoords}`);
currentImg.remove();
allPiecesCoords[rivalPlayer].delete(coords);
const beatenCoords = beatenFields[activePlayer].pop();
rivalPiece.curCoords = beatenCoords;
piecesOnBeaten[rivalPlayer].set(beatenCoords, rivalPiece.namestring);
placeImg(beatenCoords, rivalPiece);
if (rivalPiece instanceof King) winGame(activePlayer);
}
getDiagonal(player, direction) {
return this.curCoords
.split("-")
.map((num) => Number(num))
.map((num, i) => {
if (i === 0) return direction === "left" ? num - 1 : num + 1;
else return player === 0 ? num - 1 : num + 1;
})
.join("-");
}
getFront(player) {
return this.curCoords
.split("-")
.map((num) => Number(num))
.map((num, i) => {
if (i === 0) return num;
else return player === 0 ? num - 1 : num + 1;
})
.join("-");
}
}
class King extends ChessPiece {
moveArr = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
[1, 1],
[-1, 1],
[1, -1],
[-1, -1],
];
getKingsColor() {
return activePlayer === 0 ? "w" : "b";
}
getKingsRow() {
return activePlayer === 0 ? "7" : "0";
}
determineCastling() {
const isKingSide = this.curCoords.at(0) === "6";
if (isKingSide || this.curCoords.at(0) === "2") {
const castlingRook = isKingSide
? chessPiecesArr.find((obj) =>
obj.namestring.startsWith(
`rook_${this.getKingsColor()}-7-${this.getKingsRow()}`
)
)
: chessPiecesArr.find((obj) =>
obj.namestring.startsWith(
`rook_${this.getKingsColor()}-0-${this.getKingsRow()}`
)
);
const moveCoordsRook = isKingSide
? `5-${this.getKingsRow()}`
: `3-${this.getKingsRow()}`;
castlingRook.movePiece(moveCoordsRook);
} else {
this.hasBeenMoved = true;
endMove();
}
}
checkSideforCastling(rook) {
if (rook.hasBeenMoved) return [];
const isKingSide = rook.namestring.at(7) === "7" ? true : false;
const coordsToCheck = isKingSide
? [`5-${rook.namestring.at(-1)}`, `6-${rook.namestring.at(-1)}`]
: [
`1-${rook.namestring.at(-1)}`,
`2-${rook.namestring.at(-1)}`,
`3-${rook.namestring.at(-1)}`,
];
const pieceInTheWay = coordsToCheck.some(
(coords) =>
allPiecesCoords[activePlayer].has(coords) ||
allPiecesCoords[rivalPlayer].has(coords)
);
if (pieceInTheWay) return [];
return isKingSide ? [2, 0] : [-2, 0];
}
checkForCastling() {
if (this.hasBeenMoved) return [...this.moveArr];
const rookKingSide = chessPiecesArr.find((obj) =>
obj.namestring.startsWith(
`rook_${this.getKingsColor()}-7-${this.getKingsRow()}`
)
);
const rookQueenSide = chessPiecesArr.find((obj) =>
obj.namestring.startsWith(
`rook_${this.getKingsColor()}-0-${this.getKingsRow()}`
)
);
return [
...this.moveArr,
this.checkSideforCastling(rookKingSide),
this.checkSideforCastling(rookQueenSide),
];
}
checkIfPermitted(moveArr) {
const moveCopy = [...moveArr];
const coordArr = this.curCoords.split("-").map((num) => Number(num));
const curCoordKingTempCopy = this.curCoords;
const rivalArr = Array.from(allPiecesCoords[rivalPlayer].keys());
const allRivalPieces = rivalArr
.map((coords) => getPlayersPiece(coords, "rival"))
.filter((piece) => !piece.imgName.startsWith("king"));
const rivalKing = chessPiecesArr.find((obj) =>
obj.namestring.startsWith(`king_${rivalPlayer === 0 ? "w" : "b"}`)
);
const rivalKingCurCoords = rivalKing.curCoords
.split("-")
.map((num) => Number(num));
const rivalKingMoves = rivalKing.moveArr
.map((arr) => arr.map((val, i) => val + rivalKingCurCoords[i]))
.map((arr) => arr.join("-"))
.filter((arr) => boardCoords.includes(arr))
.filter((arr) => !allPiecesCoords[rivalPlayer].has(arr));
const kingMoveMap = new Map();
for (const move of moveCopy) {
const curKingMoves = move.map((val, i) => val + coordArr[i]).join("-");
kingMoveMap.set(curKingMoves, move);
}
this.updateAllPiecesCoords("none");
for (const [cur, mov] of kingMoveMap.entries()) {
if (allPiecesCoords[activePlayer].has(cur)) {
kingMoveMap.delete(cur);
continue;
}
let curCoordsRivalTempCopy;
if (allPiecesCoords[rivalPlayer].has(cur)) {
curCoordsRivalTempCopy = [cur, allPiecesCoords[rivalPlayer].get(cur)];
allPiecesCoords[rivalPlayer].delete(cur);
}
let allRivalMoves = allRivalPieces.map((piece) =>
piece instanceof Pawn
? [
piece.getDiagonal(rivalPlayer, "left"),
piece.getDiagonal(rivalPlayer, "right"),
]
: piece.getPossibleMovs("rival")
);
allRivalMoves = allRivalMoves.concat(rivalKingMoves);
allRivalMoves = allRivalMoves.flat();
if (allRivalMoves.includes(cur)) kingMoveMap.delete(cur);
if (curCoordsRivalTempCopy)
allPiecesCoords[rivalPlayer].set(
curCoordsRivalTempCopy[0],
curCoordsRivalTempCopy[1]
);
}
this.updateAllPiecesCoords(curCoordKingTempCopy);
const checkedMoveArr = Array.from(kingMoveMap.values());
if (!checkedMoveArr.includes([1, 0] && checkedMoveArr.includes[(2, 0)]))
delete checkedMoveArr.indexOf([2, 0]);
if (!checkedMoveArr.includes([-1, 0] && checkedMoveArr.includes[(-2, 0)]))
delete checkedMoveArr.indexOf([-2, 0]);
return Array.from(kingMoveMap.values());
}
moveset = function () {
const castling = this.checkForCastling();
const checkedMoves = this.checkIfPermitted([...this.moveArr, ...castling]);
return checkedMoves;
};
}
class Queen extends ChessPiece {
adjustMoves(moveArr) {
const moveCopy = [...moveArr];
const coordArr = this.curCoords.split("-").map((num) => Number(num));
let previousMovCoords = this.curCoords;
for (let i = 0; i < moveCopy.length; i++) {
const movCoords = moveCopy[i]
.map((val, i) => val + coordArr[i])
.join("-");
if (
boardCoords.includes(movCoords) &&
!allPiecesCoords[activePlayer].has(movCoords)
) {
previousMovCoords = movCoords;
if (allPiecesCoords[rivalPlayer].has(previousMovCoords)) continue;
moveCopy.push(
moveCopy[i].map((val) => {
if (val > 0) return val + 1;
else if (val < 0) return val - 1;
else return 0;
})
);
}
}
return moveCopy;
}
moveset = function () {
const moveArr = [
[1, 1],
[1, -1],
[-1, 1],
[-1, -1],
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const adjustedMoves = this.adjustMoves(moveArr);
return adjustedMoves;
};
}
class Rook extends Queen {
moveset = function () {
const moveArr = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const adjustedMoves = this.adjustMoves(moveArr);
return adjustedMoves;
};
}
class Bishop extends Queen {
moveset = function () {
// CR: hier passiert shadowing der base class member variable, das ist etwas unschön
const moveArr = [
[1, 1],
[1, -1],
[-1, 1],
[-1, -1],
];
const adjustedMoves = this.adjustMoves(moveArr);
return adjustedMoves;
};
}
class Knight extends ChessPiece {
moveset = function () {
return [
[-1, -2],
[1, -2],
[-1, 2],
[1, 2],
[-2, -1],
[-2, 1],
[2, -1],
[2, 1],
];
};
}
class Pawn extends ChessPiece {
buildPawnMovements(player) {
const moveArr = [player === 0 ? [0, -1] : [0, 1]];
if (
this.checkIfInitial() &&
!allPiecesCoords[rivalPlayer].has(this.getFront(player))
) {
moveArr.push(player === 0 ? [0, -2] : [0, 2]);
}
if (allPiecesCoords[rivalPlayer].has(this.getDiagonal(player, "left"))) {
moveArr.push(player === 0 ? [-1, -1] : [-1, 1]);
}
if (allPiecesCoords[rivalPlayer].has(this.getDiagonal(player, "right"))) {
moveArr.push(player === 0 ? [1, -1] : [1, 1]);
}
if (allPiecesCoords[rivalPlayer].has(this.getFront(player))) {
moveArr.shift();
}
return moveArr;
}
moveset() {
const moveArr = this.buildPawnMovements(activePlayer);
return moveArr;
}
changePieceIfLastRow() {
const thisPawn = this;
const replacePawn = function (e, thisPawn) {
const clickedPiece = chessPiecesArr.find(
(obj) =>
obj.namestring ===
piecesOnBeaten[activePlayer].get(e.target.dataset["coords"])
);
const thisPawnImg = document.getElementById(`img-${thisPawn.curCoords}`);
const clickedPieceImg = document.getElementById(
`img-${clickedPiece.curCoords}`
);
thisPawnImg.remove();
clickedPieceImg.remove();
allPiecesCoords[activePlayer].delete(thisPawn.curCoords);
[thisPawn.curCoords, clickedPiece.curCoords] = [
clickedPiece.curCoords,
thisPawn.curCoords,
];
allPiecesCoords[activePlayer].set(
clickedPiece.curCoords,
clickedPiece.namestring
);
piecesOnBeaten[rivalPlayer].set(thisPawn.curCoords, thisPawn.namestring);
placeImg(thisPawn.curCoords, thisPawn);
placeImg(clickedPiece.curCoords, clickedPiece);
document
.getElementById(`beaten--${rivalPlayer}`)
.removeEventListener("click", function (e) {
replacePawn(e, thisPawn);
});
endMove();
};
const condition = activePlayer === 0 ? "0" : "7";
if (this.curCoords.at(-1) === condition) {
document
.getElementById(`beaten--${rivalPlayer}`)
.addEventListener("click", function (e) {
replacePawn(e, thisPawn);
});
} else {
endMove();
}
}
}
const kingW = new King("king_w-4-7");
const kingB = new King("king_b-4-0");
const queenW = new Queen("queen_w-3-7");
const queenB = new Queen("queen_b-3-0");
const rookW07 = new Rook("rook_w-0-7");
const rookW77 = new Rook("rook_w-7-7");
const rookB00 = new Rook("rook_b-0-0");
const rookB70 = new Rook("rook_b-7-0");
const bishopW27 = new Bishop("bishop_w-2-7");
const bishopW57 = new Bishop("bishop_w-5-7");
const bishopB20 = new Bishop("bishop_b-2-0");
const bishopB50 = new Bishop("bishop_b-5-0");
const knightW17 = new Knight("knight_w-1-7");
const knightW67 = new Knight("knight_w-6-7");
const knightB10 = new Knight("knight_b-1-0");
const knightB60 = new Knight("knight_b-6-0");
const pawnW06 = new Pawn("pawn_w-0-6");
const pawnW16 = new Pawn("pawn_w-1-6");
const pawnW26 = new Pawn("pawn_w-2-6");
const pawnW36 = new Pawn("pawn_w-3-6");
const pawnW46 = new Pawn("pawn_w-4-6");
const pawnW56 = new Pawn("pawn_w-5-6");
const pawnW66 = new Pawn("pawn_w-6-6");
const pawnW76 = new Pawn("pawn_w-7-6");
const pawnB01 = new Pawn("pawn_b-0-1");
const pawnB11 = new Pawn("pawn_b-1-1");
const pawnB21 = new Pawn("pawn_b-2-1");
const pawnB31 = new Pawn("pawn_b-3-1");
const pawnB41 = new Pawn("pawn_b-4-1");
const pawnB51 = new Pawn("pawn_b-5-1");
const pawnB61 = new Pawn("pawn_b-6-1");
const pawnB71 = new Pawn("pawn_b-7-1");
const initialCoords = new Map();
const setStandardInitialCoords = function () {
chessPiecesArr.forEach((piece) =>
initialCoords.set(piece.curCoords, piece.imgName)
);
};
const setLoadedInitialCoords = function () {};
// Build board
let loaded = false;
const buildBoard = function () {
if (loaded) setLoadedInitialCoords();
else setStandardInitialCoords();
boardEl.innerHTML = "";
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
const coord = `${i}-${j}`;
let piece = initialCoords.get(coord);
const html = `<div class="field game__field ${
i % 2 === 0 ? "even" : "odd"
}" data-coords="${coord}" ${
piece
? `data-piece="${piece}-${coord}"><img class="game__piece"
src="./lib/pieces/${piece}.webp"
alt="${piece}" data-coords="${coord}" id="img-${coord}"
/>`
: ""
}</div>`;
boardEl.insertAdjacentHTML("beforeend", html);
if (piece && piece.slice(-1) === "w") {
allPiecesCoords[0].set(coord, `${piece}-${coord}`);
} else if (piece && piece.slice(-1) === "b") {
allPiecesCoords[1].set(coord, `${piece}-${coord}`);
}
boardCoords.push(coord);
}
}
document
.getElementById(`player__label--${activePlayer}`)
.classList.toggle("player__label--active");
};
const buildBeatenBoards = function () {
beatenBoardWhiteEl.innerHTML = "";
beatenBoardBlackEl.innerHTML = "";
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 8; j++) {
const coord = `${i}-${j}`;
const html = function (color) {
beatenFields[color].unshift(`${color}-${coord}`);
return `<div class="field beaten-pieces" data-coords="${color}-${coord}"></div>`;
};
beatenBoardWhiteEl.insertAdjacentHTML("beforeend", html(0));
beatenBoardBlackEl.insertAdjacentHTML("beforeend", html(1));
}
}
};
const init = function () {
buildBoard();
buildBeatenBoards();
};
////////////////////////
// Functions on click
const winGame = function (player) {
// Needs to be fully implemented
const playerName = player === 0 ? "White" : "Black";
alert(`${playerName} wins!`);
};
const changePlayer = function () {
document
.getElementById(`player__label--${activePlayer}`)
.classList.toggle("player__label--active");
activePlayer = activePlayer === 0 ? 1 : 0;
rivalPlayer = activePlayer === 0 ? 1 : 0;
document
.getElementById(`player__label--${activePlayer}`)
.classList.toggle("player__label--active");
};
const endMove = function () {
selectedPiece = "";
clearFields();
changePlayer();
};
const placeImg = function (coords, piece) {
const newField = document.querySelector(`div[data-coords="${coords}"]`);
const html = `<img class="game__piece"
src="./lib/pieces/${piece.imgName}.webp"
alt="${piece.imgName}" data-coords="${coords}" id="img-${coords}"/>`;
newField.insertAdjacentHTML("afterbegin", html);
};
const getPlayersPiece = function (coords, playerType) {
const player = playerType === "active" ? activePlayer : rivalPlayer;
const clickedPiece = chessPiecesArr.find(
(obj) => obj.namestring === allPiecesCoords[player].get(coords)
);
return clickedPiece;
};
const clearFields = function () {
const highlightedFields = document.querySelectorAll(".field__highlight");
highlightedFields.forEach((field) =>
field.classList.remove("field__highlight")
);
};
const clearSelectionIfPlayersPiece = function (coords) {
if (allPiecesCoords[activePlayer].has(coords)) {
selectedPiece = "";
}
};
const highlightFields = function (coordsArr) {
coordsArr.forEach((coord) => {
if (!boardCoords.includes(coord)) return;
document
.querySelector(`div[data-coords="${coord}"]`)
.classList.add("field__highlight");
});
};
const activatePiece = function (coords) {
if (!allPiecesCoords[activePlayer].has(coords)) return;
const clickedPiece = getPlayersPiece(coords, "active");
const toHighlight = clickedPiece.getPossibleMovs();
toHighlight.push(coords);
highlightFields(toHighlight);
return clickedPiece;
};
////////////////////////
// Event Listeners
boardEl.addEventListener("click", function (e) {
clearFields();
const clickedCoords = e.target.dataset.coords;
clearSelectionIfPlayersPiece(clickedCoords);
if (selectedPiece && selectedPiece.possibleMovs.includes(clickedCoords)) {
selectedPiece.movePiece(clickedCoords);
} else {
selectedPiece = activatePiece(clickedCoords);
}
});
//
//
//
init();