Randomize Pandemonium
[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
ded43c88
BA
89 getPPpath(m) {
90 if (m.vanish.length == 2 && m.appear.length == 2) {
91 // Castle: show castle symbol
92 return "Coregal/castle";
93 }
94 return super.getPPpath(m);
95 }
96
78d64531 97 // Used by the interface:
241bf8f2 98 getReservePpath(index, color) {
78d64531
BA
99 return color + V.RESERVE_PIECES[index];
100 }
101
102 // Ordering on reserve pieces
103 static get RESERVE_PIECES() {
104 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
105 }
106
107 getReserveMoves([x, y]) {
108 const color = this.turn;
109 const p = V.RESERVE_PIECES[y];
110 if (this.reserve[color][p] == 0) return [];
111 let moves = [];
112 const pawnShift = p == V.PAWN ? 1 : 0;
113 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
114 for (let j = 0; j < V.size.y; j++) {
115 if (this.board[i][j] == V.EMPTY) {
116 let mv = new Move({
117 appear: [
118 new PiPo({
119 x: i,
120 y: j,
121 c: color,
122 p: p
123 })
124 ],
125 vanish: [],
126 start: { x: x, y: y }, //a bit artificial...
127 end: { x: i, y: j }
128 });
129 moves.push(mv);
130 }
131 }
132 }
133 return moves;
134 }
135
136 getPotentialMovesFrom([x, y]) {
e50a8025 137 if (x >= V.size.x)
78d64531
BA
138 // Reserves, outside of board: x == sizeX(+1)
139 return this.getReserveMoves([x, y]);
78d64531
BA
140 // Standard moves
141 return super.getPotentialMovesFrom([x, y]);
142 }
143
144 getPotentialPawnMoves([x, y]) {
32f6285e
BA
145 let moves = super.getPotentialPawnMoves([x, y]);
146 // Remove pawns on 8th rank ("fallen"):
78d64531 147 const color = this.turn;
32f6285e
BA
148 const lastRank = (color == "w" ? 0 : V.size.x - 1);
149 moves.forEach(m => {
150 if (m.appear[0].x == lastRank) m.appear.pop();
151 });
78d64531
BA
152 return moves;
153 }
154
155 getAllValidMoves() {
0e001022 156 let moves = super.getAllPotentialMoves();
78d64531 157 const color = this.turn;
0e001022 158 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
78d64531
BA
159 moves = moves.concat(
160 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
161 );
0e001022 162 }
78d64531
BA
163 return this.filterValid(moves);
164 }
165
166 atLeastOneMove() {
167 if (!super.atLeastOneMove()) {
168 // Search one reserve move
169 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
170 let moves = this.filterValid(
171 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
172 );
173 if (moves.length > 0) return true;
174 }
175 return false;
176 }
177 return true;
178 }
179
180 canTake([x1, y1], [x2, y2]) {
181 // Self-captures allowed, except for the king:
182 return this.getPiece(x2, y2) != V.KING;
183 }
184
32f6285e
BA
185 prePlay(move) {
186 super.prePlay(move);
2c5d7b20
BA
187 // Skip castle:
188 if (move.vanish.length == 2 && move.appear.length == 2) return;
32f6285e 189 const color = this.turn;
3a2a7b5f
BA
190 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]--;
191 else if (move.vanish.length == 2 && move.vanish[1].c == color)
78d64531
BA
192 // Self-capture
193 this.reserve[color][move.vanish[1].p]++;
78d64531
BA
194 }
195
3a2a7b5f
BA
196 postUndo(move) {
197 super.postUndo(move);
78d64531
BA
198 if (move.vanish.length == 2 && move.appear.length == 2) return;
199 const color = this.turn;
3a2a7b5f
BA
200 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]++;
201 else if (move.vanish.length == 2 && move.vanish[1].c == color)
78d64531 202 this.reserve[color][move.vanish[1].p]--;
78d64531
BA
203 }
204
205 static get SEARCH_DEPTH() {
206 return 2;
207 }
208
209 evalPosition() {
210 let evaluation = super.evalPosition();
211 // Add reserves:
212 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
213 const p = V.RESERVE_PIECES[i];
214 evaluation += this.reserve["w"][p] * V.VALUES[p];
215 evaluation -= this.reserve["b"][p] * V.VALUES[p];
216 }
217 return evaluation;
218 }
219
220 getNotation(move) {
221 const finalSquare = V.CoordsToSquare(move.end);
222 if (move.vanish.length > 0) {
223 if (move.appear.length > 0) {
224 // Standard move
225 return super.getNotation(move);
226 } else {
227 // Pawn fallen: capturing or not
228 let res = "";
229 if (move.vanish.length == 2)
230 res += V.CoordToColumn(move.start.y) + "x";
231 return res + finalSquare;
232 }
233 }
234 // Rebirth:
235 const piece =
236 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
237 return piece + "@" + V.CoordsToSquare(move.end);
238 }
7e8a7ea1 239
78d64531 240};