X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMagnetic.js;h=d5731a63c0d70e4c8a5071575e1ca01c80cbe998;hp=4151f9adf5d0e68178c28ded313829ac2120d7e6;hb=2526c041baf44968b0aa7b98af56730e88f6a595;hpb=43c0f1d8ead5eb867a78ad00c0983d70e07fe4bb diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js index 4151f9ad..d5731a63 100644 --- a/public/javascripts/variants/Magnetic.js +++ b/public/javascripts/variants/Magnetic.js @@ -5,10 +5,24 @@ class MagneticRules extends ChessRules return undefined; //no en-passant } + getPotentialMovesFrom([x,y]) + { + const standardMoves = super.getPotentialMovesFrom([x,y]); + let moves = []; + standardMoves.forEach(m => { + let newMove_s = this.applyMagneticLaws(m); + if (newMove_s.length == 1) + moves.push(newMove_s[0]); + else //promotion + moves = moves.concat(moves, newMove_s); + }); + } + // Complete a move with magnetic actions - applyMagneticLaws([x,y], move) + applyMagneticLaws(standardMove) { - const standardMove = JSON.parse(JSON.stringify(move)); + const [x,y] = [moves.start.x, move.start.y]; + let move = JSON.parse(JSON.stringify(standardMove)); this.play(standardMove); const color = this.getColor(x,y); const [sizeX,sizeY] = VariantRules.size; @@ -88,172 +102,22 @@ class MagneticRules extends ChessRules } } this.undo(standardMove); - } - - // TODO: when pawn is pushed to 8th rank, apply promotions (similar change as in Checkered) - getBasicMove([sx,sy], [ex,ey], tr) - { - var mv = new Move({ - appear: [ - new PiPo({ - x: ex, - y: ey, - c: !!tr ? tr.c : this.getColor(sx,sy), - p: !!tr ? tr.p : this.getPiece(sx,sy) - }) - ], - vanish: [ - new PiPo({ - x: sx, - y: sy, - c: this.getColor(sx,sy), - p: this.getPiece(sx,sy) - }) - ] - }); - - if (this.board[ex][ey] != VariantRules.EMPTY) - { - mv.vanish.push( - new PiPo({ - x: ex, - y: ey, - c: this.getColor(ex,ey), - p: this.getPiece(ex,ey) - }) - ); - } - this.applyMagneticLaws([ex,ey], mv); - return mv; - } - - getPotentialPawnMoves([x,y]) - { - const color = this.getColor(x,y); - var moves = []; - var V = VariantRules; - const [sizeX,sizeY] = VariantRules.size; - let shift = (color == "w" ? -1 : 1); - let startRank = (color == "w" ? sizeY-2 : 1); - let firstRank = (color == 'w' ? sizeY-1 : 0); - let lastRank = (color == "w" ? 0 : sizeY-1); - - if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank) - { - // Normal moves - if (this.board[x+shift][y] == V.EMPTY) - { - moves.push(this.getBasicMove([x,y], [x+shift,y])); - if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY) - { - // Two squares jump - moves.push(this.getBasicMove([x,y], [x+2*shift,y])); - } - } - // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y-1])); - if (y { - // Normal move - if (this.board[x+shift][y] == V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); - // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p})); - if (y