X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FDynamo.js;h=cdb02c6afffe3db29e3ce3f1def1be810fac8743;hb=107dc1bd5361e2538b1551bdcc37c1e90a444b83;hp=3cb8dc473420ec895121fac2403568034b81a0ed;hpb=e78633dbd29be1920e0c02509b6ce62b87b61d77;p=vchess.git diff --git a/client/src/variants/Dynamo.js b/client/src/variants/Dynamo.js index 3cb8dc47..cdb02c6a 100644 --- a/client/src/variants/Dynamo.js +++ b/client/src/variants/Dynamo.js @@ -2,7 +2,7 @@ import { ChessRules, Move, PiPo } from "@/base_rules"; import { randInt } from "@/utils/alea"; export class DynamoRules extends ChessRules { - // TODO: later, allow to push out pawns on a and h files + // TODO? later, allow to push out pawns on a and h files static get HasEnpassant() { return false; } @@ -43,7 +43,6 @@ export class DynamoRules extends ChessRules { }); this.amoves.push(move); } - this.subTurn = 1; // Stack "first moves" (on subTurn 1) to merge and check opposite moves this.firstMove = []; } @@ -157,9 +156,8 @@ export class DynamoRules extends ChessRules { return [dx / divisor, dy / divisor]; } - // There was something on x2,y2, maybe our color, pushed/pulled. - // Also, the pushed/pulled piece must exit the board. - isAprioriValidExit([x1, y1], [x2, y2], color2) { + // There was something on x2,y2, maybe our color, pushed or (self)pulled + isAprioriValidExit([x1, y1], [x2, y2], color2, piece2) { const color1 = this.getColor(x1, y1); const pawnShift = (color1 == 'w' ? -1 : 1); const lastRank = (color1 == 'w' ? 0 : 7); @@ -174,13 +172,17 @@ export class DynamoRules extends ChessRules { } return !V.OnBoard(i, j); }; - switch (this.getPiece(x1, y1)) { + switch (piece2 || this.getPiece(x1, y1)) { case V.PAWN: return ( x1 + pawnShift == x2 && ( (color1 == color2 && x2 == lastRank && y1 == y2) || - (color1 != color2 && deltaY == 1 && !V.OnBoard(x2, 2 * y2 - y1)) + ( + color1 != color2 && + deltaY == 1 && + !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + ) ) ); case V.ROOK: @@ -208,15 +210,34 @@ export class DynamoRules extends ChessRules { return false; } + isAprioriValidVertical([x1, y1], x2) { + const piece = this.getPiece(x1, y1); + const deltaX = Math.abs(x1 - x2); + const startRank = (this.getColor(x1, y1) == 'w' ? 6 : 1); + return ( + [V.QUEEN, V.ROOK].includes(piece) || + ( + [V.KING, V.PAWN].includes(piece) && + ( + deltaX == 1 || + (deltaX == 2 && piece == V.PAWN && x1 == startRank) + ) + ) + ); + } + // NOTE: for pushes, play the pushed piece first. // for pulls: play the piece doing the action first // NOTE: to push a piece out of the board, make it slide until its king getPotentialMovesFrom([x, y]) { const color = this.turn; + const sqCol = this.getColor(x, y); + const pawnShift = (color == 'w' ? -1 : 1); + const pawnStartRank = (color == 'w' ? 6 : 1); + const getMoveHash = (m) => { + return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end); + }; if (this.subTurn == 1) { - const getMoveHash = (m) => { - return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end); - }; const addMoves = (dir, nbSteps) => { const newMoves = this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps) @@ -225,12 +246,20 @@ export class DynamoRules extends ChessRules { Array.prototype.push.apply(moves, newMoves); }; // Free to play any move (if piece of my color): - const moves = - this.getColor(x, y) == color + let moves = + sqCol == color ? super.getPotentialMovesFrom([x, y]) : []; - const pawnShift = (color == 'w' ? -1 : 1); - const pawnStartRank = (color == 'w' ? 6 : 1); + // There may be several suicide moves: keep only one + let hasExit = false; + moves = moves.filter(m => { + const suicide = (m.appear.length == 0); + if (suicide) { + if (hasExit) return false; + hasExit = true; + } + return true; + }); // Structure to avoid adding moves twice (can be action & move) let movesHash = {}; moves.forEach(m => { movesHash[getMoveHash(m)] = true; }); @@ -259,7 +288,6 @@ export class DynamoRules extends ChessRules { ) { const deltaX = Math.abs(i - x); const deltaY = Math.abs(j - y); - // Can a priori go both ways, except with pawns switch (this.getPiece(i, j)) { case V.PAWN: if ( @@ -267,13 +295,12 @@ export class DynamoRules extends ChessRules { deltaX <= 2 && deltaY <= 1 ) { - const pColor = this.getColor(x, y); - if (pColor == color && deltaY == 0) { + if (sqCol == color && deltaY == 0) { // Pushed forward const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1); addMoves(step, maxSteps); } - else if (pColor != color && deltaY == 1 && deltaX == 1) + else if (sqCol != color && deltaY == 1 && deltaX == 1) // Pushed diagonally addMoves(step, 1); } @@ -285,8 +312,8 @@ export class DynamoRules extends ChessRules { if (deltaX == deltaY) addMoves(step); break; case V.QUEEN: - if (deltaX == 0 || deltaY == 0 || deltaX == deltaY) - addMoves(step); + // All steps are valid for a queen: + addMoves(step); break; case V.KING: if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1); @@ -298,16 +325,18 @@ export class DynamoRules extends ChessRules { } // If subTurn == 2 then we should have a first move, // which restrict what we can play now: only in the first move direction - // NOTE: no need for knight or pawn checks, because the move will be - // naturally limited in those cases. const L = this.firstMove.length; const fm = this.firstMove[L-1]; - if (fm.appear.length == 2 && fm.vanish.length == 2) - // Castle: no real move playable then. + if ( + (fm.appear.length == 2 && fm.vanish.length == 2) || + (fm.vanish[0].c == sqCol && sqCol != color) + ) { + // Castle or again opponent color: no move playable then. return []; - if (fm.appear.length == 0) { - // Piece at subTurn 1 just exited the board. - // Can I be a piece which caused the exit? + } + const piece = this.getPiece(x, y); + const getPushExit = () => { + // Piece at subTurn 1 exited: can I have caused the exit? if ( this.isAprioriValidExit( [x, y], @@ -318,17 +347,83 @@ export class DynamoRules extends ChessRules { // Seems so: const dir = this.getNormalizedDirection( [fm.start.x - x, fm.start.y - y]); - return this.getMovesInDirection([x, y], dir); + const nbSteps = + [V.PAWN, V.KING, V.KNIGHT].includes(piece) + ? 1 + : null; + return this.getMovesInDirection([x, y], dir, nbSteps); } + return []; } - else { + const getPushMoves = () => { + // Piece from subTurn 1 is still on board: const dirM = this.getNormalizedDirection( [fm.end.x - fm.start.x, fm.end.y - fm.start.y]); const dir = this.getNormalizedDirection( [fm.start.x - x, fm.start.y - y]); // Normalized directions should match if (dir[0] == dirM[0] && dir[1] == dirM[1]) { - // And nothing should stand between [x, y] and the square fm.start + // We don't know if first move is a pushed piece or normal move, + // so still must check if the push is valid. + const deltaX = Math.abs(fm.start.x - x); + const deltaY = Math.abs(fm.start.y - y); + switch (piece) { + case V.PAWN: + if (x == pawnStartRank) { + if ( + (fm.start.x - x) * pawnShift < 0 || + deltaX >= 3 || + deltaY >= 2 || + (fm.vanish[0].c == color && deltaY > 0) || + (fm.vanish[0].c != color && deltaY == 0) || + Math.abs(fm.end.x - fm.start.x) > deltaX || + fm.end.y - fm.start.y != fm.start.y - y + ) { + return []; + } + } + else { + if ( + fm.start.x - x != pawnShift || + deltaY >= 2 || + (fm.vanish[0].c == color && deltaY == 1) || + (fm.vanish[0].c != color && deltaY == 0) || + fm.end.x - fm.start.x != pawnShift || + fm.end.y - fm.start.y != fm.start.y - y + ) { + return []; + } + } + break; + case V.KNIGHT: + if ( + (deltaX + deltaY != 3 || (deltaX == 0 && deltaY == 0)) || + (fm.end.x - fm.start.x != fm.start.x - x) || + (fm.end.y - fm.start.y != fm.start.y - y) + ) { + return []; + } + break; + case V.KING: + if ( + (deltaX >= 2 || deltaY >= 2) || + (fm.end.x - fm.start.x != fm.start.x - x) || + (fm.end.y - fm.start.y != fm.start.y - y) + ) { + return []; + } + break; + case V.BISHOP: + if (deltaX != deltaY) return []; + break; + case V.ROOK: + if (deltaX != 0 && deltaY != 0) return []; + break; + case V.QUEEN: + if (deltaX != deltaY && deltaX != 0 && deltaY != 0) return []; + break; + } + // Nothing should stand between [x, y] and the square fm.start let [i, j] = [x + dir[0], y + dir[1]]; while ( (i != fm.start.x || j != fm.start.y) && @@ -340,14 +435,135 @@ export class DynamoRules extends ChessRules { if (i == fm.start.x && j == fm.start.y) return this.getMovesInDirection([x, y], dir); } + return []; + } + const getPullExit = () => { + // Piece at subTurn 1 exited: can I be pulled? + // Note: kings cannot suicide, so fm.vanish[0].p is not KING. + // Could be PAWN though, if a pawn was pushed out of board. + if ( + fm.vanish[0].p != V.PAWN && //pawns cannot pull + this.isAprioriValidExit( + [x, y], + [fm.start.x, fm.start.y], + fm.vanish[0].c, + fm.vanish[0].p + ) + ) { + // Seems so: + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + const nbSteps = (fm.vanish[0].p == V.KNIGHT ? 1 : null); + return this.getMovesInDirection([x, y], dir, nbSteps); + } + return []; + }; + const getPullMoves = () => { + if (fm.vanish[0].p == V.PAWN) + // pawns cannot pull + return []; + const dirM = this.getNormalizedDirection( + [fm.end.x - fm.start.x, fm.end.y - fm.start.y]); + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + // Normalized directions should match + if (dir[0] == dirM[0] && dir[1] == dirM[1]) { + // Am I at the right distance? + const deltaX = Math.abs(x - fm.start.x); + const deltaY = Math.abs(y - fm.start.y); + if ( + (fm.vanish[0].p == V.KING && (deltaX > 1 || deltaY > 1)) || + (fm.vanish[0].p == V.KNIGHT && + (deltaX + deltaY != 3 || deltaX == 0 || deltaY == 0)) + ) { + return []; + } + // Nothing should stand between [x, y] and the square fm.start + let [i, j] = [x + dir[0], y + dir[1]]; + while ( + (i != fm.start.x || j != fm.start.y) && + this.board[i][j] == V.EMPTY + ) { + i += dir[0]; + j += dir[1]; + } + if (i == fm.start.x && j == fm.start.y) + return this.getMovesInDirection([x, y], dir); + } + return []; + }; + if (fm.vanish[0].c != color) { + // Only possible action is a push: + if (fm.appear.length == 0) return getPushExit(); + return getPushMoves(); + } + else if (sqCol != color) { + // Only possible action is a pull, considering moving piece abilities + if (fm.appear.length == 0) return getPullExit(); + return getPullMoves(); + } + else { + // My color + my color: both actions possible + // Structure to avoid adding moves twice (can be action & move) + let movesHash = {}; + if (fm.appear.length == 0) { + const pushes = getPushExit(); + pushes.forEach(m => { movesHash[getMoveHash(m)] = true; }); + return ( + pushes.concat(getPullExit().filter(m => !movesHash[getMoveHash(m)])) + ); + } + const pushes = getPushMoves(); + pushes.forEach(m => { movesHash[getMoveHash(m)] = true; }); + return ( + pushes.concat(getPullMoves().filter(m => !movesHash[getMoveHash(m)])) + ); } return []; } + getSlideNJumpMoves([x, y], steps, oneStep) { + let moves = []; + const c = this.getColor(x, y); + const piece = this.getPiece(x, y); + outerLoop: for (let step of steps) { + let i = x + step[0]; + let j = y + step[1]; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + moves.push(this.getBasicMove([x, y], [i, j])); + if (oneStep) continue outerLoop; + i += step[0]; + j += step[1]; + } + if (V.OnBoard(i, j)) { + if (this.canTake([x, y], [i, j])) + moves.push(this.getBasicMove([x, y], [i, j])); + } + else { + // Add potential board exit (suicide), except for the king + if (piece != V.KING) { + moves.push({ + start: { x: x, y: y}, + end: { x: this.kingPos[c][0], y: this.kingPos[c][1] }, + appear: [], + vanish: [ + new PiPo({ + x: x, + y: y, + c: c, + p: piece + }) + ] + }); + } + } + } + return moves; + } + // Does m2 un-do m1 ? (to disallow undoing actions) oppositeMoves(m1, m2) { const isEqual = (av1, av2) => { - // Precondition: av1 and av2 length = 2 for (let av of av1) { const avInAv2 = av2.find(elt => { return ( @@ -361,11 +577,12 @@ export class DynamoRules extends ChessRules { } return true; }; + // All appear and vanish arrays must have the same length + const mL = m1.appear.length; return ( - m1.appear.length == 2 && - m2.appear.length == 2 && - m1.vanish.length == 2 && - m2.vanish.length == 2 && + m2.appear.length == mL && + m1.vanish.length == mL && + m2.vanish.length == mL && isEqual(m1.appear, m2.vanish) && isEqual(m1.vanish, m2.appear) ); @@ -381,6 +598,7 @@ export class DynamoRules extends ChessRules { filterValid(moves) { const color = this.turn; + const La = this.amoves.length; if (this.subTurn == 1) { return moves.filter(m => { // A move is valid either if it doesn't result in a check, @@ -388,13 +606,17 @@ export class DynamoRules extends ChessRules { // (not undoing a potential move + action of the opponent) this.play(m); let res = this.underCheck(color); - if (res) { + let isOpposite = La > 0 && this.oppositeMoves(this.amoves[La-1], m); + if (res || isOpposite) { const moves2 = this.getAllPotentialMoves(); for (let m2 of moves2) { this.play(m2); const res2 = this.underCheck(color); + const amove = this.getAmove(m, m2); + isOpposite = + La > 0 && this.oppositeMoves(this.amoves[La-1], amove); this.undo(m2); - if (!res2) { + if (!res2 && !isOpposite) { res = false; break; } @@ -404,9 +626,8 @@ export class DynamoRules extends ChessRules { return !res; }); } - const Lf = this.firstMove.length; - const La = this.amoves.length; if (La == 0) return super.filterValid(moves); + const Lf = this.firstMove.length; return ( super.filterValid( moves.filter(m => { @@ -461,19 +682,41 @@ export class DynamoRules extends ChessRules { } isAttackedByPawn([x, y], color) { - const lastRank = (color == 'w' ? 0 : 7); - if (x != lastRank) - // The king can be pushed out by a pawn only on last rank - return false; + // The king can be pushed out by a pawn on last rank or near the edge const pawnShift = (color == "w" ? 1 : -1); for (let i of [-1, 1]) { if ( - y + i >= 0 && - y + i < V.size.y && + V.OnBoard(x + pawnShift, y + i) && + this.board[x + pawnShift][y + i] != V.EMPTY && this.getPiece(x + pawnShift, y + i) == V.PAWN && this.getColor(x + pawnShift, y + i) == color ) { - return true; + if (!V.OnBoard(x - pawnShift, y - i)) return true; + } + } + return false; + } + + static OnTheEdge(x, y) { + return (x == 0 || x == 7 || y == 0 || y == 7); + } + + isAttackedByKing([x, y], color) { + // Attacked if I'm on the edge and the opponent king just next, + // but not on the edge. + if (V.OnTheEdge(x, y)) { + for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + const [i, j] = [x + step[0], y + step[1]]; + if ( + V.OnBoard(i, j) && + !V.OnTheEdge(i, j) && + this.board[i][j] != V.EMPTY && + this.getPiece(i, j) == V.KING + // NOTE: since only one king of each color, and (x, y) is occupied + // by our king, no need to check other king's color. + ) { + return true; + } } } return false; @@ -503,12 +746,19 @@ export class DynamoRules extends ChessRules { } doClick(square) { - // If subTurn == 2 && square is empty && !underCheck, + // A click to promote a piece on subTurn 2 would trigger this. + // For now it would then return [NaN, NaN] because surrounding squares + // have no IDs in the promotion modal. TODO: improve this? + if (isNaN(square[0])) return null; + // If subTurn == 2 && square is empty && !underCheck && !isOpposite, // then return an empty move, allowing to "pass" subTurn2 + const La = this.amoves.length; + const Lf = this.firstMove.length; if ( this.subTurn == 2 && this.board[square[0]][square[1]] == V.EMPTY && - !this.underCheck(this.turn) + !this.underCheck(this.turn) && + (La == 0 || !this.oppositeMoves(this.amoves[La-1], this.firstMove[Lf-1])) ) { return { start: { x: -1, y: -1 }, @@ -556,6 +806,7 @@ export class DynamoRules extends ChessRules { this.disaggregateFlags(JSON.parse(move.flags)); V.UndoOnBoard(this.board, move); if (this.subTurn == 1) { + this.amoves.pop(); this.turn = V.GetOppCol(this.turn); this.movesCount--; }