Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 26x 26x 344x 26x 26x 293x 26x 26x 26x 51x 24x 2x 38x 38x 38x 38x 38x 38x 38x 11x 11x 11x 11x 11x 11x 11x 11x 11x 22x 11x 55x 11x 11x 11x 11x 6x 3x 6x 27x 27x 27x 24x 24x 24x 1x 1x 1x 1x 1x 24x 24x 3x 2x | import { EventEmitter } from 'events';
import { Game } from './game.js';
import { GameValidation } from './gameValidation.js';
import { Piece } from './piece.js';
// private methods
function isMoveValid (src, dest, validMoves) {
let
i = 0,
isFound = (expr, sq) => {
return ((typeof expr === 'string' && sq.file + sq.rank === expr) ||
(expr && expr.rank && expr.file &&
sq.file === expr.file && sq.rank === expr.rank));
},
squares = [];
for (i = 0; i < validMoves.length; i++) {
if (isFound(src, validMoves[i].src)) {
squares = validMoves[i].squares;
}
}
Eif (squares && squares.length > 0) {
for (i = 0; i < squares.length; i++) {
if (isFound(dest, squares[i])) {
return true;
}
}
}
return false;
}
function updateGameClient (gameClient) {
return gameClient.validation.start((err, result) => {
Iif (err) {
throw new Error(err);
}
gameClient.isCheck = result.isCheck;
gameClient.isCheckmate = result.isCheckmate;
gameClient.isRepetition = result.isRepetition;
gameClient.isStalemate = result.isStalemate;
gameClient.validMoves = result.validMoves;
});
}
// ctor
export class SimpleGameClient extends EventEmitter {
constructor (game) {
super();
this.isCheck = false;
this.isCheckmate = false;
this.isRepetition = false;
this.isStalemate = false;
this.game = game;
this.validMoves = [];
this.validation = GameValidation.create(this.game);
// bubble the game and board events
['check', 'checkmate'].forEach((ev) => {
this.game.on(ev, (data) => this.emit(ev, data));
});
['capture', 'castle', 'enPassant', 'move', 'promote'].forEach((ev) => {
this.game.board.on(ev, (data) => this.emit(ev, data));
});
}
static create () {
let
game = Game.create(),
gameClient = new SimpleGameClient(game);
updateGameClient(gameClient);
return gameClient;
}
getStatus (forceUpdate) {
if (forceUpdate) {
updateGameClient(this);
}
return {
board : this.game.board,
isCheck : this.isCheck,
isCheckmate : this.isCheckmate,
isRepetition : this.isRepetition,
isStalemate : this.isStalemate,
validMoves : this.validMoves
};
}
move (src, dest, promo) {
let
move = null,
side = this.game.getCurrentSide();
if (src && dest && isMoveValid(src, dest, this.validMoves)) {
move = this.game.board.move(src, dest);
Eif (move) {
// apply pawn promotion if applicable
if (promo) {
let piece;
switch (promo) {
case 'B':
piece = Piece.createBishop(side);
break;
case 'N':
piece = Piece.createKnight(side);
break;
case 'Q':
piece = Piece.createQueen(side);
break;
case 'R':
piece = Piece.createRook(side);
break;
default:
piece = null;
break;
}
Eif (piece) {
this.game.board.promote(move.move.postSquare, piece);
/*
p.moveCount = move.move.postSquare.piece.moveCount;
move.move.postSquare.piece = p;
//*/
}
}
updateGameClient(this);
return move;
}
}
throw new Error(`Move is invalid (${ src } to ${ dest })`);
}
getCaptureHistory () {
return this.game.captureHistory;
}
}
export default { SimpleGameClient };
|