From 7f0711a8515a199efe66ab903a2e710d77b1ce76 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Tue, 20 Nov 2018 22:01:22 +0100 Subject: [PATCH] Finish Magnetic chess --- public/javascripts/variants/Magnetic.js | 136 +++++++++++++++++++++++- views/rules/Magnetic.pug | 32 ++++++ 2 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 views/rules/Magnetic.pug diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js index e9415322..2d576e1b 100644 --- a/public/javascripts/variants/Magnetic.js +++ b/public/javascripts/variants/Magnetic.js @@ -1,4 +1,4 @@ -class MagneticRules +class MagneticRules extends ChessRules { getEpSquare(move) { @@ -8,7 +8,86 @@ class MagneticRules // Complete a move with magnetic actions applyMagneticLaws([x,y], move) { - // TODO + const standardMove = JSON.parse(JSON.stringify(move)); + this.play(standardMove); + const color = this.getColor(x,y); + const [sizeX,sizeY] = VariantRules.size; + for (let step of [[-1,0],[1,0],[0,-1],[0,1]]) + { + let [i,j] = [x+step[0],y+step[1]]; + while (i>=0 && i=0 && j=2 || Math.abs(j-y)>=2) + && this.getPiece(i,j) != VariantRules.KING) + { + move.vanish.push( + new PiPo({ + p:this.getPiece(i,j), + c:this.getColor(i,j), + x:i, + y:j + }) + ); + move.appear.push( + new PiPo({ + p:this.getPiece(i,j), + c:this.getColor(i,j), + x:x+step[0], + y:y+step[1] + }) + ); + } + } + else + { + // Repulsion + if (this.getPiece(i,j) != VariantRules.KING) + { + // Push it until we meet an obstacle or edge of the board + let [ii,jj] = [i+step[0],j+step[1]]; + while (ii>=0 && ii=0 && jj=1 || Math.abs(jj-j)>=1) + { + move.vanish.push( + new PiPo({ + p:this.getPiece(i,j), + c:this.getColor(i,j), + x:i, + y:j + }) + ); + move.appear.push( + new PiPo({ + p:this.getPiece(i,j), + c:this.getColor(i,j), + x:ii, + y:jj + }) + ); + } + } + } + break; + } + i += step[0]; + j += step[1]; + } + } + this.undo(standardMove); } getBasicMove([sx,sy], [ex,ey], tr) @@ -47,6 +126,57 @@ class MagneticRules 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