X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FSynchrone2.js;h=17b0be325643ac0a8fcc1894ba1f5f75d32f8da5;hb=HEAD;hp=ee016844521d47b6354488964aaf8921cf096d72;hpb=715f705c877510885be243c84f62722f6136abbe;p=vchess.git diff --git a/client/src/variants/Synchrone2.js b/client/src/variants/Synchrone2.js index ee016844..17b0be32 100644 --- a/client/src/variants/Synchrone2.js +++ b/client/src/variants/Synchrone2.js @@ -47,8 +47,8 @@ export class Synchrone2Rules extends Synchrone1Rules { ); } - static GenRandInitFen(randomness) { - const res = ChessRules.GenRandInitFen(randomness); + static GenRandInitFen(options) { + const res = ChessRules.GenRandInitFen(options); // Add initFen field: return res.slice(0, -1) + res.split(' ')[0] + " -"; } @@ -93,7 +93,7 @@ export class Synchrone2Rules extends Synchrone1Rules { this.board = initBoard; const movesInit = super.getPotentialMovesFrom([x, y]); this.board = saveBoard; - const target = appeared.find(a => a.c == oppCol); + const target = appeared.find(a => a.c == oppCol) || { x: -1, y: -1 }; let movesNow = super.getPotentialMovesFrom([x, y]).filter(m => { return ( m.end.x == target.x && @@ -118,10 +118,10 @@ export class Synchrone2Rules extends Synchrone1Rules { } filterValid(moves) { - if (moves.length == 0) return []; - if (moves.length == 1 && moves[0].vanish.length == 0) return moves; + const nonEmptyMove = moves.find(m => m.vanish.length > 0); + if (!nonEmptyMove) return moves; // filterValid can be called when it's "not our turn": - const color = moves.find(m => m.vanish.length > 0).vanish[0].c; + const color = nonEmptyMove.vanish[0].c; return moves.filter(m => { if (m.vanish.length == 0) return true; const piece = m.vanish[0].p; @@ -141,9 +141,19 @@ export class Synchrone2Rules extends Synchrone1Rules { return this.filterValid(this.getPotentialMovesFrom([x, y])); } - play(move) { + getAllValidMoves() { + const moves = this.filterValid(super.getAllPotentialMoves()); + if (this.movesCount % 4 <= 1) return moves; + const emptyIdx = moves.findIndex(m => m.vanish.length == 0); + if (emptyIdx >= 0) + // Keep only one pass move (for computer play) + return moves.filter((m, i) => m.vanish.length > 0 || i == emptyIdx); + return moves; + } + + play(move, noFlag) { if (this.movesCount % 4 == 0) this.initfenStack.push(this.getBaseFen()); - move.flags = JSON.stringify(this.aggregateFlags()); + if (!noFlag) move.flags = JSON.stringify(this.aggregateFlags()); // Do not play on board (would reveal the move...) this.turn = V.GetOppCol(this.turn); this.movesCount++; @@ -191,8 +201,8 @@ export class Synchrone2Rules extends Synchrone1Rules { move.smove = smove; } - undo(move) { - this.disaggregateFlags(JSON.parse(move.flags)); + undo(move, noFlag) { + if (!noFlag) this.disaggregateFlags(JSON.parse(move.flags)); if (this.turn == 'w') // Back to the middle of the move V.UndoOnBoard(this.board, move.smove); @@ -213,12 +223,15 @@ export class Synchrone2Rules extends Synchrone1Rules { } getCurrentScore() { - if (this.movesCount % 4 != 0) - // Turn (2 x [white + black]) not over yet + if (this.movesCount % 2 != 0) + // Turn [white + black] not over yet return "*"; // Was a king captured? if (this.kingPos['w'][0] < 0) return "0-1"; if (this.kingPos['b'][0] < 0) return "1-0"; + if (this.movesCount % 4 == 2) + // Turn (2 x [white + black]) not over yet + return "*"; const whiteCanMove = this.atLeastOneMove('w'); const blackCanMove = this.atLeastOneMove('b'); if (whiteCanMove && blackCanMove) return "*"; @@ -236,6 +249,12 @@ export class Synchrone2Rules extends Synchrone1Rules { return (whiteCanMove ? "1-0" : "0-1"); } + getComputerMove() { + if (this.movesCount % 4 <= 1) return super.getComputerMove(); + const moves = this.getAllValidMoves(); + return moves[randInt(moves.length)]; + } + getNotation(move) { if (move.vanish.length == 0) return "pass"; return super.getNotation(move);