0765d4b9ecd670bf25fbc78e42762a0e8f85a2af
[vchess.git] / client / src / variants / Berolina.js
1 import { ChessRules } from "@/base_rules";
2
3 export class BerolinaRules extends ChessRules {
4
5 // En-passant after 2-sq jump
6 getEpSquare(moveOrSquare) {
7 if (!moveOrSquare) return undefined;
8 if (typeof moveOrSquare === "string") {
9 const square = moveOrSquare;
10 if (square == "-") return undefined;
11 // Enemy pawn initial column must be given too:
12 let res = [];
13 const epParts = square.split(",");
14 res.push(V.SquareToCoords(epParts[0]));
15 res.push(V.ColumnToCoord(epParts[1]));
16 return res;
17 }
18 // Argument is a move:
19 const move = moveOrSquare;
20 const [sx, ex, sy] = [move.start.x, move.end.x, move.start.y];
21 if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) == 2) {
22 return [
23 {
24 x: (ex + sx) / 2,
25 y: (move.end.y + sy) / 2
26 },
27 // The arrival column must be remembered, because
28 // potentially two pawns could be candidates to be captured:
29 // one on our left, and one on our right.
30 move.end.y
31 ];
32 }
33 return undefined; //default
34 }
35
36 static IsGoodEnpassant(enpassant) {
37 if (enpassant != "-") {
38 const epParts = enpassant.split(",");
39 const epSq = V.SquareToCoords(epParts[0]);
40 if (isNaN(epSq.x) || isNaN(epSq.y) || !V.OnBoard(epSq)) return false;
41 const arrCol = V.ColumnToCoord(epParts[1]);
42 if (isNaN(arrCol) || arrCol < 0 || arrCol >= V.size.y) return false;
43 }
44 return true;
45 }
46
47 getEnpassantFen() {
48 const L = this.epSquares.length;
49 if (!this.epSquares[L - 1]) return "-"; //no en-passant
50 return (
51 V.CoordsToSquare(this.epSquares[L - 1][0]) +
52 "," +
53 V.CoordToColumn(this.epSquares[L - 1][1])
54 );
55 }
56
57 // Special pawns movements
58 getPotentialPawnMoves([x, y]) {
59 const color = this.turn;
60 let moves = [];
61 const [sizeX, sizeY] = [V.size.x, V.size.y];
62 const shiftX = color == "w" ? -1 : 1;
63 const startRank = color == "w" ? sizeX - 2 : 1;
64 const lastRank = color == "w" ? 0 : sizeX - 1;
65 const finalPieces =
66 x + shiftX == lastRank
67 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
68 : [V.PAWN];
69
70 // One square diagonally
71 for (let shiftY of [-1, 1]) {
72 if (this.board[x + shiftX][y + shiftY] == V.EMPTY) {
73 for (let piece of finalPieces) {
74 moves.push(
75 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
76 c: color,
77 p: piece
78 })
79 );
80 }
81 if (
82 V.PawnSpecs.twoSquares &&
83 x == startRank &&
84 y + 2 * shiftY >= 0 &&
85 y + 2 * shiftY < sizeY &&
86 this.board[x + 2 * shiftX][y + 2 * shiftY] == V.EMPTY
87 ) {
88 // Two squares jump
89 moves.push(
90 this.getBasicMove([x, y], [x + 2 * shiftX, y + 2 * shiftY])
91 );
92 }
93 }
94 }
95 // Capture
96 if (
97 this.board[x + shiftX][y] != V.EMPTY &&
98 this.canTake([x, y], [x + shiftX, y])
99 ) {
100 for (let piece of finalPieces) {
101 moves.push(
102 this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
103 );
104 }
105 }
106
107 // Next condition so that other variants could inherit from this class
108 if (V.PawnSpecs.enPassant) {
109 // En passant
110 const Lep = this.epSquares.length;
111 const epSquare = this.epSquares[Lep - 1]; //always at least one element
112 if (
113 !!epSquare &&
114 epSquare[0].x == x + shiftX &&
115 epSquare[0].y == y
116 ) {
117 let enpassantMove = this.getBasicMove([x, y], [x + shiftX, y]);
118 enpassantMove.vanish.push({
119 x: x,
120 y: epSquare[1],
121 p: "p",
122 c: this.getColor(x, epSquare[1])
123 });
124 moves.push(enpassantMove);
125 }
126 }
127
128 return moves;
129 }
130
131 isAttackedByPawn([x, y], color) {
132 let pawnShift = (color == "w" ? 1 : -1);
133 return (
134 x + pawnShift >= 0 && x + pawnShift < V.size.x &&
135 this.getPiece(x + pawnShift, y) == V.PAWN &&
136 this.getColor(x + pawnShift, y) == color
137 );
138 }
139
140 static get SEARCH_DEPTH() {
141 return 2;
142 }
143
144 getNotation(move) {
145 const piece = this.getPiece(move.start.x, move.start.y);
146 if (piece == V.PAWN) {
147 // Pawn move
148 const finalSquare = V.CoordsToSquare(move.end);
149 let notation = "";
150 if (move.vanish.length == 2)
151 // Capture
152 notation = "Px" + finalSquare;
153 else {
154 // No capture: indicate the initial square for potential ambiguity
155 const startSquare = V.CoordsToSquare(move.start);
156 notation = startSquare + finalSquare;
157 }
158 if (move.appear[0].p != V.PAWN)
159 // Promotion
160 notation += "=" + move.appear[0].p.toUpperCase();
161 return notation;
162 }
163 return super.getNotation(move); //all other pieces are orthodox
164 }
165
166 };