-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.js
364 lines (330 loc) · 10.9 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
class chess {
//constructors
constructor(pieces) {
this.board = document.getElementById('board');
this.box = this.board.querySelectorAll('.box');
this.pieces = pieces;
this.turn = 'white';
this.clickedPiece = null;
this.allowedMoves = null;
this.addEventListeners();
}
//adds event listeners to each piece
addEventListeners() {
this.pieces.forEach( piece => {
piece.img.addEventListener("click", this.pieceMove.bind(this));
piece.img.addEventListener("dragstart", this.pieceMove.bind(this));
piece.img.addEventListener("drop", this.pieceMove.bind(this));
});
this.box.forEach( box => {
box.addEventListener("click", this.movePiece.bind(this));
box.addEventListener("dragover", function(event){
event.preventDefault();
});
box.addEventListener("drop", this.movePiece.bind(this));
});
}
//moves
pieceMove(event) {
const name = event.target.getAttribute('id');
const allowedMoves = this.getPieceAllowedMoves(event, name);
if (allowedMoves) {
const position = this.getPieceByName(name).position;
const clickedBox = document.getElementById(position);
clickedBox.classList.add('clicked-box');
allowedMoves.forEach( allowedMove => {
if (document.body.contains(document.getElementById(allowedMove))) {
document.getElementById(allowedMove).classList.add('allowed');
}
});
}
else{
this.clearBoxes();
}
}
//changes the turn in gmae and for HTMl
changeTurn() {
if (this.turn == 'white') {
this.turn = 'black';
document.getElementById('turn').innerHTML = "Black's Turn";
}
else{
this.turn = 'white';
document.getElementById('turn').innerHTML = "White's Turn";
}
}
//returns the pieces that shares the color passed in
getPiecesByColor(color) {
return this.pieces.filter(obj => {
return obj.color === color
});
}
//gets the positions of each piece of the color passed in
getPlayerPositions(color){
const pieces = this.getPiecesByColor(color);
return pieces.map( a => parseInt(a.position));
}
//filters the position in the board
filterPositions(positions) {
return positions.filter(pos => {
return pos > 10 && pos < 89
});
};
//gets the positons that aren't blocked by other pieces
unblockedPositions(allowedPositions=[], position, color, checking=true){
position = parseInt(position);
const unblockedPositions = [];
if (color == 'white') {
var myBlockedPositions = this.getPlayerPositions('white');
var otherBlockedPositions = this.getPlayerPositions('black');
}
else{
var myBlockedPositions = this.getPlayerPositions('black');
var otherBlockedPositions = this.getPlayerPositions('white');
}
if (this.clickedPiece.getRank() == 'pawn') {
for (const move of allowedPositions[0]) { //attacking moves
if (checking && this.myKingChecked(move)) continue;
if (otherBlockedPositions.indexOf(move) != -1) unblockedPositions.push(move);
}
const blockedPositions = myBlockedPositions + otherBlockedPositions;
for (const move of allowedPositions[1]) { //moving moves
if (blockedPositions.indexOf(move) != -1) break;
else if (checking && this.myKingChecked(move, false)) continue;
unblockedPositions.push(move);
}
}
else{
allowedPositions.forEach( allowedPositionsGroup => {
for (const move of allowedPositionsGroup) {
if (myBlockedPositions.indexOf(move) != -1) {
break;
}
else if ( checking && this.myKingChecked(move) ) {
continue;
}
unblockedPositions.push(move);
if (otherBlockedPositions.indexOf(move) != -1) break;
}
});
}
return this.filterPositions(unblockedPositions);
}
//gets the allowed moves of each piece
getPieceAllowedMoves(event, pieceName){
const piece = this.getPieceByName(pieceName);
if(this.turn == piece.color){
this.clearBoxes();
this.setClickedPiece(piece);
if (event.type == 'dragstart') {
event.dataTransfer.setData("text", event.target.id);
}
let pieceAllowedMoves = piece.Moves();
if (piece.rank == 'king') {
pieceAllowedMoves = this.getCastlingBoxes(pieceAllowedMoves);
}
const allowedMoves = this.unblockedPositions( pieceAllowedMoves, piece.position, piece.color, true );
this.allowedMoves = allowedMoves;
return allowedMoves;
}
else if (this.clickedPiece && this.turn == this.clickedPiece.color && this.allowedMoves && this.allowedMoves.indexOf(piece.position) != -1) {
this.kill(piece);
}
else{
return 0;
}
}
//checks if king is able to castle and adds it to allowed moves
getCastlingBoxes(allowedMoves) {
if ( !this.clickedPiece.ableToCastle || this.king_checked(this.turn) ) return allowedMoves;
const rook1 = this.getPieceByName(this.turn+'Rook1');
const rook2 = this.getPieceByName(this.turn+'Rook2');
if (rook1 && rook1.ableToCastle) {
const castlingPosition = rook1.position + 2
if(
!this.positionHasExistingPiece(castlingPosition - 1) &&
!this.positionHasExistingPiece(castlingPosition) && !this.myKingChecked(castlingPosition, true) &&
!this.positionHasExistingPiece(castlingPosition + 1) && !this.myKingChecked(castlingPosition + 1, true)
)
allowedMoves[1].push(castlingPosition);
}
if (rook2 && rook2.ableToCastle) {
const castlingPosition = rook2.position - 1;
if(
!this.positionHasExistingPiece(castlingPosition - 1) && !this.myKingChecked(castlingPosition - 1, true) &&
!this.positionHasExistingPiece(castlingPosition) && !this.myKingChecked(castlingPosition, true)
)
allowedMoves[0].push(castlingPosition);
}
return allowedMoves;
}
getPieceByName(piecename) {
return this.pieces.filter( obj => obj.name === piecename )[0];
}
getPieceByPos(piecePosition) {
return this.pieces.filter(obj => obj.position === piecePosition )[0];
}
positionHasExistingPiece(position) {
return this.getPieceByPos(position) != undefined;
}
setClickedPiece(piece) {
this.clickedPiece = piece;
}
//moves the piece to its new positon
movePiece(event, box ='') {
box = box || event.target;
if (box.classList.contains('allowed')) {
const clickedPiece = this.clickedPiece;
if (clickedPiece) {
const newPosition = box.getAttribute('id');
if (clickedPiece.getRank() == 'king' || clickedPiece.getRank() == 'pawn')
clickedPiece.changePosition(newPosition, true);
else
clickedPiece.changePosition(newPosition);
box.append(clickedPiece.img);
this.clearBoxes();
this.changeTurn();
if (this.king_checked(this.turn)) {
if (this.king_dead(this.turn)) {
this.checkmate(clickedPiece.color);
}
else{
// alert('check');
}
}
}
else{
return 0;
}
}
if (event) event.preventDefault();
}
//kills any piece previously occupying the new positon
kill(piece) {
piece.img.parentNode.removeChild(piece.img);
piece.img.className = '';
const chosenBox = document.getElementById(piece.position);
this.pieces.splice(this.pieces.indexOf(piece), 1);
this.movePiece('', chosenBox);
}
//castels the rook
castleRook(rookName) {
const rook = this.getPieceByName(rookName);
const newPosition = rookName.indexOf('Rook2') != -1 ? rook.position - 2 : rook.position + 3;
this.setClickedPiece(rook);
const chosenBox = document.getElementById(newPosition);
chosenBox.classList.add('allowed');
this.movePiece('', chosenBox);
this.changeTurn();
}
//promotes a pawn to queen
promote(pawn) {
const queenName = pawn.name.replace('Pawn', 'Queen');
const image = pawn.img;
image.id = queenName;
image.src = image.src.replace('Pawn', 'Queen');
this.pieces.splice(this.pieces.indexOf(pawn), 1);
this.pieces.push( new Queen(pawn.position, queenName) );
}
//checks if the King is checked by the move you might mkae
myKingChecked(pos, kill=true){
const piece = this.clickedPiece;
const originalPosition = piece.position;
const otherPiece = this.getPieceByPos(pos);
const should_kill_other_piece = kill && otherPiece && otherPiece.rank != 'king';
piece.changePosition(pos);
if (should_kill_other_piece) this.pieces.splice(this.pieces.indexOf(otherPiece), 1);
if (this.king_checked(piece.color)) {
piece.changePosition(originalPosition);
if (should_kill_other_piece) this.pieces.push(otherPiece);
return 1;
}
else{
piece.changePosition(originalPosition);
if (should_kill_other_piece) this.pieces.push(otherPiece);
return 0;
}
}
//checks if the king is checkmated
king_dead(color) {
const pieces = this.getPiecesByColor(color);
for (const piece of pieces) {
this.setClickedPiece(piece);
const allowedMoves = this.unblockedPositions( piece.Moves(), piece.position, piece.color, true );
if (allowedMoves.length) {
this.setClickedPiece(null);
return 0;
}
}
this.setClickedPiece(null);
return 1;
}
//checks if king is checked
king_checked(color) {
const piece = this.clickedPiece;
const king = this.getPieceByName(color + 'King');
const enemyColor = (color == 'white') ? 'black' : 'white';
const enemyPieces = this.getPiecesByColor(enemyColor);
for (const enemyPiece of enemyPieces) {
this.setClickedPiece(enemyPiece);
const allowedMoves = this.unblockedPositions( enemyPiece.Moves(), enemyPiece.position, enemyColor, false );
if (allowedMoves.indexOf(king.position) != -1) {
this.setClickedPiece(piece);
return 1;
}
}
this.setClickedPiece(piece);
return 0;
}
//clears the boxes from clicked box class
clearBoxes(){
this.allowedMoves = null;
const allowedBoxes = this.board.querySelectorAll('.allowed');
allowedBoxes.forEach(allowedBox => allowedBox.classList.remove('allowed') );
const cllickedBox = document.getElementsByClassName('clicked-box')[0];
if (cllickedBox) cllickedBox.classList.remove('clicked-box');
}
//initianes the endscene
checkmate(color){
const endScene = document.getElementById('endscene');
endScene.getElementsByClassName('winning-sign')[0].innerHTML = color + ' Wins';
endScene.classList.add('show');
}
}
//initializes a array of pieces to play
const pieces = [
new Rook(11, 'whiteRook1'),
new Knight(12, 'whiteKnight1'),
new Bishop(13, 'whiteBishop1'),
new Queen(14, 'whiteQueen'),
new King(15, 'whiteKing'),
new Bishop(16, 'whiteBishop2'),
new Knight(17, 'whiteKnight2'),
new Rook(18, 'whiteRook2'),
new Pawn(21, 'whitePawn1'),
new Pawn(22, 'whitePawn2'),
new Pawn(23, 'whitePawn3'),
new Pawn(24, 'whitePawn4'),
new Pawn(25, 'whitePawn5'),
new Pawn(26, 'whitePawn6'),
new Pawn(27, 'whitePawn7'),
new Pawn(28, 'whitePawn8'),
new Pawn(71, 'blackPawn1'),
new Pawn(72, 'blackPawn2'),
new Pawn(73, 'blackPawn3'),
new Pawn(74, 'blackPawn4'),
new Pawn(75, 'blackPawn5'),
new Pawn(76, 'blackPawn6'),
new Pawn(77, 'blackPawn7'),
new Pawn(78, 'blackPawn8'),
new Rook(81, 'blackRook1'),
new Knight(82, 'blackKnight1'),
new Bishop(83, 'blackBishop1'),
new Queen(84, 'blackQueen'),
new King(85, 'blackKing'),
new Bishop(86, 'blackBishop2'),
new Knight(87, 'blackKnight2'),
new Rook(88, 'blackRook2')
];
//initioalizes a new chess game
const game = new chess(pieces);