Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Schess.js
CommitLineData
305ede7e
BA
1import { ChessRules, PiPo } from "@/base_rules";
2
3export class SchessRules extends ChessRules {
7e8a7ea1 4
305ede7e
BA
5 static get PawnSpecs() {
6 return Object.assign(
7 {},
8 ChessRules.PawnSpecs,
9 {
10 promotions:
11 ChessRules.PawnSpecs.promotions.concat([V.HAWK, V.ELEPHANT])
12 }
13 );
14 }
15
16 static get HAWK() {
17 return 'h';
18 }
19
20 static get ELEPHANT() {
21 return 'e';
22 }
23
4ec83d37
BA
24 static get NOTHING() {
25 return 'o';
26 }
27
305ede7e
BA
28 static get PIECES() {
29 return ChessRules.PIECES.concat([V.HAWK, V.ELEPHANT]);
30 }
31
32 getPpath(b) {
4ec83d37 33 if ([V.HAWK, V.ELEPHANT, V.NOTHING].includes(b[1])) return "Schess/" + b;
305ede7e
BA
34 return b;
35 }
36
305ede7e
BA
37 static IsGoodFen(fen) {
38 if (!ChessRules.IsGoodFen(fen)) return false;
39 const fenParsed = V.ParseFen(fen);
40 // Check pocket state
41 if (!fenParsed.pocket || !fenParsed.pocket.match(/^[0-1]{4,4}$/))
42 return false;
43 return true;
44 }
45
46 static IsGoodFlags(flags) {
47 // 4 for castle + 16 for generators
48 return !!flags.match(/^[a-z]{4,4}[01]{16,16}$/);
49 }
50
51 setFlags(fenflags) {
52 super.setFlags(fenflags); //castleFlags
53 this.pieceFlags = {
0d5335de 54 w: [...Array(8)], //pieces can generate Hawk or Elephant?
305ede7e
BA
55 b: [...Array(8)]
56 };
57 const flags = fenflags.substr(4); //skip first 4 letters, for castle
58 for (let c of ["w", "b"]) {
59 for (let i = 0; i < 8; i++)
60 this.pieceFlags[c][i] = flags.charAt((c == "w" ? 0 : 8) + i) == "1";
61 }
62 }
63
64 aggregateFlags() {
65 return [this.castleFlags, this.pieceFlags];
66 }
67
68 disaggregateFlags(flags) {
69 this.castleFlags = flags[0];
70 this.pieceFlags = flags[1];
71 }
72
73 static ParseFen(fen) {
74 const fenParts = fen.split(" ");
75 return Object.assign(
76 ChessRules.ParseFen(fen),
77 { pocket: fenParts[5] }
78 );
79 }
80
4313762d 81 static GenRandInitFen(options) {
305ede7e 82 return (
4313762d 83 ChessRules.GenRandInitFen(options).slice(0, -2) +
305ede7e
BA
84 // Add pieceFlags + pocket
85 "1111111111111111 - 1111"
86 );
87 }
88
89 getFen() {
90 return (
91 super.getFen() + " " +
92 this.getPocketFen()
93 );
94 }
95
96 getFenForRepeat() {
97 return (
98 super.getFenForRepeat() + "_" +
99 this.getPocketFen()
100 );
101 }
102
103 getFlagsFen() {
104 let fen = super.getFlagsFen();
105 // Add pieces flags
106 for (let c of ["w", "b"])
107 for (let i = 0; i < 8; i++) fen += (this.pieceFlags[c][i] ? "1" : "0");
108 return fen;
109 }
110
111 getPocketFen() {
112 let res = "";
113 for (let c of ["w", "b"])
114 res += this.pocket[c][V.HAWK] + this.pocket[c][V.ELEPHANT];
115 return res;
116 }
117
118 setOtherVariables(fen) {
119 super.setOtherVariables(fen);
120 const fenParsed = V.ParseFen(fen);
121 this.pocket = {
122 "w": {
e50a8025
BA
123 h: parseInt(fenParsed.pocket[0], 10),
124 e: parseInt(fenParsed.pocket[1], 10)
305ede7e
BA
125 },
126 "b": {
e50a8025
BA
127 h: parseInt(fenParsed.pocket[2], 10),
128 e: parseInt(fenParsed.pocket[3], 10)
305ede7e
BA
129 }
130 };
131 }
132
133 getPotentialMovesFrom([x, y]) {
134 let moves = undefined;
135 switch (this.getPiece(x, y)) {
136 case V.HAWK:
137 moves = this.getPotentialHawkMoves([x, y]);
138 break;
139 case V.ELEPHANT:
140 moves = this.getPotentialElephantMoves([x, y]);
141 break;
142 default:
143 moves = super.getPotentialMovesFrom([x, y]);
144 }
4ec83d37
BA
145 // For moves presentation when choices:
146 const unshiftNothing = (m) => {
147 const a = m.appear[0];
148 m.appear.unshift(new PiPo({
149 p: V.NOTHING,
150 c: 'o',
151 x: a.x,
152 y: a.y
153 }));
154 };
305ede7e
BA
155 // Post-processing: add choices for hawk and elephant,
156 // except for moves letting the king under check.
157 const color = this.turn;
158 if (Object.values(this.pocket[color]).some(v => v > 0)) {
159 const firstRank = (color == "w" ? 7 : 0);
4ec83d37 160 let validMoves = [];
305ede7e
BA
161 moves.forEach(m => {
162 let inCheckAfter = false;
163 this.play(m);
164 if (this.underCheck(color)) inCheckAfter = true;
165 this.undo(m);
166 if (!inCheckAfter) {
167 for (let pp of ['h', 'e']) {
168 if (this.pocket[color][pp] > 0) {
4ec83d37 169 let shift = (m.appear[0].p == V.NOTHING ? 1 : 0);
305ede7e
BA
170 if (
171 m.start.x == firstRank &&
172 this.pieceFlags[color][m.start.y] &&
173 (
4ec83d37 174 m.appear.length == shift+1 ||
305ede7e 175 // Special castle case: is initial king square free?
2c5d7b20
BA
176 ![m.appear[shift].y, m.appear[shift+1].y]
177 .includes(m.vanish[0].y)
305ede7e
BA
178 )
179 ) {
180 let pMove = JSON.parse(JSON.stringify(m));
4ec83d37 181 if (shift == 1) pMove.appear.shift();
305ede7e
BA
182 // NOTE: unshift instead of push, for choices presentation
183 pMove.appear.unshift(new PiPo({
184 p: pp,
185 c: color,
186 x: x,
187 y: y
188 }));
4ec83d37
BA
189 validMoves.push(pMove);
190 if (shift == 0) unshiftNothing(m);
305ede7e 191 }
4ec83d37 192 shift = (m.appear[0].p == V.NOTHING ? 1 : 0);
305ede7e 193 if (
58bf4670 194 m.appear.length >= 2 + shift &&
4ec83d37 195 m.vanish.length == 2 &&
2c5d7b20
BA
196 ![m.appear[shift].y, m.appear[shift+1].y]
197 .includes(m.vanish[1].y)
305ede7e
BA
198 ) {
199 // Special castle case: rook flag was necessarily on
200 let pMove = JSON.parse(JSON.stringify(m));
4ec83d37 201 if (shift == 1) pMove.appear.shift();
305ede7e
BA
202 pMove.appear.unshift(new PiPo({
203 p: pp,
204 c: color,
205 x: m.vanish[1].x,
206 y: m.vanish[1].y
207 }));
4ec83d37
BA
208 validMoves.push(pMove);
209 if (shift == 0) unshiftNothing(m);
305ede7e
BA
210 }
211 }
212 }
4ec83d37
BA
213 // Unshift, to show the empty square on the left:
214 validMoves.unshift(m);
305ede7e
BA
215 }
216 });
4ec83d37 217 moves = validMoves;
305ede7e
BA
218 }
219 return moves;
220 }
221
222 getPotentialHawkMoves(sq) {
40ac07d9 223 return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat(
4313762d 224 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], 1)
305ede7e
BA
225 );
226 }
227
228 getPotentialElephantMoves(sq) {
40ac07d9 229 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat(
4313762d 230 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], 1)
305ede7e
BA
231 );
232 }
233
234 isAttacked(sq, color) {
235 return (
236 super.isAttacked(sq, color) ||
237 this.isAttackedByHawk(sq, color) ||
238 this.isAttackedByElephant(sq, color)
239 );
240 }
241
242 isAttackedByHawk(sq, color) {
40ac07d9
BA
243 return (
244 this.isAttackedBySlideNJump(sq, color, V.HAWK, V.steps[V.BISHOP]) ||
4313762d 245 this.isAttackedBySlideNJump(sq, color, V.HAWK, V.steps[V.KNIGHT], 1)
305ede7e
BA
246 );
247 }
248
249 isAttackedByElephant(sq, color) {
40ac07d9
BA
250 return (
251 this.isAttackedBySlideNJump(sq, color, V.ELEPHANT, V.steps[V.ROOK]) ||
4313762d 252 this.isAttackedBySlideNJump(sq, color, V.ELEPHANT, V.steps[V.KNIGHT], 1)
305ede7e
BA
253 );
254 }
255
4ec83d37
BA
256 filterValid(moves) {
257 if (Object.values(this.pocket[this.turn]).some(v => v > 0))
258 // Undercheck tests done in getPotentialMovesFrom()
259 return moves;
260 return super.filterValid(moves);
261 }
262
305ede7e
BA
263 prePlay(move) {
264 super.prePlay(move);
265 if (move.appear.length >= 2) {
266 if ([V.HAWK, V.ELEPHANT].includes(move.appear[0].p)) {
267 // A pocket piece is used
268 const color = this.turn;
269 this.pocket[color][move.appear[0].p] = 0;
270 }
271 }
272 }
273
274 postPlay(move) {
305ede7e 275 const color = move.vanish[0].c;
4ec83d37
BA
276 const piece = move.vanish[0].p;
277 // Update king position + flags
278 if (piece == V.KING) {
279 const shift =
280 ([V.HAWK, V.ELEPHANT, V.NOTHING].includes(move.appear[0].p) ? 1 : 0);
281 this.kingPos[color][0] = move.appear[shift].x;
282 this.kingPos[color][1] = move.appear[shift].y;
4ec83d37
BA
283 }
284 this.updateCastleFlags(move, piece);
285
0d5335de 286 const oppCol = this.turn;
305ede7e
BA
287 const firstRank = (color == 'w' ? 7 : 0);
288 const oppFirstRank = 7 - firstRank;
289 // Does this move turn off a piece init square flag?
290 if (move.start.x == firstRank) {
291 if (this.pieceFlags[color][move.start.y])
292 this.pieceFlags[color][move.start.y] = false;
293 // Special castle case:
4ec83d37 294 if (move.appear.length >= 2 && move.vanish.length == 2) {
305ede7e
BA
295 const L = move.appear.length;
296 if (move.appear[L-1].p == V.ROOK)
297 this.pieceFlags[color][move.vanish[1].y] = false;
298 }
299 }
300 if (move.end.x == oppFirstRank && this.pieceFlags[oppCol][move.end.y])
301 this.pieceFlags[oppCol][move.end.y] = false;
302 }
303
304 postUndo(move) {
305 super.postUndo(move);
306 if (move.appear.length >= 2) {
307 if ([V.HAWK, V.ELEPHANT].includes(move.appear[0].p)) {
308 // A pocket piece was used
309 const color = this.turn;
310 this.pocket[color][move.appear[0].p] = 1;
311 }
312 }
313 }
314
315 static get SEARCH_DEPTH() {
316 return 2;
317 }
318
319 static get VALUES() {
320 return Object.assign(
4313762d
BA
321 {
322 'h': 5,
323 'e': 7
324 },
325 ChessRules.VALUES
305ede7e
BA
326 );
327 }
328
329 getNotation(move) {
4ec83d37
BA
330 if (move.appear.length >= 2) {
331 const pPieceAppear = [V.HAWK, V.ELEPHANT].includes(move.appear[0].p);
332 const nothingAppear = (move.appear[0].p == V.NOTHING);
333 if (pPieceAppear || nothingAppear) {
334 let suffix = "";
42d9f8eb
BA
335 if (pPieceAppear) {
336 suffix = "/" + move.appear[0].p.toUpperCase();
337 if (move.appear.length == 3) {
338 // Castling; indicate square
339 suffix +=
340 V.CoordsToSquare({ x: move.appear[0].x, y: move.appear[0].y });
341 }
342 }
4ec83d37
BA
343 let cmove = JSON.parse(JSON.stringify(move));
344 cmove.appear.shift();
345 return super.getNotation(cmove) + suffix;
346 }
305ede7e
BA
347 }
348 return super.getNotation(move);
349 }
7e8a7ea1 350
305ede7e 351};