X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FSwitching.js;h=cc2febd1e646f64fbdec59e520e9a94fa7ee1243;hp=a7d678791e801f788d846ca77b19ccdf8d27f50c;hb=1221ac47836806efb287b0323b92957d9129c653;hpb=a6abf094c35a26019e47fea21302c4be32ff030b diff --git a/public/javascripts/variants/Switching.js b/public/javascripts/variants/Switching.js index a7d67879..cc2febd1 100644 --- a/public/javascripts/variants/Switching.js +++ b/public/javascripts/variants/Switching.js @@ -1,10 +1,73 @@ -//https://www.chessvariants.com/diffmove.dir/switching.html class SwitchingRules extends ChessRules { - //TODO: - // Move completion: promote switched pawns (as in Magnetic) + // Build switch move between squares x1,y1 and x2,y2 + getSwitchMove_s([x1,y1],[x2,y2]) + { - // To every piece potential moves: add switchings + const c = this.getColor(x1,y1); //same as color at square 2 + const p1 = this.getPiece(x1,y1); + const p2 = this.getPiece(x2,y2); + let move = new Move({ + appear: [ + new PiPo({x:x2,y:y2,c:c,p:p1}), + new PiPo({x:x1,y:y1,c:c,p:p2}) + ], + vanish: [ + new PiPo({x:x1,y:y1,c:c,p:p1}), + new PiPo({x:x2,y:y2,c:c,p:p2}) + ], + start: {x:x1,y:y1}, + end: {x:x2,y:y2} + }); + // Move completion: promote switched pawns (as in Magnetic) + const sizeX = VariantRules.size[0]; + const lastRank = (c == "w" ? 0 : sizeX-1); + const V = VariantRules; + let moves = []; + if (p1==V.PAWN && x2==lastRank) //TODO: also the case p2==V.PAWN and x1==lastRank! see Magnetic chess + { + move.appear[0].p = V.ROOK; + moves.push(move); + for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN]) + { + let cmove = JSON.parse(JSON.stringify(move)); + cmove.appear[0].p = piece; + moves.push(cmove); + } + } + else //other cases + moves.push(move); + return moves; + } - // Prevent king switching if under check + getPotentialMovesFrom([x,y]) + { + let moves = super.getPotentialMovesFrom([x,y]); + // Add switches: + const V = VariantRules; + const color = this.turn; + const piece = this.getPiece(x,y); + const [sizeX,sizeY] = V.size; + const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); + const kp = this.kingPos[color]; + const oppCol = this.getOppCol(color); + for (let step of steps) + { + let [i,j] = [x+step[0],y+step[1]]; + if (i>=0 && i=0 && j