b14b19e8ef24fabc387ad57fba34ee8c0707567b
[vchess.git] / client / src / variants / Berolina.js
1 import { ChessRules } from "@/base_rules";
2
3 export class BerolinaRules extends ChessRules {
4 // En-passant after 2-sq jump
5 getEpSquare(moveOrSquare) {
6 if (!moveOrSquare) return undefined;
7 if (typeof moveOrSquare === "string") {
8 const square = moveOrSquare;
9 if (square == "-") return undefined;
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;
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 [
22 {
23 x: (ex + sx) / 2,
24 y: (move.end.y + sy) / 2
25 },
26 // The arrival column must be remembered, because
27 // potentially two pawns could be candidates to be captured:
28 // one on our left, and one on our right.
29 move.end.y
30 ];
31 }
32 return undefined; //default
33 }
34
35 static IsGoodEnpassant(enpassant) {
36 if (enpassant != "-") {
37 const epParts = enpassant.split(",");
38 const epSq = V.SquareToCoords(epParts[0]);
39 if (isNaN(epSq.x) || isNaN(epSq.y) || !V.OnBoard(epSq)) return false;
40 const arrCol = V.ColumnToCoord(epParts[1]);
41 if (isNaN(arrCol) || arrCol < 0 || arrCol >= V.size.y) return false;
42 }
43 return true;
44 }
45
46 getEnpassantFen() {
47 const L = this.epSquares.length;
48 if (!this.epSquares[L - 1]) return "-"; //no en-passant
49 return (
50 V.CoordsToSquare(this.epSquares[L - 1][0]) +
51 "," +
52 V.CoordToColumn(this.epSquares[L - 1][1])
53 );
54 }
55
56 // Special pawns movements
57 getPotentialPawnMoves([x, y]) {
58 const color = this.turn;
59 let moves = [];
60 const [sizeX, sizeY] = [V.size.x, V.size.y];
61 const shiftX = color == "w" ? -1 : 1;
62 const startRank = color == "w" ? sizeX - 2 : 1;
63 const lastRank = color == "w" ? 0 : sizeX - 1;
64 const finalPieces =
65 x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN];
66
67 // One square diagonally
68 for (let shiftY of [-1, 1]) {
69 if (this.board[x + shiftX][y + shiftY] == V.EMPTY) {
70 for (let piece of finalPieces) {
71 moves.push(
72 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
73 c: color,
74 p: piece
75 })
76 );
77 }
78 if (
79 V.PawnSpecs.twoSquares &&
80 x == startRank &&
81 y + 2 * shiftY >= 0 &&
82 y + 2 * shiftY < sizeY &&
83 this.board[x + 2 * shiftX][y + 2 * shiftY] == V.EMPTY
84 ) {
85 // Two squares jump
86 moves.push(
87 this.getBasicMove([x, y], [x + 2 * shiftX, y + 2 * shiftY])
88 );
89 }
90 }
91 }
92 // Capture
93 if (
94 this.board[x + shiftX][y] != V.EMPTY &&
95 this.canTake([x, y], [x + shiftX, y])
96 ) {
97 for (let piece of finalPieces)
98 moves.push(
99 this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
100 );
101 }
102
103 // Next condition so that other variants could inherit from this class
104 if (V.PawnSpecs.enPassant) {
105 // En passant
106 const Lep = this.epSquares.length;
107 const epSquare = this.epSquares[Lep - 1]; //always at least one element
108 if (
109 !!epSquare &&
110 epSquare[0].x == x + shiftX &&
111 epSquare[0].y == y
112 ) {
113 let enpassantMove = this.getBasicMove([x, y], [x + shiftX, y]);
114 enpassantMove.vanish.push({
115 x: x,
116 y: epSquare[1],
117 p: "p",
118 c: this.getColor(x, epSquare[1])
119 });
120 moves.push(enpassantMove);
121 }
122 }
123
124 return moves;
125 }
126
127 isAttackedByPawn([x, y], color) {
128 let pawnShift = (color == "w" ? 1 : -1);
129 if (x + pawnShift >= 0 && x + pawnShift < V.size.x) {
130 if (
131 this.getPiece(x + pawnShift, y) == V.PAWN &&
132 this.getColor(x + pawnShift, y) == color
133 ) {
134 return true;
135 }
136 }
137 return false;
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 };