$.getElementById("selectColor").selectedIndex = 0;
toggleVisible("newGameForm");
import(`/variants/${vname}/class.js`).then(module => {
- const Rules = module.default;
- prepareOptions(Rules);
+ window.V = module.default;
+ prepareOptions();
});
}
}
}
let options;
-function prepareOptions(Rules) {
+function prepareOptions() {
options = {};
- let optHtml = Rules.Options.select.map(select => { return `
+ let optHtml = V.Options.select.map(select => { return `
<div class="option-select">
<label for="var_${select.variable}">${select.label}</label>
<div class="select">
</div>
</div>`;
}).join("");
- optHtml += Rules.Options.check.map(check => {
+ optHtml += V.Options.check.map(check => {
return `
<div class="option-check">
<label class="checkbox">
</label>
</div>`;
}).join("");
- if (Rules.Options.styles.length >= 1) {
+ if (V.Options.styles.length >= 1) {
optHtml += '<div class="words">';
let i = 0;
- const stylesLength = Rules.Options.styles.length;
+ const stylesLength = V.Options.styles.length;
while (i < stylesLength) {
optHtml += '<div class="row">';
for (let j=i; j<i+4; j++) {
if (j == stylesLength) break;
- const style = Rules.Options.styles[j];
+ const style = V.Options.styles[j];
optHtml +=
`<span onClick="toggleStyle(event, '${style}')">${style}</span>`;
}
for (const select of $.querySelectorAll("#gameOptions select")) {
let value = select.value;
if (select.attributes["data-numeric"]) value = parseInt(value, 10);
- options[ select.id.split("_")[1] ] = value;
+ if (value) options[ select.id.split("_")[1] ] = value;
+ }
+ for (const check of $.querySelectorAll("#gameOptions input")) {
+ if (check.checked) options[ check.id.split("_")[1] ] = check.checked;
}
- for (const check of $.querySelectorAll("#gameOptions input"))
- options[ check.id.split("_")[1] ] = check.checked;
send("creategame", {
vname: vname,
player: { sid: sid, name: localStorage.getItem("name"), color: color },
function initializeGame(obj) {
const options = obj.options || {};
import(`/variants/${obj.vname}/class.js`).then(module => {
- const Rules = module.default;
+ window.V = module.default;
conditionalLoadCSS(obj.vname);
playerColor = (sid == obj.players[0].sid ? "w" : "b");
// Init + remove potential extra DOM elements from a previous game:
document.getElementById("boardContainer").innerHTML = `
<div id="upLeftInfos"
onClick="toggleGameInfos()">
- <img src="/assets/icon_infos.svg"/>
+ <svg version="1.1"
+ viewBox="0.5 0.5 100 100">
+ <g>
+ <path d="M50.5,0.5c-27.614,0-50,22.386-50,50c0,27.614,22.386,50,50,50s50-22.386,50-50C100.5,22.886,78.114,0.5,50.5,0.5z M60.5,85.5h-20v-40h20V85.5z M50.5,35.5c-5.523,0-10-4.477-10-10s4.477-10,10-10c5.522,0,10,4.477,10,10S56.022,35.5,50.5,35.5z"/>
+ </g>
+ </svg>
</div>
<div id="upRightStop"
onClick="confirmStopGame()">
- <img src="/assets/icon_close.svg"/>
+ <svg version="1.1"
+ viewBox="0 0 533.333 533.333">
+ <g>
+ <path d="M528.468,428.468c-0.002-0.002-0.004-0.004-0.006-0.005L366.667,266.666l161.795-161.797 c0.002-0.002,0.004-0.003,0.006-0.005c1.741-1.742,3.001-3.778,3.809-5.946c2.211-5.925,0.95-12.855-3.814-17.62l-76.431-76.43 c-4.765-4.763-11.694-6.024-17.619-3.812c-2.167,0.807-4.203,2.066-5.946,3.807c0,0.002-0.002,0.003-0.005,0.005L266.667,166.666 L104.87,4.869c-0.002-0.002-0.003-0.003-0.005-0.005c-1.743-1.74-3.778-3-5.945-3.807C92.993-1.156,86.065,0.105,81.3,4.869 L4.869,81.3c-4.764,4.765-6.024,11.694-3.813,17.619c0.808,2.167,2.067,4.205,3.808,5.946c0.002,0.001,0.003,0.003,0.005,0.005 l161.797,161.796L4.869,428.464c-0.001,0.002-0.003,0.003-0.004,0.005c-1.741,1.742-3,3.778-3.809,5.945 c-2.212,5.924-0.951,12.854,3.813,17.619L81.3,528.464c4.766,4.765,11.694,6.025,17.62,3.813c2.167-0.809,4.203-2.068,5.946-3.809 c0.001-0.002,0.003-0.003,0.005-0.005l161.796-161.797l161.795,161.797c0.003,0.001,0.005,0.003,0.007,0.004 c1.743,1.741,3.778,3.001,5.944,3.81c5.927,2.212,12.856,0.951,17.619-3.813l76.43-76.432c4.766-4.765,6.026-11.696,3.815-17.62 C531.469,432.246,530.209,430.21,528.468,428.468z"/>
+ </g>
+ </svg>
</div>
<div class="resizeable" id="chessboard"></div>`;
- vr = new Rules({
+ vr = new V({
seed: obj.seed, //may be null if FEN already exists (running game)
fen: obj.fen,
element: "chessboard",
import Move from "/utils/Move.js";
// NOTE: x coords: top to bottom (white perspective); y: left to right
+// NOTE: ChessRules is aliased as window.C, and variants as window.V
export default class ChessRules {
/////////////////////////
doClick([x, y]) {
if (typeof x != "number") return null; //click on reserves
if (
- this.options["teleport"] && this.subTurn == 2 &&
+ this.options["teleport"] && this.subTurnTeleport == 2 &&
this.board[x][y] == ""
) {
return new Move({
// a2 --> {x:2,y:0} (this is in fact a6)
static SquareToCoords(sq) {
return {
- x: ChessRules.RowToCoord(sq[1]),
+ x: C.RowToCoord(sq[1]),
// NOTE: column is always one char => max 26 columns
- y: ChessRules.ColumnToCoord(sq[0])
+ y: C.ColumnToCoord(sq[0])
};
}
// {x:0,y:4} --> e0 (should be e8)
static CoordsToSquare(coords) {
- return (
- ChessRules.CoordToColumn(coords.y) + ChessRules.CoordToRow(coords.x)
- );
+ return C.CoordToColumn(coords.y) + C.CoordToRow(coords.x);
}
coordsToId([x, y]) {
Random.setSeed(seed);
let fen, flags = "0707";
- if (this.options.randomness == 0 || !this.options.randomness)
+ if (!this.options.randomness)
// Deterministic:
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0";
// Enpassant part of the FEN string
getEnpassantFen() {
if (!this.epSquare) return "-"; //no en-passant
- return ChessRules.CoordsToSquare(this.epSquare);
+ return C.CoordsToSquare(this.epSquare);
}
getReserveFen() {
getIspawnFen() {
const coords = Object.keys(this.ispawn);
if (coords.length == 0) return "-";
- return coords.map(ChessRules.CoordsToSquare).join(",");
+ return coords.map(C.CoordsToSquare).join(",");
}
// Set flags from fen (castle: white a,h then black a,h)
// Fen string fully describes the game state
constructor(o) {
+ window.C = ChessRules; //easier alias
+
this.options = o.options;
this.playerColor = o.color;
this.afterPlay = o.afterPlay;
if (this.hasReserve) this.initReserves(fenParsed.reserve);
if (this.options["crazyhouse"]) this.initIspawn(fenParsed.ispawn);
this.subTurn = 1; //may be unused
- if (this.options["teleport"]) this.captured = null;
+ if (this.options["teleport"]) {
+ this.subTurnTeleport = 1;
+ this.captured = null;
+ }
if (this.options["dark"]) {
this.enlightened = ArrayFun.init(this.size.x, this.size.y);
// Setup enlightened: squares reachable by player side
if (this.board[x][y] != "" && this.getColor(x, y) == this.playerColor)
{
newEnlightened[x][y] = true;
- const piece = this.getPiece(x, y);
- if (piece == ChessRules.PAWN) {
+ if (this.getPiece(x, y) == "p") {
// Attacking squares wouldn't be highlighted if no captures:
- this.pieces(this.playerColor)[piece].attack.forEach(step => {
+ this.pieces(this.playerColor)["p"].attack.forEach(step => {
const [i, j] = [x + step[0], this.computeY(y + step[1])];
if (this.onBoard(i, j) && this.board[i][j] == "")
newEnlightened[i][j] = true;
// Include en-passant capturing square if any:
enlightEnpassant(newEnlightened) {
- const steps = this.pieces(this.playerColor)[ChessRules.PAWN].attack;
+ const steps = this.pieces(this.playerColor)["p"].attack;
for (let step of steps) {
const x = this.epSquare.x - step[0],
y = this.computeY(this.epSquare.y - step[1]);
if (
this.onBoard(x, y) &&
this.getColor(x, y) == this.playerColor &&
- this.getPiece(x, y) == ChessRules.PAWN
+ this.getPieceType(x, y) == "p"
) {
newEnlightened[x][this.epSquare.y] = true;
break;
let elt = document.getElementById(this.coordsToId([x, y]));
elt.classList.remove("in-shadow");
if (this.board[x][y] != "") {
- const piece = this.getPiece(x, y);
- const color = this.getColor(x, y);
+ const color = this.getColor(i, j);
+ const piece = this.getPiece(i, j);
this.g_pieces[x][y] = document.createElement("piece");
let newClasses = [
this.pieces()[piece]["class"],
initIspawn(ispawnStr) {
if (ispawnStr != "-") {
- this.ispawn = ispawnStr.split(",").map(ChessRules.SquareToCoords)
+ this.ispawn = ispawnStr.split(",").map(C.SquareToCoords)
.reduce((o, key) => ({ ...o, [key]: true}), {});
}
else this.ispawn = {};
re_drawBoardElements() {
const board = this.getSvgChessboard();
- const oppCol = ChessRules.GetOppCol(this.playerColor);
+ const oppCol = C.GetOppCol(this.playerColor);
let container = document.getElementById(this.containerId);
container.innerHTML = "";
container.insertAdjacentHTML('beforeend', board);
return board;
}
- // Generally light square bottom-right; TODO: user-defined colors at least
+ // Generally light square bottom-right
getSquareColorClass(i, j) {
return ((i+j) % 2 == 0 ? "light-square": "dark-square");
}
this.board[i][j] != "" &&
(!this.options["dark"] || this.enlightened[i][j])
) {
- const piece = this.getPiece(i, j);
const color = this.getColor(i, j);
+ const piece = this.getPiece(i, j);
this.g_pieces[i][j] = document.createElement("piece");
this.g_pieces[i][j].classList.add(this.pieces()[piece]["class"]);
this.g_pieces[i][j].classList.add(color == "w" ? "white" : "black");
}
updateReserve(color, piece, count) {
+ if (this.options["cannibal"] && C.CannibalKing[piece])
+ piece = "k"; //capturing cannibal king: back to king form
const oldCount = this.reserve[color][piece];
this.reserve[color][piece] = count;
// Redrawing is much easier if count==0
return this.board[i][j].charAt(0);
}
- // Piece type on square (i,j). 'undefined' if square is empty
+ // Assume square i,j isn't empty
getPiece(i, j) {
return this.board[i][j].charAt(1);
}
+ // Piece type on square (i,j)
+ getPieceType(i, j) {
+ const p = this.board[i][j].charAt(1);
+ return C.CannibalKings[p] || p; //a cannibal king move as...
+ }
+
// Get opponent color
static GetOppCol(color) {
return (color == "w" ? "b" : "w");
// Can thing on square1 take thing on square2
canTake([x1, y1], [x2, y2]) {
return (
+ (this.getColor(x1, y1) !== this.getColor(x2, y2)) ||
(
(this.options["recycle"] || this.options["teleport"]) &&
- this.getPiece(x2, y2) != ChessRules.KING
- ) ||
- (this.getColor(x1, y1) !== this.getColor(x2, y2))
+ this.getPieceType(x2, y2) != "k"
+ )
);
}
[1, 1], [1, -1], [-1, 1], [-1, -1]
],
range: 1
- }
+ },
+ // Cannibal kings:
+ 's': { "class": "king-pawn" },
+ 'u': { "class": "king-rook" },
+ 'o': { "class": "king-knight" },
+ 'c': { "class": "king-bishop" },
+ 't': { "class": "king-queen" }
};
}
- // Some pieces codes (for a clearer code)
- static get PAWN() {
- return "p";
- }
- static get QUEEN() {
- return "q";
- }
- static get KING() {
- return "k";
- }
-
////////////////////
// MOVES GENERATION
// Stop at the first capture found
atLeastOneCapture(color) {
color = color || this.turn;
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
for (let i = 0; i < this.size.x; i++) {
for (let j = 0; j < this.size.y; j++) {
if (this.board[i][j] != "" && this.getColor(i, j) == color) {
- const specs = this.pieces(color)[this.getPiece(i, j)];
+ const specs = this.pieces(color)[this.getPieceType(i, j)];
const steps = specs.attack || specs.steps;
outerLoop: for (let step of steps) {
let [ii, jj] = [i + step[0], this.computeY(j + step[1])];
getDropMovesFrom([c, p]) {
// NOTE: by design, this.reserve[c][p] >= 1 on user click
// (but not necessarily otherwise)
- if (!this.reserve[c][p] || this.reserve[c][p] == 0) return [];
+ if (this.reserve[c][p] == 0) return [];
let moves = [];
for (let i=0; i<this.size.x; i++) {
for (let j=0; j<this.size.y; j++) {
this.board[i][j] == "" &&
(!this.options["dark"] || this.enlightened[i][j]) &&
(
- p != ChessRules.PAWN ||
+ p != "p" ||
(c == 'w' && i < this.size.x - 1) ||
(c == 'b' && i > 0)
)
getPotentialMovesFrom(sq, color) {
if (typeof sq[0] == "string") return this.getDropMovesFrom(sq);
if (this.options["madrasi"] && this.isImmobilized(sq)) return [];
- const piece = this.getPiece(sq[0], sq[1]);
+ const piece = this.getPieceType(sq[0], sq[1]);
let moves;
- if (piece == ChessRules.PAWN) moves = this.getPotentialPawnMoves(sq);
+ if (piece == "p") moves = this.getPotentialPawnMoves(sq);
else moves = this.getPotentialMovesOf(piece, sq);
if (
- piece == ChessRules.KING &&
+ piece == "k" &&
this.hasCastle &&
this.castleFlags[color || this.turn].some(v => v < this.size.y)
) {
postProcessPotentialMoves(moves) {
if (moves.length == 0) return [];
const color = this.getColor(moves[0].start.x, moves[0].start.y);
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
if (this.options["capture"] && this.atLeastOneCapture()) {
// Filter out non-capturing moves (not using m.vanish because of
if (
this.onBoard(x, y) &&
this.board[x][y] != "" &&
- this.getPiece(x, y) != ChessRules.PAWN
+ this.getPieceType(x, y) != "p"
) {
m.vanish.push(
new PiPo({
}
});
}
+
+ if (
+ this.options["cannibal"] &&
+ this.options["rifle"] &&
+ this.pawnSpecs.promotions
+ ) {
+ // In this case a rifle-capture from last rank may promote a pawn
+ const lastRank = (color == "w" ? 0 : this.size.x - 1);
+ let newMoves = [];
+ moves.forEach(m => {
+ if (
+ m.start.x == lastRank &&
+ m.appear.length >= 1 &&
+ m.appear[0].p == "p" &&
+ m.appear[0].x == m.start.x &&
+ m.appear[0].y == m.start.y
+ ) {
+ const promotionPiece0 = this.pawnSpecs.promotions[0];
+ m.appear[0].p = this.pawnSpecs.promotions[0];
+ for (let i=1; i<this.pawnSpecs.promotions.length; i++) {
+ let newMv = JSON.parse(JSON.stringify(m));
+ newMv.appear[0].p = this.pawnSpecs.promotions[i];
+ newMoves.push(newMv);
+ }
+ }
+ });
+ Array.prototype.push.apply(moves, newMoves);
+ }
+
return moves;
}
+ static get CannibalKings() {
+ return {
+ "s": "p",
+ "u": "r",
+ "o": "n",
+ "c": "b",
+ "t": "q"
+ };
+ }
+
+ static get CannibalKingCode() {
+ return {
+ "p": "s",
+ "r": "u",
+ "n": "o",
+ "b": "c",
+ "q": "t",
+ "k": "k"
+ };
+ }
+
+ isKing(symbol) {
+ return (
+ symbol == 'k' ||
+ (this.options["cannibal"] && C.CannibalKings[symbol])
+ );
+ }
+
// For Madrasi:
// (redefined in Baroque etc, where Madrasi condition doesn't make sense)
isImmobilized([x, y]) {
const color = this.getColor(x, y);
- const oppCol = ChessRules.GetOppCol(color);
- const piece = this.getPiece(x, y);
+ const oppCol = C.GetOppCol(color);
+ const piece = this.getPieceType(x, y);
const stepSpec = this.pieces(color)[piece];
let [steps, range] = [stepSpec.attack || stepSpec.steps, stepSpec.range];
outerLoop: for (let step of steps) {
if (
this.onBoard(i, j) &&
this.getColor(i, j) == oppCol &&
- this.getPiece(i, j) == piece
+ this.getPieceType(i, j) == piece
) {
return true;
}
this.onBoard(i, j) &&
(
!this.options["zen"] ||
- this.getPiece(i, j) == ChessRules.KING ||
+ this.getPieceType(i, j) == "k" ||
this.getColor(i, j) == color //OK for Recycle and Teleport
) &&
this.canTake([x, y], [i, j]) &&
let moves = [];
// Find reverse captures (opponent takes)
const color = this.getColor(x, y);
- const oppCol = ChessRules.GetOppCol(color);
+ const pieceType = this.getPieceType(x, y);
+ const oppCol = C.GetOppCol(color);
const pieces = this.pieces(oppCol);
Object.keys(pieces).forEach(p => {
- if (p == ChessRules.KING) return; //king isn't captured this way
+ if (
+ p == "k" ||
+ (this.options["cannibal"] && C.CannibalKings[p])
+ ) {
+ return; //king isn't captured this way
+ }
const steps = pieces[p].attack || pieces[p].steps;
const range = pieces[p].range;
steps.forEach(s => {
}
if (
this.onBoard(i, j) &&
- this.getPiece(i, j) == p &&
+ this.getPieceType(i, j) == p &&
this.getColor(i, j) == oppCol && //condition for Recycle & Teleport
this.canTake([i, j], [x, y])
) {
- if (this.getPiece(x, y) != ChessRules.PAWN)
- moves.push(this.getBasicMove([x, y], [i, j]));
+ if (pieceType != "p") moves.push(this.getBasicMove([x, y], [i, j]));
else this.addPawnMoves([x, y], [i, j], moves);
}
});
// tr: transformation
getBasicMove([sx, sy], [ex, ey], tr) {
const initColor = this.getColor(sx, sy);
- const initPiece = this.board[sx][sy].charAt(1);
+ const initPiece = this.getPiece(sx, sy);
const destColor = (this.board[ex][ey] != "" ? this.getColor(ex, ey) : "");
let mv = new Move({
appear: [],
x: ex,
y: ey,
c: this.getColor(ex, ey),
- p: this.board[ex][ey].charAt(1)
+ p: this.getPiece(ex, ey)
})
);
if (this.options["rifle"])
mv.capture = true;
if (this.options["cannibal"] && destColor != initColor) {
const lastIdx = mv.vanish.length - 1;
- if (mv.appear.length >= 1) mv.appear[0].p = mv.vanish[lastIdx].p;
+ let trPiece = mv.vanish[lastIdx].p;
+ if (this.isKing(this.getPiece(sx, sy)))
+ trPiece = C.CannibalKingCode[trPiece];
+ if (mv.appear.length >= 1) mv.appear[0].p = trPiece;
else if (this.options["rifle"]) {
mv.appear.unshift(
new PiPo({
x: sx,
y: sy,
c: initColor,
- p: mv.vanish[lastIdx].p
+ p: trPiece
})
);
mv.vanish.unshift(
if (typeof moveOrSquare === "string") {
const square = moveOrSquare;
if (square == "-") return undefined;
- return ChessRules.SquareToCoords(square);
+ return C.SquareToCoords(square);
}
// Argument is a move:
const move = moveOrSquare;
s.y == e.y &&
Math.abs(s.x - e.x) == 2 &&
// Next conditions for variants like Atomic or Rifle, Recycle...
- (move.appear.length > 0 && move.appear[0].p == ChessRules.PAWN) &&
- (move.vanish.length > 0 && move.vanish[0].p == ChessRules.PAWN)
+ (move.appear.length > 0 && move.appear[0].p == "p") &&
+ (move.vanish.length > 0 && move.vanish[0].p == "p")
) {
return {
x: (s.x + e.x) / 2,
// Special case of en-passant captures: treated separately
getEnpassantCaptures([x, y], shiftX) {
const color = this.getColor(x, y);
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
let enpassantMove = null;
if (
!!this.epSquare &&
// Consider all potential promotions:
addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
- let finalPieces = [ChessRules.PAWN];
+ let finalPieces = ["p"];
const color = this.getColor(x1, y1);
const lastRank = (color == "w" ? 0 : this.size.x - 1);
if (x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == ""))
finalPieces = this.pawnSpecs.promotions;
}
for (let piece of finalPieces) {
- const tr = (piece != ChessRules.PAWN ? { c: color, p: piece } : null);
+ const tr = (piece != "p" ? { c: color, p: piece } : null);
moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
}
}
this.canTake([x, y], [x + shiftX, yCoord]) &&
(
!this.options["zen"] ||
- this.getPiece(x + shiftX, yCoord) == ChessRules.KING
+ this.getPieceType(x + shiftX, yCoord) == "k"
)
) {
this.addPawnMoves(
this.canTake([x, y], [x - shiftX, yCoord]) &&
(
!this.options["zen"] ||
- this.getPiece(x + shiftX, yCoord) == ChessRules.KING
+ this.getPieceType(x + shiftX, yCoord) == "k"
)
) {
this.addPawnMoves(
const c = this.getColor(x, y);
// Castling ?
- const oppCol = ChessRules.GetOppCol(c);
+ const oppCol = C.GetOppCol(c);
let moves = [];
// King, then rook:
finalSquares =
finalSquares || [ [2, 3], [this.size.y - 2, this.size.y - 3] ];
- const castlingKing = this.board[x][y].charAt(1);
+ const castlingKing = this.getPiece(x, y);
castlingCheck: for (
let castleSide = 0;
castleSide < 2;
// NOTE: in some variants this is not a rook
const rookPos = this.castleFlags[c][castleSide];
- const castlingPiece = this.board[x][rookPos].charAt(1);
+ const castlingPiece = this.getPiece(x, rookPos);
if (
this.board[x][rookPos] == "" ||
this.getColor(x, rookPos) != c ||
// Is (king at) given position under check by "color" ?
underCheck([x, y], color) {
if (this.taking || this.options["dark"]) return false;
- color = color || ChessRules.GetOppCol(this.getColor(x, y));
+ color = color || C.GetOppCol(this.getColor(x, y));
const pieces = this.pieces(color);
return Object.keys(pieces).some(p => {
return this.isAttackedBy([x, y], p, color, pieces[p]);
isAttackedBy([x, y], piece, color, stepSpec) {
const steps = stepSpec.attack || stepSpec.steps;
+ if (!steps) return false; //cannibal king, for example
const range = stepSpec.range;
let explored = {}; //for Cylinder mode
outerLoop: for (let step of steps) {
if (
this.onBoard(rx, ry) &&
this.board[rx][ry] != "" &&
- this.getPiece(rx, ry) == piece &&
+ this.getPieceType(rx, ry) == piece &&
this.getColor(rx, ry) == color &&
(!this.options["madrasi"] || !this.isImmobilized([rx, ry]))
) {
searchKingPos(color) {
for (let i=0; i < this.size.x; i++) {
for (let j=0; j < this.size.y; j++) {
- if (this.board[i][j] == color + 'k') return [i, j];
+ if (this.getColor(i, j) == color && this.isKing(this.getPiece(i, j)))
+ return [i, j];
}
}
return [-1, -1]; //king not found
filterValid(moves) {
if (moves.length == 0) return [];
const color = this.turn;
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
if (this.options["balance"] && [1, 3].includes(this.movesCount)) {
// Forbid moves either giving check or exploding opponent's king:
const oppKingPos = this.searchKingPos(oppCol);
moves = moves.filter(m => {
if (
- m.vanish.some(v => v.c == oppCol && v.p == ChessRules.KING) &&
- m.appear.every(a => a.c != oppCol || a.p != ChessRUles.KING)
+ m.vanish.some(v => v.c == oppCol && v.p == "k") &&
+ m.appear.every(a => a.c != oppCol || a.p != "k")
)
return false;
this.playOnBoard(m);
this.playOnBoard(m);
let square = kingPos,
res = true; //a priori valid
- if (m.vanish.some(v => v.p == ChessRules.KING && v.c == color)) {
+ if (m.vanish.some(v => {
+ return (v.p == "k" || C.CannibalKings[v.p]) && v.c == color;
+ })) {
// Search king in appear array:
const newKingIdx =
- m.appear.findIndex(a => a.p == ChessRules.KING && a.c == color);
+ m.appear.findIndex(a => {
+ return (a.p == "k" || C.CannibalKings[a.p]) && a.c == color;
+ });
if (newKingIdx >= 0)
square = [m.appear[newKingIdx].x, m.appear[newKingIdx].y];
else res = false;
move.appear.concat(move.vanish).forEach(psq => {
if (
this.board[psq.x][psq.y] != "" &&
- this.getPiece(psq.x, psq.y) == ChessRules.KING
+ this.getPieceType(psq.x, psq.y) == "k"
) {
this.castleFlags[psq.c] = [this.size.y, this.size.y];
}
// NOTE: not "else if" because king can capture enemy rook...
- let c = '';
- if (psq.x == 0) c = 'b';
- else if (psq.x == this.size.x - 1) c = 'w';
- if (c != '') {
+ let c = "";
+ if (psq.x == 0) c = "b";
+ else if (psq.x == this.size.x - 1) c = "w";
+ if (c != "") {
const fidx = this.castleFlags[c].findIndex(f => f == psq.y);
if (fidx >= 0) this.castleFlags[c][fidx] = this.size.y;
}
prePlay(move) {
if (
typeof move.start.x == "number" &&
- (!this.options["teleport"] || this.subTurn == 1)
+ (!this.options["teleport"] || this.subTurnTeleport == 1)
) {
// OK, not a drop move
if (
) {
this.updateCastleFlags(move);
}
- const initSquare = ChessRules.CoordsToSquare(move.start);
+ const initSquare = C.CoordsToSquare(move.start);
if (
this.options["crazyhouse"] &&
(!this.options["rifle"] || !move.capture)
) {
if (this.ispawn[initSquare]) {
delete this.ispawn[initSquare];
- this.ispawn[ChessRules.CoordsToSquare(move.end)] = true;
+ this.ispawn[C.CoordsToSquare(move.end)] = true;
}
else if (
- move.vanish[0].p == ChessRules.PAWN &&
- move.appear[0].p != ChessRules.PAWN
+ move.vanish[0].p == "p" &&
+ move.appear[0].p != "p"
) {
- this.ispawn[ChessRules.CoordsToSquare(move.end)] = true;
+ this.ispawn[C.CoordsToSquare(move.end)] = true;
}
}
}
postPlay(move) {
const color = this.turn;
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
if (this.options["dark"]) this.updateEnlightened(true);
if (this.options["teleport"]) {
if (
- this.subTurn == 1 &&
+ this.subTurnTeleport == 1 &&
move.vanish.length > move.appear.length &&
move.vanish[move.vanish.length - 1].c == color
) {
const v = move.vanish[move.vanish.length - 1];
this.captured = {x: v.x, y: v.y, c: v.c, p: v.p};
- this.subTurn = 2;
+ this.subTurnTeleport = 2;
return;
}
+ this.subTurnTeleport = 1;
this.captured = null;
}
if (this.options["balance"]) {
for (let i = 0; i < this.size.x; i++) {
for (let j = 0; j < this.size.y; j++) {
if (this.board[i][j] != "" && this.getColor(i, j) == color) {
- // TODO?: do not search all possible moves here
+ // NOTE: in fact searching for all potential moves from i,j.
+ // I don't believe this is an issue, for now at least.
const moves = this.getPotentialMovesFrom([i, j]);
if (moves.some(m => this.filterValid([m]).length >= 1)) return true;
}
// What is the score ? (Interesting if game is over)
getCurrentScore(move) {
const color = this.turn;
- const oppCol = ChessRules.GetOppCol(color);
+ const oppCol = C.GetOppCol(color);
const kingPos = [this.searchKingPos(color), this.searchKingPos(oppCol)];
if (kingPos[0][0] < 0 && kingPos[1][0] < 0) return "1/2";
if (kingPos[0][0] < 0) return (color == "w" ? "0-1" : "1-0");
return (color == "w" ? "0-1" : "1-0");
}
- // NOTE: quite suboptimal for eg. Benedict (TODO?)
+ // NOTE: quite suboptimal for eg. Benedict (not a big deal I think)
playVisual(move, r) {
move.vanish.forEach(v => {
if (!this.enlightened || this.enlightened[v.x][v.y]) {
const clonePiece = (
!dropMove &&
this.options["rifle"] ||
- (this.options["teleport"] && this.subTurn == 2)
+ (this.options["teleport"] && this.subTurnTeleport == 2)
);
if (clonePiece) {
startPiece = startPiece.cloneNode();
if (this.options["rifle"]) startArray[i1][j1].style.opacity = "0";
- if (this.options["teleport"] && this.subTurn == 2) {
+ if (this.options["teleport"] && this.subTurnTeleport == 2) {
const pieces = this.pieces();
const startCode = (dropMove ? j1 : this.getPiece(i1, j1));
startPiece.classList.remove(pieces[startCode]["class"]);