X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FEightpieces.js;h=42df93e3da04233415da1ed7e88150fc561d05d8;hb=7e476ce4b299c0a35529b80c730a452992453cc6;hp=a0eed50df110d36fc5045b729dcf5f85fe04c829;hpb=68e19a449db7a12e0a168e99cd750d985c983ba1;p=vchess.git diff --git a/client/src/variants/Eightpieces.js b/client/src/variants/Eightpieces.js index a0eed50d..42df93e3 100644 --- a/client/src/variants/Eightpieces.js +++ b/client/src/variants/Eightpieces.js @@ -2,7 +2,7 @@ import { ArrayFun } from "@/utils/array"; import { randInt } from "@/utils/alea"; import { ChessRules, PiPo, Move } from "@/base_rules"; -export const VariantRules = class EightpiecesRules extends ChessRules { +export class EightpiecesRules extends ChessRules { static get JAILER() { return "j"; } @@ -175,7 +175,8 @@ export const VariantRules = class EightpiecesRules extends ChessRules { if (c == 'b') { // Check if white sentry is on the same color as ours. // If yes: swap bishop and sentry positions. - if ((pieces['w'].indexOf('s') - sentryPos) % 2 == 0) + // NOTE: test % 2 == 1 because there are 7 slashes. + if ((pieces['w'].indexOf('s') - sentryPos) % 2 == 1) [bishopPos, sentryPos] = [sentryPos, bishopPos]; } positions.splice(Math.max(randIndex, randIndex_tmp), 1); @@ -195,7 +196,7 @@ export const VariantRules = class EightpiecesRules extends ChessRules { positions.splice(randIndex, 1); // Rook, jailer and king positions are now almost fixed, - // only the ordering rook-> jailer or jailer->rook must be decided. + // only the ordering rook->jailer or jailer->rook must be decided. let rookPos = positions[0]; let jailerPos = positions[2]; const kingPos = positions[1]; @@ -247,21 +248,23 @@ export const VariantRules = class EightpiecesRules extends ChessRules { // Because of the lancers, getPiece() could be wrong: // use board[x][y][1] instead (always valid). getBasicMove([sx, sy], [ex, ey], tr) { + const initColor = this.getColor(sx, sy); + const initPiece = this.board[sx][sy].charAt(1); let mv = new Move({ appear: [ new PiPo({ x: ex, y: ey, - c: tr ? tr.c : this.getColor(sx, sy), - p: tr ? tr.p : this.board[sx][sy].charAt(1) + c: tr ? tr.c : initColor, + p: tr ? tr.p : initPiece }) ], vanish: [ new PiPo({ x: sx, y: sy, - c: this.getColor(sx, sy), - p: this.board[sx][sy].charAt(1) + c: initColor, + p: initPiece }) ] }); @@ -608,81 +611,6 @@ export const VariantRules = class EightpiecesRules extends ChessRules { ); } - // Adapted: castle with jailer possible - getCastleMoves([x, y]) { - const c = this.getColor(x, y); - const firstRank = (c == "w" ? V.size.x - 1 : 0); - if (x != firstRank || y != this.INIT_COL_KING[c]) - return []; - - const oppCol = V.GetOppCol(c); - let moves = []; - let i = 0; - // King, then rook or jailer: - const finalSquares = [ - [2, 3], - [V.size.y - 2, V.size.y - 3] - ]; - castlingCheck: for ( - let castleSide = 0; - castleSide < 2; - castleSide++ - ) { - if (this.castleFlags[c][castleSide] >= 8) continue; - // Rook (or jailer) and king are on initial position - const finDist = finalSquares[castleSide][0] - y; - let step = finDist / Math.max(1, Math.abs(finDist)); - i = y; - do { - if ( - this.isAttacked([x, i], oppCol) || - (this.board[x][i] != V.EMPTY && - (this.getColor(x, i) != c || - ![V.KING, V.ROOK, V.JAILER].includes(this.getPiece(x, i)))) - ) { - continue castlingCheck; - } - i += step; - } while (i != finalSquares[castleSide][0]); - step = castleSide == 0 ? -1 : 1; - const rookOrJailerPos = this.castleFlags[c][castleSide]; - for (i = y + step; i != rookOrJailerPos; i += step) - if (this.board[x][i] != V.EMPTY) continue castlingCheck; - - // Nothing on final squares, except maybe king and castling rook or jailer? - for (i = 0; i < 2; i++) { - if ( - this.board[x][finalSquares[castleSide][i]] != V.EMPTY && - this.getPiece(x, finalSquares[castleSide][i]) != V.KING && - finalSquares[castleSide][i] != rookOrJailerPos - ) { - continue castlingCheck; - } - } - - // If this code is reached, castle is valid - const castlingPiece = this.getPiece(firstRank, rookOrJailerPos); - moves.push( - new Move({ - appear: [ - new PiPo({ x: x, y: finalSquares[castleSide][0], p: V.KING, c: c }), - new PiPo({ x: x, y: finalSquares[castleSide][1], p: castlingPiece, c: c }) - ], - vanish: [ - new PiPo({ x: x, y: y, p: V.KING, c: c }), - new PiPo({ x: x, y: rookOrJailerPos, p: castlingPiece, c: c }) - ], - end: - Math.abs(y - rookOrJailerPos) <= 2 - ? { x: x, y: rookOrJailerPos } - : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) } - }) - ); - } - - return moves; - } - atLeastOneMove() { // If in second-half of a move, we already know that a move is possible if (this.subTurn == 2) return true; @@ -762,10 +690,6 @@ export const VariantRules = class EightpiecesRules extends ChessRules { } play(move) { -// if (!this.states) this.states = []; -// const stateFen = this.getFen(); -// this.states.push(stateFen); - this.prePlay(move); move.flags = JSON.stringify(this.aggregateFlags()); this.epSquares.push(this.getEpSquare(move)); @@ -826,10 +750,6 @@ export const VariantRules = class EightpiecesRules extends ChessRules { if (move.sentryPush) this.subTurn = 2; } this.postUndo(move); - -// const stateFen = this.getFen(); -// if (stateFen != this.states[this.states.length-1]) debugger; -// this.states.pop(); } postUndo(move) { @@ -842,6 +762,7 @@ export const VariantRules = class EightpiecesRules extends ChessRules { super.isAttacked(sq, color) || this.isAttackedByLancer(sq, color) || this.isAttackedBySentry(sq, color) + // The jailer doesn't capture. ); } @@ -917,6 +838,7 @@ export const VariantRules = class EightpiecesRules extends ChessRules { // Helper to check sentries attacks: selfAttack([x1, y1], [x2, y2]) { const color = this.getColor(x1, y1); + const oppCol = V.GetOppCol(color); const sliderAttack = (allowedSteps, lancer) => { const deltaX = x2 - x1, absDeltaX = Math.abs(deltaX); @@ -935,7 +857,7 @@ export const VariantRules = class EightpiecesRules extends ChessRules { if ( // NOTE: no need to check OnBoard in this special case (!lancer && this.board[sq[0]][sq[1]] != V.EMPTY) || - (!!lancer && this.getColor(sq[0], sq[1]) != color) + (!!lancer && this.getColor(sq[0], sq[1]) == oppCol) ) { return false; } @@ -1109,6 +1031,13 @@ export const VariantRules = class EightpiecesRules extends ChessRules { if (Object.keys(V.LANCER_DIRNAMES).includes(move.vanish[0].p)) // Lancer: add direction info notation += "=" + V.LANCER_DIRNAMES[move.appear[0].p]; + else if ( + move.vanish[0].p == V.PAWN && + Object.keys(V.LANCER_DIRNAMES).includes(move.appear[0].p) + ) { + // Fix promotions in lancer: + notation = notation.slice(0, -1) + "L:" + V.LANCER_DIRNAMES[move.appear[0].p]; + } return notation; } };