X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=d85ff869a22eea9ba8e841040efc3734ea889ccb;hp=ae0800cdc5abc8d3680e36a7e3f1133785d6f1db;hb=375ecdd1387e729f85ed114e82253469e4849869;hpb=2d7194bd9c976f444e43e5dc0a725823b6472eb9 diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index ae0800cd..d85ff869 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -273,7 +273,7 @@ class ChessRules turn: fenParts[1], }; let nextIdx = 2; - if (V.hasFlags) + if (V.HasFlags) Object.assign(res, {flags: fenParts[nextIdx++]}); if (V.HasEnpassant) Object.assign(res, {enpassant: fenParts[nextIdx]}); @@ -307,7 +307,7 @@ class ChessRules position += emptyCount; emptyCount = 0; } - fen += V.board2fen(this.board[i][j]); + position += V.board2fen(this.board[i][j]); } } if (emptyCount > 0) @@ -438,7 +438,7 @@ class ChessRules // Set flags and enpassant: const parsedFen = V.ParseFen(fen); if (V.HasFlags) - this.setFlags(fenParsed.flags); + this.setFlags(parsedFen.flags); if (V.HasEnpassant) { const epSq = parsedFen.enpassant != "-" @@ -599,74 +599,65 @@ class ChessRules const color = this.turn; let moves = []; const [sizeX,sizeY] = [V.size.x,V.size.y]; - const shift = (color == "w" ? -1 : 1); + const shiftX = (color == "w" ? -1 : 1); const firstRank = (color == 'w' ? sizeX-1 : 0); const startRank = (color == "w" ? sizeX-2 : 1); const lastRank = (color == "w" ? 0 : sizeX-1); + const pawnColor = this.getColor(x,y); //can be different for checkered - if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank) + if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true { - // Normal moves - if (this.board[x+shift][y] == V.EMPTY) + const finalPieces = x + shiftX == lastRank + ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] + : [V.PAWN] + // One square forward + if (this.board[x+shiftX][y] == V.EMPTY) { - moves.push(this.getBasicMove([x,y], [x+shift,y])); - // Next condition because variants with pawns on 1st rank allow them to jump - if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY) + for (let piece of finalPieces) + { + moves.push(this.getBasicMove([x,y], [x+shiftX,y], + {c:pawnColor,p:piece})); + } + // Next condition because pawns on 1st rank can generally jump + if ([startRank,firstRank].includes(x) + && this.board[x+2*shiftX][y] == V.EMPTY) { // Two squares jump - moves.push(this.getBasicMove([x,y], [x+2*shift,y])); + moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); } } // Captures - if (y>0 && this.board[x+shift][y-1] != V.EMPTY - && this.canTake([x,y], [x+shift,y-1])) - { - 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:pawnColor,p:p})); - // Captures - if (y>0 && this.board[x+shift][y-1] != V.EMPTY - && this.canTake([x,y], [x+shift,y-1])) + if (y + shiftY >= 0 && y + shiftY < sizeY + && this.board[x+shiftX][y+shiftY] != V.EMPTY + && this.canTake([x,y], [x+shiftX,y+shiftY])) { - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p})); - } - if (y