X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FBaroque.js;h=031ea72c8892f39f1c0731fe0b2d3bd92f2772e7;hb=0d5335de5c94d780e03ac0aa3279b731c69455cc;hp=5687e22c7416e760149eb18cefd4a2661d4de216;hpb=78d64531113d4b5045ff588dd43f301a332ebae8;p=vchess.git diff --git a/client/src/variants/Baroque.js b/client/src/variants/Baroque.js index 5687e22c..031ea72c 100644 --- a/client/src/variants/Baroque.js +++ b/client/src/variants/Baroque.js @@ -1,8 +1,8 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; import { ArrayFun } from "@/utils/array"; -import { randInt } from "@/utils/alea"; +import { shuffle } from "@/utils/alea"; -export const VariantRules = class BaroqueRules extends ChessRules { +export class BaroqueRules extends ChessRules { static get HasFlags() { return false; } @@ -11,17 +11,17 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } - static getPpath(b) { + static get PIECES() { + return ChessRules.PIECES.concat([V.IMMOBILIZER]); + } + + getPpath(b) { if (b[1] == "m") //'m' for Immobilizer (I is too similar to 1) return "Baroque/" + b; return b; //usual piece } - static get PIECES() { - return ChessRules.PIECES.concat([V.IMMOBILIZER]); - } - // No castling, but checks, so keep track of kings setOtherVariables(fen) { this.kingPos = { w: [-1, -1], b: [-1, -1] }; @@ -72,7 +72,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { ) { const oppPiece = this.getPiece(i, j); if (oppPiece == V.IMMOBILIZER) { - // Moving is impossible only if this immobilizer is not neutralized + // Moving is possible only if this immobilizer is neutralized for (let step2 of adjacentSteps) { const [i2, j2] = [i + step2[0], j + step2[1]]; if (i2 == x && j2 == y) continue; //skip initial piece! @@ -352,17 +352,9 @@ export const VariantRules = class BaroqueRules extends ChessRules { return super.getPotentialQueenMoves(sq); } - getPotentialKingMoves(sq) { - return this.getSlideNJumpMoves( - sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]), - "oneStep" - ); - } - // isAttacked() is OK because the immobilizer doesn't take - isAttackedByPawn([x, y], colors) { + isAttackedByPawn([x, y], color) { // Square (x,y) must be surroundable by two enemy pieces, // and one of them at least should be a pawn (moving). const dirs = [ @@ -375,12 +367,17 @@ export const VariantRules = class BaroqueRules extends ChessRules { const [i2, j2] = [x + dir[0], y + dir[1]]; //"after" if (V.OnBoard(i1, j1) && V.OnBoard(i2, j2)) { if ( - (this.board[i1][j1] != V.EMPTY && - colors.includes(this.getColor(i1, j1)) && - this.board[i2][j2] == V.EMPTY) || - (this.board[i2][j2] != V.EMPTY && - colors.includes(this.getColor(i2, j2)) && - this.board[i1][j1] == V.EMPTY) + ( + this.board[i1][j1] != V.EMPTY && + this.getColor(i1, j1) == color && + this.board[i2][j2] == V.EMPTY + ) + || + ( + this.board[i2][j2] != V.EMPTY && + this.getColor(i2, j2) == color && + this.board[i1][j1] == V.EMPTY + ) ) { // Search a movable enemy pawn landing on the empty square for (let step of steps) { @@ -392,7 +389,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { } if ( V.OnBoard(i3, j3) && - colors.includes(this.getColor(i3, j3)) && + this.getColor(i3, j3) == color && this.getPiece(i3, j3) == V.PAWN && !this.isImmobilized([i3, j3]) ) { @@ -405,19 +402,18 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } - isAttackedByRook([x, y], colors) { + isAttackedByRook([x, y], color) { // King must be on same column or row, // and a rook should be able to reach a capturing square - // colors contains only one element, giving the oppCol and thus king position - const sameRow = x == this.kingPos[colors[0]][0]; - const sameColumn = y == this.kingPos[colors[0]][1]; + const sameRow = x == this.kingPos[color][0]; + const sameColumn = y == this.kingPos[color][1]; if (sameRow || sameColumn) { // Look for the enemy rook (maximum 1) for (let i = 0; i < V.size.x; i++) { for (let j = 0; j < V.size.y; j++) { if ( this.board[i][j] != V.EMPTY && - colors.includes(this.getColor(i, j)) && + this.getColor(i, j) == color && this.getPiece(i, j) == V.ROOK ) { if (this.isImmobilized([i, j])) return false; //because only one rook @@ -438,7 +434,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } - isAttackedByKnight([x, y], colors) { + isAttackedByKnight([x, y], color) { // Square (x,y) must be on same line as a knight, // and there must be empty square(s) behind. const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); @@ -453,7 +449,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { j -= step[1]; } if (V.OnBoard(i, j)) { - if (colors.includes(this.getColor(i, j))) { + if (this.getColor(i, j) == color) { if ( this.getPiece(i, j) == V.KNIGHT && !this.isImmobilized([i, j]) @@ -473,16 +469,16 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } - isAttackedByBishop([x, y], colors) { - // We cheat a little here: since this function is used exclusively for king, - // it's enough to check the immediate surrounding of the square. + isAttackedByBishop([x, y], color) { + // We cheat a little here: since this function is used exclusively for + // the king, it's enough to check the immediate surrounding of the square. const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); for (let step of adjacentSteps) { const [i, j] = [x + step[0], y + step[1]]; if ( V.OnBoard(i, j) && this.board[i][j] != V.EMPTY && - colors.includes(this.getColor(i, j)) && + this.getColor(i, j) == color && this.getPiece(i, j) == V.BISHOP ) { return true; //bishops are never immobilized @@ -491,7 +487,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } - isAttackedByQueen([x, y], colors) { + isAttackedByQueen([x, y], color) { // Square (x,y) must be adjacent to a queen, and the queen must have // some free space in the opposite direction from (x,y) const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); @@ -501,7 +497,7 @@ export const VariantRules = class BaroqueRules extends ChessRules { const sq1 = [x + step[0], y + step[1]]; if ( this.board[sq1[0]][sq1[1]] != V.EMPTY && - colors.includes(this.getColor(sq1[0], sq1[1])) && + this.getColor(sq1[0], sq1[1]) == color && this.getPiece(sq1[0], sq1[1]) == V.QUEEN && !this.isImmobilized(sq1) ) { @@ -512,6 +508,23 @@ export const VariantRules = class BaroqueRules extends ChessRules { return false; } + isAttackedByKing([x, y], color) { + const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); + for (let step of steps) { + let rx = x + step[0], + ry = y + step[1]; + if ( + V.OnBoard(rx, ry) && + this.getPiece(rx, ry) === V.KING && + this.getColor(rx, ry) == color && + !this.isImmobilized([rx, ry]) + ) { + return true; + } + } + return false; + } + static get VALUES() { return { p: 1, @@ -528,50 +541,23 @@ export const VariantRules = class BaroqueRules extends ChessRules { return 2; } - static GenRandInitFen() { + static GenRandInitFen(randomness) { + if (randomness == 0) + // Deterministic: + return "rnbqkbnrm/pppppppp/8/8/8/8/PPPPPPPP/MNBKQBNR w 0"; + let pieces = { w: new Array(8), b: new Array(8) }; // Shuffle pieces on first and last rank for (let c of ["w", "b"]) { - let positions = ArrayFun.range(8); - // Get random squares for every piece, totally freely + if (c == 'b' && randomness == 1) { + pieces['b'] = pieces['w']; + break; + } - let randIndex = randInt(8); - const bishop1Pos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(7); - const bishop2Pos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(6); - const knight1Pos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(5); - const knight2Pos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(4); - const queenPos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(3); - const kingPos = positions[randIndex]; - positions.splice(randIndex, 1); - - randIndex = randInt(2); - const rookPos = positions[randIndex]; - positions.splice(randIndex, 1); - const immobilizerPos = positions[0]; - - pieces[c][bishop1Pos] = "b"; - pieces[c][bishop2Pos] = "b"; - pieces[c][knight1Pos] = "n"; - pieces[c][knight2Pos] = "n"; - pieces[c][queenPos] = "q"; - pieces[c][kingPos] = "k"; - pieces[c][rookPos] = "r"; - pieces[c][immobilizerPos] = "m"; + // Get random squares for every piece, totally freely + let positions = shuffle(ArrayFun.range(8)); + const composition = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'm']; + for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; } return ( pieces["b"].join("") + @@ -591,7 +577,8 @@ export const VariantRules = class BaroqueRules extends ChessRules { } else if (move.appear[0].p == V.KING) notation = "K" + (move.vanish.length > 1 ? "x" : "") + finalSquare; else notation = move.appear[0].p.toUpperCase() + finalSquare; - if (move.vanish.length > 1 && move.appear[0].p != V.KING) notation += "X"; //capture mark (not describing what is captured...) + // Add a capture mark (not describing what is captured...): + if (move.vanish.length > 1 && move.appear[0].p != V.KING) notation += "X"; return notation; } };