From: Benjamin Auder Date: Tue, 20 Nov 2018 21:01:22 +0000 (+0100) Subject: Finish Magnetic chess X-Git-Url: https://git.auder.net/js/%7B%7B%20path%28%27mixstore_store_package_view%27%2C%20%7B%20id:%20newsItem.id%20%7D%29%20%7D%7D?a=commitdiff_plain;h=7f0711a8515a199efe66ab903a2e710d77b1ce76;p=vchess.git Finish Magnetic chess --- 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