-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSimulationGame.js
96 lines (76 loc) · 2.58 KB
/
SimulationGame.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
class SimulationGame extends Game {
startNewGame(pieces, turn) {
this._setPieces(pieces);
this.turn = turn;
this.clickedPiece = null;
}
saveHistory() {}
addToHistory(move) {}
triggerEvent(eventName, params) {}
clearEvents() {}
undo() {}
getPieceAllowedMoves(pieceName){
const piece = this.getPieceByName(pieceName);
if(piece && this.turn === piece.color){
this.setClickedPiece(piece);
let pieceAllowedMoves = getAllowedMoves(piece);
if (piece.rank === 'king') {
pieceAllowedMoves = this.getCastlingSquares(piece, pieceAllowedMoves);
}
return this.unblockedPositions(piece, pieceAllowedMoves, true);
}
else{
return [];
}
}
movePiece(pieceName, position) {
const piece = this.getPieceByName(pieceName);
/*if (!piece) {
return false;
}*/
const prevPosition = piece.position;
const existedPiece = this.getPieceByPos(position)
if (existedPiece) {
this.kill(existedPiece);
}
const castling = !existedPiece && piece.rank === 'king' && piece.ableToCastle === true;
if (castling) {
if (position - prevPosition === 2) {
this.castleRook(piece.color + 'Rook2');
}
else if (position - prevPosition === -2) {
this.castleRook(piece.color + 'Rook1');
}
changePosition(piece, position, true);
}
else {
changePosition(piece, position);
}
if (piece.rank === 'pawn' && (position > 80 || position < 20)) {
this.promote(piece);
}
// this.history.add({ from: prevPosition, to: position, piece: piece, castling });
this.changeTurn();
return true;
}
king_checked(color) {
const piece = this.clickedPiece;
const king = this.getPieceByName(color + 'King');
if (!king) {
return true;
}
const enemyColor = (color === 'white') ? 'black' : 'white';
const enemyPieces = this.getPiecesByColor(enemyColor);
for (const enemyPiece of enemyPieces) {
this.setClickedPiece(enemyPiece);
const allowedMoves = this.unblockedPositions(enemyPiece, getAllowedMoves(enemyPiece), false);
if (allowedMoves.indexOf(king.position) !== -1) {
this.setClickedPiece(piece);
return 1;
}
}
this.setClickedPiece(piece);
return 0;
}
checkmate(color){}
}