X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=53393dfcea8cde3b7f0a9253ef4a9a7c6d20caf7;hb=b99ce1fb4539b6ac0afd686acc39e2776e7961b4;hp=79e8650bc5c57ed9e470acaa5c97fd38b6a6639f;hpb=b4ae3ff62916d54d56778f63ef0231bfb5761142;p=xogo.git diff --git a/base_rules.js b/base_rules.js index 79e8650..53393df 100644 --- a/base_rules.js +++ b/base_rules.js @@ -385,7 +385,7 @@ export default class ChessRules { const coords = Object.keys(this.ispawn); if (coords.length == 0) return "-"; - return coords.map(C.CoordsToSquare).join(","); + return coords.join(","); } // Set flags from fen (castle: white a,h then black a,h) @@ -755,10 +755,9 @@ export default class ChessRules { } else this.r_pieces = { 'w': {}, 'b': {} }; - let chessboard = - document.getElementById(this.containerId).querySelector(".chessboard"); + let container = document.getElementById(this.containerId); if (!r) - r = chessboard.getBoundingClientRect(); + r = container.querySelector(".chessboard").getBoundingClientRect(); for (let c of colors) { if (!this.reserve[c]) continue; @@ -777,7 +776,7 @@ export default class ChessRules { // NOTE: +1 fix display bug on Firefox at least rcontainer.style.width = (nbR * sqResSize + 1) + "px"; rcontainer.style.height = sqResSize + "px"; - chessboard.appendChild(rcontainer); + container.appendChild(rcontainer); for (let p of Object.keys(this.reserve[c])) { if (this.reserve[c][p] == 0) continue; @@ -993,7 +992,7 @@ export default class ChessRules { startPiece.style.opacity = "1"; const offset = getOffset(e); const landingElt = document.elementFromPoint(offset.x, offset.y); - const sq = this.idToCoords(landingElt.id); + const sq = landingElt ? this.idToCoords(landingElt.id) : undefined; if (sq) { const [i, j] = sq; // NOTE: clearly suboptimal, but much easier, and not a big deal. @@ -1103,7 +1102,8 @@ export default class ChessRules { // Is (x,y) on the chessboard? onBoard(x, y) { - return x >= 0 && x < this.size.x && y >= 0 && y < this.size.y; + return (x >= 0 && x < this.size.x && + y >= 0 && y < this.size.y); } // Used in interface: 'side' arg == player color @@ -1364,23 +1364,24 @@ export default class ChessRules { return moves; } + // NOTE: using special symbols to not interfere with variants' pieces codes static get CannibalKings() { return { - "s": "p", - "u": "r", - "o": "n", - "c": "b", - "t": "q" + "!": "p", + "#": "r", + "$": "n", + "%": "b", + "*": "q" }; } static get CannibalKingCode() { return { - "p": "s", - "r": "u", - "n": "o", - "b": "c", - "q": "t", + "p": "!", + "r": "#", + "n": "$", + "b": "%", + "q": "*", "k": "k" }; } @@ -2047,15 +2048,23 @@ export default class ChessRules { this.options["crazyhouse"] && (!this.options["rifle"] || !move.capture) ) { + const destSquare = C.CoordsToSquare(move.end); if (this.ispawn[initSquare]) { delete this.ispawn[initSquare]; - this.ispawn[C.CoordsToSquare(move.end)] = true; + this.ispawn[destSquare] = true; } else if ( move.vanish[0].p == "p" && move.appear[0].p != "p" ) { - this.ispawn[C.CoordsToSquare(move.end)] = true; + this.ispawn[destSquare] = true; + } + else if ( + this.ispawn[destSquare] && + this.getColor(move.end.x, move.end.y) != move.vanish[0].c + ) { + move.vanish[1].p = "p"; + delete this.ispawn[destSquare]; } } } @@ -2117,7 +2126,13 @@ export default class ChessRules { (this.options["progressive"] && this.subTurn <= this.movesCount) ) { const oppKingPos = this.searchKingPos(oppCol); - if (oppKingPos[0] >= 0 && !this.underCheck(oppKingPos, color)) { + if ( + oppKingPos[0] >= 0 && + ( + this.options["taking"] || + !this.underCheck(oppKingPos, color) + ) + ) { this.subTurn++; return; } @@ -2176,7 +2191,9 @@ export default class ChessRules { playVisual(move, r) { move.vanish.forEach(v => { if (!this.enlightened || this.enlightened[v.x][v.y]) { - this.g_pieces[v.x][v.y].remove(); + // TODO: next "if" shouldn't be required + if (this.g_pieces[v.x][v.y]) + this.g_pieces[v.x][v.y].remove(); this.g_pieces[v.x][v.y] = null; } }); @@ -2229,6 +2246,11 @@ export default class ChessRules { const dropMove = (typeof i1 == "string"); const startArray = (dropMove ? this.r_pieces : this.g_pieces); let startPiece = startArray[i1][j1]; + // TODO: next "if" shouldn't be required + if (!startPiece) { + callback(); + return; + } let chessboard = document.getElementById(this.containerId).querySelector(".chessboard"); const clonePiece = (