Add Fugue variant
[vchess.git] / client / src / variants / Recycle.js
CommitLineData
78d64531
BA
1import { ChessRules, PiPo, Move } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3
32f6285e 4export class RecycleRules extends ChessRules {
7e8a7ea1 5
32f6285e
BA
6 static get PawnSpecs() {
7 return Object.assign(
8 {},
9 ChessRules.PawnSpecs,
10 { promotions: [V.PAWN] } //in fact, none
11 );
12 }
13
78d64531
BA
14 static IsGoodFen(fen) {
15 if (!ChessRules.IsGoodFen(fen)) return false;
16 const fenParsed = V.ParseFen(fen);
17 // 5) Check reserves
18 if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{10,10}$/))
19 return false;
20 return true;
21 }
22
23 static ParseFen(fen) {
24 const fenParts = fen.split(" ");
6f2f9437
BA
25 return Object.assign(
26 ChessRules.ParseFen(fen),
27 { reserve: fenParts[5] }
28 );
78d64531
BA
29 }
30
7ba4a5bc
BA
31 static GenRandInitFen(randomness) {
32 return ChessRules.GenRandInitFen(randomness) + " 0000000000";
78d64531
BA
33 }
34
35 getFen() {
90e814b6 36 return super.getFen() + " " + this.getReserveFen();
78d64531
BA
37 }
38
f9c36b2d 39 getFenForRepeat() {
90e814b6 40 return super.getFenForRepeat() + "_" + this.getReserveFen();
f9c36b2d
BA
41 }
42
78d64531
BA
43 getReserveFen() {
44 let counts = new Array(10);
45 for (
46 let i = 0;
47 i < V.PIECES.length - 1;
48 i++ //-1: no king reserve
49 ) {
50 counts[i] = this.reserve["w"][V.PIECES[i]];
51 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
52 }
53 return counts.join("");
54 }
55
56 setOtherVariables(fen) {
57 super.setOtherVariables(fen);
78d64531 58 // Also init reserves (used by the interface to show landable pieces)
e50a8025
BA
59 const reserve =
60 V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
78d64531
BA
61 this.reserve = {
62 w: {
e50a8025
BA
63 [V.PAWN]: reserve[0],
64 [V.ROOK]: reserve[1],
65 [V.KNIGHT]: reserve[2],
66 [V.BISHOP]: reserve[3],
67 [V.QUEEN]: reserve[4]
78d64531
BA
68 },
69 b: {
e50a8025
BA
70 [V.PAWN]: reserve[5],
71 [V.ROOK]: reserve[6],
72 [V.KNIGHT]: reserve[7],
73 [V.BISHOP]: reserve[8],
74 [V.QUEEN]: reserve[9]
78d64531
BA
75 }
76 };
77 }
78
79 getColor(i, j) {
80 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
81 return this.board[i][j].charAt(0);
82 }
83
84 getPiece(i, j) {
85 if (i >= V.size.x) return V.RESERVE_PIECES[j];
86 return this.board[i][j].charAt(1);
87 }
88
89 // Used by the interface:
241bf8f2 90 getReservePpath(index, color) {
78d64531
BA
91 return color + V.RESERVE_PIECES[index];
92 }
93
94 // Ordering on reserve pieces
95 static get RESERVE_PIECES() {
96 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
97 }
98
99 getReserveMoves([x, y]) {
100 const color = this.turn;
101 const p = V.RESERVE_PIECES[y];
102 if (this.reserve[color][p] == 0) return [];
103 let moves = [];
104 const pawnShift = p == V.PAWN ? 1 : 0;
105 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
106 for (let j = 0; j < V.size.y; j++) {
107 if (this.board[i][j] == V.EMPTY) {
108 let mv = new Move({
109 appear: [
110 new PiPo({
111 x: i,
112 y: j,
113 c: color,
114 p: p
115 })
116 ],
117 vanish: [],
118 start: { x: x, y: y }, //a bit artificial...
119 end: { x: i, y: j }
120 });
121 moves.push(mv);
122 }
123 }
124 }
125 return moves;
126 }
127
128 getPotentialMovesFrom([x, y]) {
e50a8025 129 if (x >= V.size.x)
78d64531
BA
130 // Reserves, outside of board: x == sizeX(+1)
131 return this.getReserveMoves([x, y]);
78d64531
BA
132 // Standard moves
133 return super.getPotentialMovesFrom([x, y]);
134 }
135
136 getPotentialPawnMoves([x, y]) {
32f6285e
BA
137 let moves = super.getPotentialPawnMoves([x, y]);
138 // Remove pawns on 8th rank ("fallen"):
78d64531 139 const color = this.turn;
32f6285e
BA
140 const lastRank = (color == "w" ? 0 : V.size.x - 1);
141 moves.forEach(m => {
142 if (m.appear[0].x == lastRank) m.appear.pop();
143 });
78d64531
BA
144 return moves;
145 }
146
147 getAllValidMoves() {
0e001022 148 let moves = super.getAllPotentialMoves();
78d64531 149 const color = this.turn;
0e001022 150 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
78d64531
BA
151 moves = moves.concat(
152 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
153 );
0e001022 154 }
78d64531
BA
155 return this.filterValid(moves);
156 }
157
158 atLeastOneMove() {
159 if (!super.atLeastOneMove()) {
160 // Search one reserve move
161 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
162 let moves = this.filterValid(
163 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
164 );
165 if (moves.length > 0) return true;
166 }
167 return false;
168 }
169 return true;
170 }
171
172 canTake([x1, y1], [x2, y2]) {
173 // Self-captures allowed, except for the king:
174 return this.getPiece(x2, y2) != V.KING;
175 }
176
32f6285e
BA
177 prePlay(move) {
178 super.prePlay(move);
2c5d7b20
BA
179 // Skip castle:
180 if (move.vanish.length == 2 && move.appear.length == 2) return;
32f6285e 181 const color = this.turn;
3a2a7b5f
BA
182 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]--;
183 else if (move.vanish.length == 2 && move.vanish[1].c == color)
78d64531
BA
184 // Self-capture
185 this.reserve[color][move.vanish[1].p]++;
78d64531
BA
186 }
187
3a2a7b5f
BA
188 postUndo(move) {
189 super.postUndo(move);
78d64531
BA
190 if (move.vanish.length == 2 && move.appear.length == 2) return;
191 const color = this.turn;
3a2a7b5f
BA
192 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]++;
193 else if (move.vanish.length == 2 && move.vanish[1].c == color)
78d64531 194 this.reserve[color][move.vanish[1].p]--;
78d64531
BA
195 }
196
197 static get SEARCH_DEPTH() {
198 return 2;
199 }
200
201 evalPosition() {
202 let evaluation = super.evalPosition();
203 // Add reserves:
204 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
205 const p = V.RESERVE_PIECES[i];
206 evaluation += this.reserve["w"][p] * V.VALUES[p];
207 evaluation -= this.reserve["b"][p] * V.VALUES[p];
208 }
209 return evaluation;
210 }
211
212 getNotation(move) {
213 const finalSquare = V.CoordsToSquare(move.end);
214 if (move.vanish.length > 0) {
215 if (move.appear.length > 0) {
216 // Standard move
217 return super.getNotation(move);
218 } else {
219 // Pawn fallen: capturing or not
220 let res = "";
221 if (move.vanish.length == 2)
222 res += V.CoordToColumn(move.start.y) + "x";
223 return res + finalSquare;
224 }
225 }
226 // Rebirth:
227 const piece =
228 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
229 return piece + "@" + V.CoordsToSquare(move.end);
230 }
7e8a7ea1 231
78d64531 232};