X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=b150fcb083fb2cf4edaa657bb2e6d2790535e656;hb=4bff03f5e53dbf11ed23210aba2ee873189e0889;hp=d743c0e27f8301928dca99454094827a5998385d;hpb=15106e8201ee8e46ce14f1dd369cae960e9a860c;p=xogo.git diff --git a/base_rules.js b/base_rules.js index d743c0e..b150fcb 100644 --- a/base_rules.js +++ b/base_rules.js @@ -118,13 +118,13 @@ export default class ChessRules { //////////////////// // COORDINATES UTILS - // a3 --> {x:10, y:3} + // 3a --> {x:3, y:10} static SquareToCoords(sq) { return ArrayFun.toObject(["x", "y"], [0, 1].map(i => parseInt(sq[i], 36))); } - // {x:1, y:12} --> 1c + // {x:11, y:12} --> bc static CoordsToSquare(cd) { return Object.values(cd).map(c => c.toString(36)).join(""); } @@ -162,12 +162,12 @@ export default class ChessRules { // Turn "wb" into "B" (for FEN) board2fen(b) { - return b[0] == "w" ? b[1].toUpperCase() : b[1]; + return (b[0] == "w" ? b[1].toUpperCase() : b[1]); } // Turn "p" into "bp" (for board) fen2board(f) { - return f.charCodeAt(0) <= 90 ? "w" + f.toLowerCase() : "b" + f; + return (f.charCodeAt(0) <= 90 ? "w" + f.toLowerCase() : "b" + f); } // Setup the initial random-or-not (asymmetric-or-not) position @@ -1352,7 +1352,6 @@ export default class ChessRules { const lastRank = (color == "w" ? 0 : this.size.x - 1); const initPiece = this.getPiece(moves[0].start.x, moves[0].start.y); moves.forEach(m => { - let finalPieces = ["p"]; const [x1, y1] = [m.start.x, m.start.y]; const [x2, y2] = [m.end.x, m.end.y]; const promotionOk = ( @@ -1361,34 +1360,30 @@ export default class ChessRules { ); if (!promotionOk) return; //nothing to do - if (!this.options["pawnfall"]) { - if ( - this.options["cannibal"] && - this.board[x2][y2] != "" && - this.getColor(x2, y2) == oppCol - ) { - finalPieces = [this.getPieceType(x2, y2)]; - } - else - finalPieces = this.pawnPromotions; + if (this.options["pawnfall"]) { + m.appear.shift(); + return; + } + let finalPieces = ["p"]; + if ( + this.options["cannibal"] && + this.board[x2][y2] != "" && + this.getColor(x2, y2) == oppCol + ) { + finalPieces = [this.getPieceType(x2, y2)]; } + else + finalPieces = this.pawnPromotions; m.appear[0].p = finalPieces[0]; if (initPiece == "!") //cannibal king-pawn m.appear[0].p = C.CannibalKingCode[finalPieces[0]]; for (let i=1; i { + return this.castleFlags[c].some(val => val < this.size.y)}) ) { - // OK, not a drop move - if ( - this.hasCastle && - // If flags already off, no need to re-check: - Object.keys(this.castleFlags).some(c => { - return this.castleFlags[c].some(val => val < this.size.y)}) - ) { - this.updateCastleFlags(move); - } - const initSquare = C.CoordsToSquare(move.start); - if ( - this.options["crazyhouse"] && - (!this.options["rifle"] || !move.capture) - ) { + this.updateCastleFlags(move); + } + if (this.options["crazyhouse"]) { + move.vanish.forEach(v => { + const square = C.CoordsToSquare({x: v.x, y: v.y}); + if (this.ispawn[square]) + delete this.ispawn[square]; + }); + if (move.appear.length > 0 && move.vanish.length > 0) { + // Assumption: something is moving + const initSquare = C.CoordsToSquare(move.start); const destSquare = C.CoordsToSquare(move.end); - if (this.ispawn[initSquare]) { - delete this.ispawn[initSquare]; - this.ispawn[destSquare] = true; - } - else if ( - move.vanish[0].p == "p" && - move.appear[0].p != "p" + if ( + this.ispawn[initSquare] || + (move.vanish[0].p == "p" && move.appear[0].p != "p") ) { this.ispawn[destSquare] = true; } @@ -1985,6 +1976,10 @@ export default class ChessRules { } } } + + // TODO: robustify this by adding fields + // "captures" (capts?) and "births" (e.g...) to Move + // --> store only indices in appear/vanish ? const minSize = Math.min(move.appear.length, move.vanish.length); if (this.hasReserve && !move.pawnfall) { const color = this.turn; @@ -2000,6 +1995,12 @@ export default class ChessRules { this.updateReserve(color, piece, this.reserve[color][piece] + 1); } } + move.captures.forEach(capt => { + // TODO + }); + move.births.forEach(bth => { + // TODO + }); } play(move) { @@ -2154,6 +2155,10 @@ export default class ChessRules { return; } let movingPiece = this.getDomPiece(move.start.x, move.start.y); + if (!movingPiece) { //TODO this shouldn't be required + callback(); + return; + } const initTransform = movingPiece.style.transform; let chessboard = document.getElementById(this.containerId).querySelector(".chessboard"); @@ -2180,24 +2185,22 @@ export default class ChessRules { const [i2, j2] = move.segments[index][1]; const dep = this.getPixelPosition(i1, j1, r); const arr = this.getPixelPosition(i2, j2, r); - // Start from i1, j1: - movingPiece.style.transform = - `translate(${dep[0] - ix}px, ${dep[1] - iy}px)`; - movingPiece.style.transitionDuration = "0s"; const distance = Math.sqrt((arr[0] - dep[0]) ** 2 + (arr[1] - dep[1]) ** 2); const duration = 0.2 + (distance / maxDist) * 0.3; - movingPiece.style.transform = - `translate(${arr[0] - dep[0]}px, ${arr[1] - dep[1]}px)`; + movingPiece.style.transform = `translate(${arr[0]}px, ${arr[1]}px)`; movingPiece.style.transitionDuration = duration + "s"; setTimeout(cb, duration * 1000); }; - if (!move.segments) - move.segments = [[move.start.x, move.start.y], [move.end.x, move.end.y]]; + if (!move.segments) { + move.segments = [ + [[move.start.x, move.start.y], [move.end.x, move.end.y]] + ]; + } let index = 0; - animateSegment(index, () => { + const animateSegmentCallback = () => { if (index < move.segments.length) - animateSegment(++index); + animateSegment(index++, animateSegmentCallback); else { if (move.drag) movingPiece.remove(); @@ -2207,7 +2210,8 @@ export default class ChessRules { } callback(); } - }); + }; + animateSegmentCallback(); } playReceivedMove(moves, callback) {