Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
6808d7a1 | 3 | export const VariantRules = class BerolinaRules extends ChessRules { |
dac39588 | 4 | // En-passant after 2-sq jump |
6808d7a1 BA |
5 | getEpSquare(moveOrSquare) { |
6 | if (!moveOrSquare) return undefined; | |
7 | if (typeof moveOrSquare === "string") { | |
dac39588 | 8 | const square = moveOrSquare; |
6808d7a1 | 9 | if (square == "-") return undefined; |
dac39588 BA |
10 | // Enemy pawn initial column must be given too: |
11 | let res = []; | |
12 | const epParts = square.split(","); | |
13 | res.push(V.SquareToCoords(epParts[0])); | |
14 | res.push(V.ColumnToCoord(epParts[1])); | |
15 | return res; | |
16 | } | |
17 | // Argument is a move: | |
18 | const move = moveOrSquare; | |
6808d7a1 BA |
19 | const [sx, ex, sy] = [move.start.x, move.end.x, move.start.y]; |
20 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) == 2) { | |
21 | return [ | |
dac39588 | 22 | { |
6808d7a1 BA |
23 | x: (ex + sx) / 2, |
24 | y: (move.end.y + sy) / 2 | |
dac39588 BA |
25 | }, |
26 | move.end.y | |
27 | ]; | |
28 | } | |
29 | return undefined; //default | |
30 | } | |
375ecdd1 | 31 | |
dac39588 | 32 | // Special pawns movements |
6808d7a1 | 33 | getPotentialPawnMoves([x, y]) { |
dac39588 BA |
34 | const color = this.turn; |
35 | let moves = []; | |
6808d7a1 BA |
36 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
37 | const shiftX = color == "w" ? -1 : 1; | |
38 | const startRank = color == "w" ? sizeX - 2 : 1; | |
39 | const lastRank = color == "w" ? 0 : sizeX - 1; | |
40 | const finalPieces = | |
41 | x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN]; | |
375ecdd1 | 42 | |
dac39588 | 43 | // One square diagonally |
6808d7a1 BA |
44 | for (let shiftY of [-1, 1]) { |
45 | if (this.board[x + shiftX][y + shiftY] == V.EMPTY) { | |
46 | for (let piece of finalPieces) { | |
47 | moves.push( | |
48 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
49 | c: color, | |
50 | p: piece | |
51 | }) | |
52 | ); | |
dac39588 | 53 | } |
6808d7a1 BA |
54 | if ( |
55 | x == startRank && | |
56 | y + 2 * shiftY >= 0 && | |
57 | y + 2 * shiftY < sizeY && | |
58 | this.board[x + 2 * shiftX][y + 2 * shiftY] == V.EMPTY | |
59 | ) { | |
dac39588 | 60 | // Two squares jump |
6808d7a1 BA |
61 | moves.push( |
62 | this.getBasicMove([x, y], [x + 2 * shiftX, y + 2 * shiftY]) | |
63 | ); | |
dac39588 BA |
64 | } |
65 | } | |
66 | } | |
67 | // Capture | |
6808d7a1 BA |
68 | if ( |
69 | this.board[x + shiftX][y] != V.EMPTY && | |
70 | this.canTake([x, y], [x + shiftX, y]) | |
71 | ) { | |
dac39588 | 72 | for (let piece of finalPieces) |
6808d7a1 BA |
73 | moves.push( |
74 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
75 | ); | |
dac39588 | 76 | } |
375ecdd1 | 77 | |
dac39588 BA |
78 | // En passant |
79 | const Lep = this.epSquares.length; | |
6808d7a1 BA |
80 | const epSquare = this.epSquares[Lep - 1]; //always at least one element |
81 | if ( | |
82 | !!epSquare && | |
83 | epSquare[0].x == x + shiftX && | |
84 | epSquare[0].y == y && | |
85 | Math.abs(epSquare[1] - y) == 1 | |
86 | ) { | |
87 | let enpassantMove = this.getBasicMove([x, y], [x + shiftX, y]); | |
dac39588 BA |
88 | enpassantMove.vanish.push({ |
89 | x: x, | |
90 | y: epSquare[1], | |
6808d7a1 BA |
91 | p: "p", |
92 | c: this.getColor(x, epSquare[1]) | |
dac39588 BA |
93 | }); |
94 | moves.push(enpassantMove); | |
95 | } | |
375ecdd1 | 96 | |
dac39588 BA |
97 | return moves; |
98 | } | |
f6dbe8e3 | 99 | |
6808d7a1 BA |
100 | isAttackedByPawn([x, y], colors) { |
101 | for (let c of colors) { | |
102 | let pawnShift = c == "w" ? 1 : -1; | |
103 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { | |
104 | if ( | |
105 | this.getPiece(x + pawnShift, y) == V.PAWN && | |
106 | this.getColor(x + pawnShift, y) == c | |
107 | ) { | |
dac39588 BA |
108 | return true; |
109 | } | |
110 | } | |
111 | } | |
112 | return false; | |
113 | } | |
26c1e3bd | 114 | |
6808d7a1 | 115 | getNotation(move) { |
dac39588 | 116 | const piece = this.getPiece(move.start.x, move.start.y); |
6808d7a1 | 117 | if (piece == V.PAWN) { |
dac39588 BA |
118 | // Pawn move |
119 | const finalSquare = V.CoordsToSquare(move.end); | |
120 | let notation = ""; | |
6808d7a1 BA |
121 | if (move.vanish.length == 2) |
122 | //capture | |
dac39588 | 123 | notation = "Px" + finalSquare; |
6808d7a1 | 124 | else { |
dac39588 BA |
125 | // No capture: indicate the initial square for potential ambiguity |
126 | const startSquare = V.CoordsToSquare(move.start); | |
127 | notation = startSquare + finalSquare; | |
128 | } | |
6808d7a1 BA |
129 | if (move.appear[0].p != V.PAWN) |
130 | //promotion | |
dac39588 BA |
131 | notation += "=" + move.appear[0].p.toUpperCase(); |
132 | return notation; | |
133 | } | |
134 | return super.getNotation(move); //all other pieces are orthodox | |
135 | } | |
6808d7a1 | 136 | }; |