-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
115 lines (102 loc) · 3.13 KB
/
Game.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
/* global King */
/* global Dom, sq, board, whiteToPlay, selectedSquare */
/* eslint-disable no-unused-vars, no-global-assign */
class Game {
static addPiece(side, piece, column) {
const columnLetter = 'ABCDEFGH'[column - 1];
const newPiece = new piece(columnLetter + side.pieceRow, side.colour);
side.array.push(newPiece);
return newPiece;
}
static isMyTurn(piece) {
return piece.colour === (whiteToPlay ? 'white' : 'black');
}
static currentTurnColour() {
return whiteToPlay ? 'white' : 'black';
}
static opponentColour() {
return whiteToPlay ? 'black' : 'white';
}
static move(piece, square) {
const moved = piece.attemptMoveTo(square);
selectedSquare = null;
if (moved) {
this.finishMove();
}
}
static finishMove() {
Dom.togglePlayer();
const whiteCheckmate = Game.isCheckmate('white');
const blackCheckmate = Game.isCheckmate('black');
if (whiteCheckmate || blackCheckmate) {
Dom.message('Checkmate ' + (whiteCheckmate ? 'black' : 'white') + ' wins.');
}
}
static isInCheck(colour) {
const king = board
.piecesByColour(colour)
.find(piece => piece instanceof King);
return !!king.square.isThreatenedBy(king.opponentColour).length;
}
static isCheckmate(colour) {
if (!Game.isInCheck('white') && !Game.isInCheck('black')) {
Dom.showHint(false);
return false;
}
Dom.showHint(true);
Dom.message('Check');
return !Game.movesPreventingCheck(colour).length;
}
static movesPreventingCheck(colour) {
return board.piecesByColour(colour)
.map(piece => {
return {
piece,
preventCheck: Game.pieceMovesPreventingCheck(piece)
};
})
.filter(pieceMoves => pieceMoves.preventCheck.length);
}
static pieceMovesPreventingCheck(piece) {
return piece.availableSquares()
.filter(square => {
return Game.simulateMove(piece, square, () => !Game.isInCheck(piece.colour));
});
}
static giveHint() {
Game.movesPreventingCheck(Game.currentTurnColour()).forEach(hint => {
hint.piece.square.highlight(2);
hint.preventCheck.forEach(square => square.highlight());
});
}
static simulateMove(piece, square, outcomeFunction) {
const currentSquare = piece.square;
const pieceAtMoveSquare = square.piece;
piece.moveTo(square);
const outcome = outcomeFunction();
piece.moveTo(currentSquare);
if (pieceAtMoveSquare) {
square.setPiece(pieceAtMoveSquare);
}
return outcome;
}
static moveByNotation(notation) {
// Be5
const square = sq(notation.slice(-2));
const pieceName = notation.length === 2 ? 'p' : notation[0];
const piece = board
.piecesByColour(Game.currentTurnColour())
.filter(piece => piece.name.toLowerCase() === pieceName.toLowerCase())
.find(piece => piece.availableSquares().includes(square));
if (piece) {
Game.move(piece, square);
}
}
static handleNotation(event) {
event.preventDefault();
const $input = this.querySelector('input');
const notation = $input.value;
Game.moveByNotation(notation);
$input.value = '';
}
}