a9991316618dfcfaa9c94bc9b2dd6e2f22bb485b
[vchess.git] / client / src / variants / Clorange.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3
4 export class ClorangeRules extends ChessRules {
5 static get PawnSpecs() {
6 return Object.assign(
7 {},
8 ChessRules.PawnSpecs,
9 // TODO: pawns reaching last rank promote normally? Seems better
10 { promotions: [V.PAWN] }
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]{20,20}$/))
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 // Capturing and non-capturing reserves:
33 return ChessRules.GenRandInitFen(randomness) + " 00000000000000000000";
34 }
35
36 getFen() {
37 return super.getFen() + " " + this.getReserveFen();
38 }
39
40 getFenForRepeat() {
41 return super.getFenForRepeat() + "_" + this.getReserveFen();
42 }
43
44 getReserveFen() {
45 let counts = new Array(10);
46 for (
47 let i = 0;
48 i < V.PIECES.length - 1;
49 i++ //-1: no king reserve
50 ) {
51 // TODO: adapt
52 counts[i] = this.reserve["w"][V.PIECES[i]];
53 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
54 }
55 return counts.join("");
56 }
57
58 setOtherVariables(fen) {
59 super.setOtherVariables(fen);
60 const fenParsed = V.ParseFen(fen);
61 // Also init reserves (used by the interface to show landable pieces)
62 // TODO: adapt
63 this.reserve = {
64 w: {
65 [V.PAWN]: parseInt(fenParsed.reserve[0]),
66 [V.ROOK]: parseInt(fenParsed.reserve[1]),
67 [V.KNIGHT]: parseInt(fenParsed.reserve[2]),
68 [V.BISHOP]: parseInt(fenParsed.reserve[3]),
69 [V.QUEEN]: parseInt(fenParsed.reserve[4])
70 },
71 b: {
72 [V.PAWN]: parseInt(fenParsed.reserve[5]),
73 [V.ROOK]: parseInt(fenParsed.reserve[6]),
74 [V.KNIGHT]: parseInt(fenParsed.reserve[7]),
75 [V.BISHOP]: parseInt(fenParsed.reserve[8]),
76 [V.QUEEN]: parseInt(fenParsed.reserve[9])
77 }
78 };
79 }
80
81 getColor(i, j) {
82 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
83 return this.board[i][j].charAt(0);
84 }
85
86 getPiece(i, j) {
87 if (i >= V.size.x) return V.RESERVE_PIECES[j];
88 return this.board[i][j].charAt(1);
89 }
90
91 getReservePpath(index, color) {
92 return color + V.RESERVE_PIECES[index];
93 }
94
95 static get NON_VIOLENT() {
96 return ['s', 'u', 'o', 'c', 't', 'l'];
97 }
98
99 // Ordering on reserve pieces
100 static get RESERVE_PIECES() {
101 return ChessRules.PIECES.concat(V.NON_VIOLENT);
102 }
103
104 getReserveMoves([x, y]) {
105 const color = this.turn;
106 const p = V.RESERVE_PIECES[y];
107 if (this.reserve[color][p] == 0) return [];
108 let moves = [];
109 const pawnShift = p == V.PAWN ? 1 : 0;
110 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
111 for (let j = 0; j < V.size.y; j++) {
112 if (this.board[i][j] == V.EMPTY) {
113 let mv = new Move({
114 appear: [
115 new PiPo({
116 x: i,
117 y: j,
118 c: color,
119 p: p
120 })
121 ],
122 vanish: [],
123 start: { x: x, y: y }, //a bit artificial...
124 end: { x: i, y: j }
125 });
126 moves.push(mv);
127 }
128 }
129 }
130 return moves;
131 }
132
133 // TODO: adapt all below:
134 getPotentialMovesFrom([x, y]) {
135 if (x >= V.size.x) {
136 // Reserves, outside of board: x == sizeX(+1)
137 return this.getReserveMoves([x, y]);
138 }
139 // Standard moves
140 return super.getPotentialMovesFrom([x, y]);
141 }
142
143 getPotentialPawnMoves([x, y]) {
144
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 };