- if (Object.keys(V.PromoteMap).includes(p)) {
- const promoted = V.PromoteMap[p];
- const lastRanks = (c == 'w' ? [0, 1] : [9, 8]);
- let promotions = [];
- moves.forEach(m => {
- if (lastRanks.includes(m.start.x) || lastRanks.includes(m.end.x)) {
- let pMove = JSON.parse(JSON.stringify(m));
- pMove.appear[0].p = promoted;
- promotions.push(pMove);
- }
- });
- Array.prototype.push.apply(moves, promotions);
- }
- return moves;
- }
-
- addPawnMoves([x1, y1], [x2, y2], moves) {
- const color = this.turn;
- const lastRanks = (color == "w" ? [0, 1] : [9, 8]);
- if (!lastRanks.includes(x2)) {
- moves.push(this.getBasicMove([x1, y1], [x2, y2]));
- return;
- }
- let finalPieces = [V.GILDING];
- if (x2 == lastRanks[1]) finalPieces.push(V.PAWN);
- for (let piece of finalPieces) {
- const tr = (piece != V.PAWN ? { c: color, p: piece } : null);
- moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
- }
- }
-
- getPotentialPawnMoves([x, y]) {
- const color = this.turn;
- const shiftX = (color == 'w' ? -1 : 1);
- let moves = [];
- if (this.board[x + shiftX][y] == V.EMPTY) {
- this.addPawnMoves([x, y], [x + shiftX, y], moves);
- if ((color == 'w' && x >= V.size.x - 3) || (color == 'b' && x <= 2)) {
- if (this.board[x + 2 * shiftX][y] == V.EMPTY) {
- moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
- if (
- (
- (color == 'w' && x == V.size.x - 2) ||
- (color == 'b' && x == 1)
- )
- &&
- this.board[x + 3 * shiftX][y] == V.EMPTY
- ) {
- moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
- }
- }
- }
- }
- for (let shiftY of [-1, 1]) {
- if (y + shiftY >= 0 && y + shiftY < V.size.y) {
- if (
- this.board[x + shiftX][y + shiftY] != V.EMPTY &&
- this.canTake([x, y], [x + shiftX, y + shiftY])
- ) {
- this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves);
- }
- }
- }
- Array.prototype.push.apply(
- moves,
- this.getEnpassantCaptures([x, y], shiftX)
- );