X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMagnetic.js;h=d5731a63c0d70e4c8a5071575e1ca01c80cbe998;hb=2526c041baf44968b0aa7b98af56730e88f6a595;hp=e94153229532d42acaf32dd1c06ea6f7ae0717de;hpb=1af36beb39aa5d59735483e915fd1040f93eecca;p=vchess.git diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js index e9415322..d5731a63 100644 --- a/public/javascripts/variants/Magnetic.js +++ b/public/javascripts/variants/Magnetic.js @@ -1,128 +1,123 @@ -class MagneticRules +class MagneticRules extends ChessRules { getEpSquare(move) { return undefined; //no en-passant } - // Complete a move with magnetic actions - applyMagneticLaws([x,y], move) - { - // TODO - } - - getBasicMove([sx,sy], [ex,ey], tr) + getPotentialMovesFrom([x,y]) { - 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) - }) - ] + 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); }); - - 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; } - getCastleMoves([x,y]) + // Complete a move with magnetic actions + applyMagneticLaws(standardMove) { - const c = this.getColor(x,y); - if (x != (c=="w" ? 7 : 0) || y != this.INIT_COL_KING[c]) - return []; //x isn't first rank, or king has moved (shortcut) - - const V = VariantRules; - - // Castling ? - const oppCol = this.getOppCol(c); - let moves = []; - let i = 0; - const finalSquares = [ [2,3], [6,5] ]; //king, then rook - castlingCheck: - for (let castleSide=0; castleSide < 2; castleSide++) //large, then small + 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; + for (let step of [[-1,0],[1,0],[0,-1],[0,1]]) { - if (!this.flags[c][castleSide]) - continue; - // If this code is reached, rooks and king are on initial position - - // Nothing on the path of the king (and no checks; OK also if y==finalSquare)? - let step = finalSquares[castleSide][0] < y ? -1 : 1; - for (i=y; i!=finalSquares[castleSide][0]; i+=step) - { - if (this.isAttacked([x,i], oppCol) || (this.board[x][i] != V.EMPTY && - // NOTE: next check is enough, because of chessboard constraints - (this.getColor(x,i) != c || ![V.KING,V.ROOK].includes(this.getPiece(x,i))))) - { - continue castlingCheck; - } - } - - // Nothing on the path to the rook? - step = castleSide == 0 ? -1 : 1; - for (i = y + step; i != this.INIT_COL_ROOK[c][castleSide]; i += step) + 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]; } - - // If this code is reached, castle is valid - let cmove = new Move({ - appear: [ - new PiPo({x:x,y:finalSquares[castleSide][0],p:V.KING,c:c}), - new PiPo({x:x,y:finalSquares[castleSide][1],p:V.ROOK,c:c})], - vanish: [ - new PiPo({x:x,y:y,p:V.KING,c:c}), - new PiPo({x:x,y:rookPos,p:V.ROOK,c:c})], - end: Math.abs(y - rookPos) <= 2 - ? {x:x, y:rookPos} - : {x:x, y:y + 2 * (castleSide==0 ? -1 : 1)} - }); - this.applyMagneticLaws([x,finalSquares[castleSide][1]], cmove); - moves.push(cmove); } - + this.undo(standardMove); + let moves = []; + if (..condition pawn promote) + { + move. ... = ... //loop + moves.push(...); + } + else + moves.push(move); return moves; } // TODO: verify this assertion -// atLeastOneMove() -// { -// return true; //always at least one possible move -// } + atLeastOneMove() + { + return true; //always at least one possible move + } underCheck(move) { @@ -135,7 +130,7 @@ class MagneticRules const saveKingPos = this.kingPos[c]; //king might be taken this.play(move); // The only way to be "under check" is to have lost the king (thus game over) - let res = this.kingPos[c][0] < 0; + let res = this.kingPos[c][0] < 0 ? [ JSON.parse(JSON.stringify(saveKingPos)) ] : [ ]; this.undo(move); @@ -153,7 +148,7 @@ class MagneticRules // We took opponent king ! const oppCol = this.getOppCol(c); this.kingPos[oppCol] = [-1,-1]; - this.flags[oppCol] = [false,false]; + this.castleFlags[oppCol] = [false,false]; } }