X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FMarseille.js;h=c3bec32f32f39254cd31c912708db56cfb5a45b3;hb=3a2a7b5fd3c6bfd0752838094c27e1fb6172d109;hp=19d35a63a730c522dc030e699217786d125d2041;hpb=6808d7a16ec1e761c6a2dffec2281c96953e4d89;p=vchess.git diff --git a/client/src/variants/Marseille.js b/client/src/variants/Marseille.js index 19d35a63..c3bec32f 100644 --- a/client/src/variants/Marseille.js +++ b/client/src/variants/Marseille.js @@ -3,10 +3,10 @@ import { randInt } from "@/utils/alea"; export const VariantRules = class MarseilleRules extends ChessRules { static IsGoodEnpassant(enpassant) { - if (enpassant != "-") { - const squares = enpassant.split(","); - if (squares.length > 2) return false; - for (let sq of squares) { + const squares = enpassant.split(","); + if (squares.length > 2) return false; + for (let sq of squares) { + if (sq != "-") { const ep = V.SquareToCoords(sq); if (isNaN(ep.x) || !V.OnBoard(ep)) return false; } @@ -20,31 +20,27 @@ export const VariantRules = class MarseilleRules extends ChessRules { // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn) getEnpassantFen() { - const L = this.epSquares.length; - if (this.epSquares[L - 1].every(epsq => epsq === undefined)) return "-"; //no en-passant - let res = ""; - this.epSquares[L - 1].forEach(epsq => { - if (epsq) res += V.CoordsToSquare(epsq) + ","; - }); - return res.slice(0, -1); //remove last comma + return this.epSquares[this.epSquares.length - 1].map( + epsq => epsq === undefined + ? "-" //no en-passant + : V.CoordsToSquare(epsq) + ).join(","); } setOtherVariables(fen) { const parsedFen = V.ParseFen(fen); this.setFlags(parsedFen.flags); - if (parsedFen.enpassant == "-") this.epSquares = [[undefined]]; - else { - let res = []; - const squares = parsedFen.enpassant.split(","); - for (let sq of squares) res.push(V.SquareToCoords(sq)); - this.epSquares = [res]; - } - this.scanKingsRooks(fen); + this.epSquares = [parsedFen.enpassant.split(",").map(sq => { + if (sq != "-") return V.SquareToCoords(sq); + return undefined; + })]; + this.scanKings(fen); // Extract subTurn from turn indicator: "w" (first move), or // "w1" or "w2" white subturn 1 or 2, and same for black const fullTurn = V.ParseFen(fen).turn; this.turn = fullTurn[0]; - this.subTurn = fullTurn[1] || 0; //"w0" = special code for first move in game + // At move 1, the subTurn doesn't need to be specified: + this.subTurn = fullTurn[1] || 1; } getPotentialPawnMoves([x, y]) { @@ -139,41 +135,76 @@ export const VariantRules = class MarseilleRules extends ChessRules { move.turn = this.turn + this.subTurn; V.PlayOnBoard(this.board, move); const epSq = this.getEpSquare(move); - if (this.subTurn == 0) { - //first move in game + if (this.movesCount == 0) { + // First move in game this.turn = "b"; - this.subTurn = 1; this.epSquares.push([epSq]); + this.movesCount = 1; } // Does this move give check on subturn 1? If yes, skip subturn 2 else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) { this.turn = V.GetOppCol(this.turn); this.epSquares.push([epSq]); move.checkOnSubturn1 = true; + this.movesCount++; } else { if (this.subTurn == 2) { this.turn = V.GetOppCol(this.turn); let lastEpsq = this.epSquares[this.epSquares.length - 1]; lastEpsq.push(epSq); - } else this.epSquares.push([epSq]); + } else { + this.epSquares.push([epSq]); + this.movesCount++; + } this.subTurn = 3 - this.subTurn; } - this.updateVariables(move); + this.postPlay(move); + } + + postPlay(move) { + const c = move.turn.charAt(0); + const piece = move.vanish[0].p; + const firstRank = c == "w" ? V.size.x - 1 : 0; + + if (piece == V.KING && move.appear.length > 0) { + this.kingPos[c][0] = move.appear[0].x; + this.kingPos[c][1] = move.appear[0].y; + if (V.HasCastle) this.castleFlags[c] = [V.size.y, V.size.y]; + return; + } + const oppCol = V.GetOppCol(c); + const oppFirstRank = V.size.x - 1 - firstRank; + if ( + move.start.x == firstRank && //our rook moves? + this.castleFlags[c].includes(move.start.y) + ) { + const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); + this.castleFlags[c][flagIdx] = V.size.y; + } else if ( + move.end.x == oppFirstRank && //we took opponent rook? + this.castleFlags[oppCol].includes(move.end.y) + ) { + const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); + this.castleFlags[oppCol][flagIdx] = V.size.y; + } } undo(move) { this.disaggregateFlags(JSON.parse(move.flags)); V.UndoOnBoard(this.board, move); - if (move.turn[1] == "0" || move.checkOnSubturn1 || this.subTurn == 2) + if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) { + // The move may not be full, but is fully undone: this.epSquares.pop(); - //this.subTurn == 1 - else { + // Moves counter was just incremented: + this.movesCount--; + } else { + // Undo the second half of a move let lastEpsq = this.epSquares[this.epSquares.length - 1]; lastEpsq.pop(); } this.turn = move.turn[0]; this.subTurn = parseInt(move.turn[1]); - this.unupdateVariables(move); + super.postUndo(move); } // NOTE: GenRandInitFen() is OK, @@ -192,8 +223,6 @@ export const VariantRules = class MarseilleRules extends ChessRules { // No alpha-beta here, just adapted min-max at depth 2(+1) getComputerMove() { - if (this.subTurn == 2) return null; //TODO: imperfect interface setup - const maxeval = V.INFINITY; const color = this.turn; const oppCol = V.GetOppCol(this.turn);