Adjust Chakart rules
[vchess.git] / client / src / variants / Recycle.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3
4 export class RecycleRules extends ChessRules {
5
6 static get PawnSpecs() {
7 return Object.assign(
8 {},
9 ChessRules.PawnSpecs,
10 { promotions: [V.PAWN] } //in fact, none
11 );
12 }
13
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(" ");
25 return Object.assign(
26 ChessRules.ParseFen(fen),
27 { reserve: fenParts[5] }
28 );
29 }
30
31 static GenRandInitFen(randomness) {
32 return ChessRules.GenRandInitFen(randomness) + " 0000000000";
33 }
34
35 getFen() {
36 return super.getFen() + " " + this.getReserveFen();
37 }
38
39 getFenForRepeat() {
40 return super.getFenForRepeat() + "_" + this.getReserveFen();
41 }
42
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);
58 // Also init reserves (used by the interface to show landable pieces)
59 const reserve =
60 V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
61 this.reserve = {
62 w: {
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]
68 },
69 b: {
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]
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 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
97 // Used by the interface:
98 getReservePpath(index, color) {
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]) {
137 if (x >= V.size.x)
138 // Reserves, outside of board: x == sizeX(+1)
139 return this.getReserveMoves([x, y]);
140 // Standard moves
141 return super.getPotentialMovesFrom([x, y]);
142 }
143
144 getPotentialPawnMoves([x, y]) {
145 let moves = super.getPotentialPawnMoves([x, y]);
146 // Remove pawns on 8th rank ("fallen"):
147 const color = this.turn;
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 });
152 return moves;
153 }
154
155 getAllValidMoves() {
156 let moves = super.getAllPotentialMoves();
157 const color = this.turn;
158 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
159 moves = moves.concat(
160 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
161 );
162 }
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
185 prePlay(move) {
186 super.prePlay(move);
187 // Skip castle:
188 if (move.vanish.length == 2 && move.appear.length == 2) return;
189 const color = this.turn;
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)
192 // Self-capture
193 this.reserve[color][move.vanish[1].p]++;
194 }
195
196 postUndo(move) {
197 super.postUndo(move);
198 if (move.vanish.length == 2 && move.appear.length == 2) return;
199 const color = this.turn;
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)
202 this.reserve[color][move.vanish[1].p]--;
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 }
239
240 };