Fix Eightpieces
[vchess.git] / client / src / variants / Ball.js
1 import { ChessRules, Move, PiPo } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { shuffle } from "@/utils/alea";
4
5 export class BallRules extends ChessRules {
6
7 static get Lines() {
8 return [
9 // White goal:
10 [[0, 3], [0, 6]],
11 [[0, 6], [1, 6]],
12 [[1, 6], [1, 3]],
13 [[1, 3], [0, 3]],
14 // Black goal:
15 [[9, 3], [9, 6]],
16 [[9, 6], [8, 6]],
17 [[8, 6], [8, 3]],
18 [[8, 3], [9, 3]]
19 ];
20 }
21
22 static get PawnSpecs() {
23 return Object.assign(
24 {},
25 ChessRules.PawnSpecs,
26 { promotions: ChessRules.PawnSpecs.promotions.concat([V.PHOENIX]) }
27 );
28 }
29
30 static get HasFlags() {
31 return false;
32 }
33
34 static get PHOENIX() {
35 return 'h';
36 }
37
38 static get BALL() {
39 // 'b' is already taken:
40 return "aa";
41 }
42
43 static get HAS_BALL_CODE() {
44 return {
45 'p': 's',
46 'r': 'u',
47 'n': 'o',
48 'b': 'c',
49 'q': 't',
50 'k': 'l',
51 'h': 'i'
52 };
53 }
54
55 static get HAS_BALL_DECODE() {
56 return {
57 's': 'p',
58 'u': 'r',
59 'o': 'n',
60 'c': 'b',
61 't': 'q',
62 'l': 'k',
63 'i': 'h'
64 };
65 }
66
67 static get PIECES() {
68 return ChessRules.PIECES
69 .concat([V.PHOENIX])
70 .concat(Object.keys(V.HAS_BALL_DECODE))
71 .concat(['a']);
72 }
73
74 static board2fen(b) {
75 if (b == V.BALL) return 'a';
76 return ChessRules.board2fen(b);
77 }
78
79 static fen2board(f) {
80 if (f == 'a') return V.BALL;
81 return ChessRules.fen2board(f);
82 }
83
84 static ParseFen(fen) {
85 return Object.assign(
86 ChessRules.ParseFen(fen),
87 { pmove: fen.split(" ")[4] }
88 );
89 }
90
91 // Check that exactly one ball is on the board
92 // + at least one piece per color.
93 static IsGoodPosition(position) {
94 if (position.length == 0) return false;
95 const rows = position.split("/");
96 if (rows.length != V.size.x) return false;
97 let pieces = { "w": 0, "b": 0 };
98 const withBall = Object.keys(V.HAS_BALL_DECODE).concat(['a']);
99 let ballCount = 0;
100 for (let row of rows) {
101 let sumElts = 0;
102 for (let i = 0; i < row.length; i++) {
103 const lowerRi = row[i].toLowerCase();
104 if (V.PIECES.includes(lowerRi)) {
105 if (lowerRi != 'a') pieces[row[i] == lowerRi ? "b" : "w"]++;
106 if (withBall.includes(lowerRi)) ballCount++;
107 sumElts++;
108 }
109 else {
110 const num = parseInt(row[i], 10);
111 if (isNaN(num)) return false;
112 sumElts += num;
113 }
114 }
115 if (sumElts != V.size.y) return false;
116 }
117 if (ballCount != 1 || Object.values(pieces).some(v => v == 0))
118 return false;
119 return true;
120 }
121
122 static IsGoodFen(fen) {
123 if (!ChessRules.IsGoodFen(fen)) return false;
124 const fenParts = fen.split(" ");
125 if (fenParts.length != 5) return false;
126 if (
127 fenParts[4] != "-" &&
128 !fenParts[4].match(/^([a-i][1-9]){2,2}$/)
129 ) {
130 return false;
131 }
132 return true;
133 }
134
135 getPpath(b) {
136 let prefix = "";
137 const withPrefix =
138 Object.keys(V.HAS_BALL_DECODE)
139 .concat([V.PHOENIX])
140 .concat(['a', 'w']); //TODO: 'w' for backward compatibility - to remove
141 if (withPrefix.includes(b[1])) prefix = "Ball/";
142 return prefix + b;
143 }
144
145 getPPpath(m) {
146 if (
147 m.vanish.length == 2 &&
148 m.appear.length == 2 &&
149 m.appear[0].c != m.appear[1].c
150 ) {
151 // Take ball in place (from opponent)
152 return "Ball/inplace";
153 }
154 return super.getPPpath(m);
155 }
156
157 canTake([x1, y1], [x2, y2]) {
158 if (this.getColor(x1, y1) !== this.getColor(x2, y2)) {
159 // The piece holding the ball cannot capture:
160 return (
161 !(Object.keys(V.HAS_BALL_DECODE)
162 .includes(this.board[x1][y1].charAt(1)))
163 );
164 }
165 // Pass: possible only if one of the friendly pieces has the ball
166 return (
167 Object.keys(V.HAS_BALL_DECODE).includes(this.board[x1][y1].charAt(1)) ||
168 Object.keys(V.HAS_BALL_DECODE).includes(this.board[x2][y2].charAt(1))
169 );
170 }
171
172 getFen() {
173 return super.getFen() + " " + this.getPmoveFen();
174 }
175
176 getFenForRepeat() {
177 return super.getFenForRepeat() + "_" + this.getPmoveFen();
178 }
179
180 getPmoveFen() {
181 const L = this.pmoves.length;
182 if (!this.pmoves[L-1]) return "-";
183 return (
184 V.CoordsToSquare(this.pmoves[L-1].start) +
185 V.CoordsToSquare(this.pmoves[L-1].end)
186 );
187 }
188
189 static GenRandInitFen(options) {
190 if (options.randomness == 0)
191 return "hbnrqrnhb/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/HBNRQRNHB w 0 - -";
192
193 let pieces = { w: new Array(9), b: new Array(9) };
194 for (let c of ["w", "b"]) {
195 if (c == 'b' && options.randomness == 1) {
196 pieces['b'] = pieces['w'];
197 break;
198 }
199
200 // Get random squares for every piece, with bishops and phoenixes
201 // on different colors:
202 let positions = shuffle(ArrayFun.range(9));
203 const composition = ['b', 'b', 'h', 'h', 'n', 'n', 'r', 'r', 'q'];
204 let rem2 = positions[0] % 2;
205 if (rem2 == positions[1] % 2) {
206 // Fix bishops (on different colors)
207 for (let i=4; i<9; i++) {
208 if (positions[i] % 2 != rem2) {
209 [positions[1], positions[i]] = [positions[i], positions[1]];
210 break;
211 }
212 }
213 }
214 rem2 = positions[2] % 2;
215 if (rem2 == positions[3] % 2) {
216 // Fix phoenixes too:
217 for (let i=4; i<9; i++) {
218 if (positions[i] % 2 != rem2)
219 [positions[3], positions[i]] = [positions[i], positions[3]];
220 }
221 }
222 for (let i = 0; i < 9; i++) pieces[c][positions[i]] = composition[i];
223 }
224 return (
225 pieces["b"].join("") +
226 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
227 pieces["w"].join("").toUpperCase() +
228 " w 0 - -"
229 );
230 }
231
232 scanKings() {}
233
234 setOtherVariables(fen) {
235 super.setOtherVariables(fen);
236 const pmove = V.ParseFen(fen).pmove;
237 // Local stack of "pass moves" (no need for appear & vanish)
238 this.pmoves = [
239 pmove != "-"
240 ?
241 {
242 start: V.SquareToCoords(pmove.substr(0, 2)),
243 end: V.SquareToCoords(pmove.substr(2))
244 }
245 : null
246 ];
247 }
248
249 static get size() {
250 return { x: 9, y: 9 };
251 }
252
253 getPiece(i, j) {
254 const p = this.board[i][j].charAt(1);
255 if (Object.keys(V.HAS_BALL_DECODE).includes(p))
256 return V.HAS_BALL_DECODE[p];
257 return p;
258 }
259
260 static get steps() {
261 return Object.assign(
262 {},
263 ChessRules.steps,
264 // Add phoenix moves
265 {
266 h: [
267 [-2, -2],
268 [-2, 2],
269 [2, -2],
270 [2, 2],
271 [-1, 0],
272 [1, 0],
273 [0, -1],
274 [0, 1]
275 ]
276 }
277 );
278 }
279
280 // Because of the ball, getPiece() could be wrong:
281 // use board[x][y][1] instead (always valid).
282 getBasicMove([sx, sy], [ex, ey], tr) {
283 const initColor = this.getColor(sx, sy);
284 const initPiece = this.board[sx][sy].charAt(1);
285 let mv = new Move({
286 appear: [
287 new PiPo({
288 x: ex,
289 y: ey,
290 c: tr ? tr.c : initColor,
291 p: tr ? tr.p : initPiece
292 })
293 ],
294 vanish: [
295 new PiPo({
296 x: sx,
297 y: sy,
298 c: initColor,
299 p: initPiece
300 })
301 ]
302 });
303
304 // Fix "ball holding" indication in case of promotions:
305 if (!!tr && Object.keys(V.HAS_BALL_DECODE).includes(initPiece))
306 mv.appear[0].p = V.HAS_BALL_CODE[tr.p];
307
308 // The opponent piece disappears if we take it
309 if (this.board[ex][ey] != V.EMPTY) {
310 mv.vanish.push(
311 new PiPo({
312 x: ex,
313 y: ey,
314 c: this.getColor(ex, ey),
315 p: this.board[ex][ey].charAt(1)
316 })
317 );
318 }
319
320 // Post-processing: maybe the ball was taken, or a piece + ball,
321 // or maybe a pass (ball <--> piece)
322 if (mv.vanish.length == 2) {
323 if (
324 // Take the ball?
325 mv.vanish[1].c == 'a' ||
326 // Capture a ball-holding piece? If friendly one, then adjust
327 Object.keys(V.HAS_BALL_DECODE).includes(mv.vanish[1].p)
328 ) {
329 mv.appear[0].p = V.HAS_BALL_CODE[mv.appear[0].p];
330 if (mv.vanish[1].c == mv.vanish[0].c) {
331 // "Capturing" self => pass
332 mv.appear[0].x = mv.start.x;
333 mv.appear[0].y = mv.start.y;
334 mv.appear.push(
335 new PiPo({
336 x: mv.end.x,
337 y: mv.end.y,
338 p: V.HAS_BALL_DECODE[mv.vanish[1].p],
339 c: mv.vanish[0].c
340 })
341 );
342 }
343 }
344 else if (mv.vanish[1].c == mv.vanish[0].c) {
345 // Pass the ball: the passing unit does not disappear
346 mv.appear.push(JSON.parse(JSON.stringify(mv.vanish[0])));
347 mv.appear[0].p = V.HAS_BALL_CODE[mv.vanish[1].p];
348 mv.appear[1].p = V.HAS_BALL_DECODE[mv.appear[1].p];
349 }
350 // Else: standard capture
351 }
352
353 return mv;
354 }
355
356 // NOTE: if a pawn captures en-passant, he doesn't hold the ball
357 // So base implementation is fine.
358
359 getPotentialMovesFrom([x, y]) {
360 let moves = undefined;
361 const piece = this.getPiece(x, y);
362 if (piece == V.PHOENIX)
363 moves = this.getPotentialPhoenixMoves([x, y]);
364 else moves = super.getPotentialMovesFrom([x, y]);
365 // Add "taking ball in place" move (at most one in list)
366 for (let m of moves) {
367 if (
368 m.vanish.length == 2 &&
369 m.vanish[1].p != 'a' &&
370 m.vanish[0].c != m.vanish[1].c &&
371 Object.keys(V.HAS_BALL_DECODE).includes(m.appear[0].p)
372 ) {
373 const color = this.turn;
374 const oppCol = V.GetOppCol(color);
375 moves.push(
376 new Move({
377 appear: [
378 new PiPo({
379 x: x,
380 y: y,
381 c: color,
382 p: m.appear[0].p
383 }),
384 new PiPo({
385 x: m.vanish[1].x,
386 y: m.vanish[1].y,
387 c: oppCol,
388 p: V.HAS_BALL_DECODE[m.vanish[1].p]
389 })
390 ],
391 vanish: [
392 new PiPo({
393 x: x,
394 y: y,
395 c: color,
396 p: piece
397 }),
398 new PiPo({
399 x: m.vanish[1].x,
400 y: m.vanish[1].y,
401 c: oppCol,
402 p: m.vanish[1].p
403 })
404 ],
405 end: { x: m.end.x, y: m.end.y }
406 })
407 );
408 break;
409 }
410 }
411 return moves;
412 }
413
414 getSlideNJumpMoves(sq, steps, nbSteps) {
415 // "Sliders": at most 3 steps
416 return super.getSlideNJumpMoves(sq, steps, !nbSteps ? 3 : 1);
417 }
418
419 getPotentialPhoenixMoves(sq) {
420 return super.getSlideNJumpMoves(sq, V.steps[V.PHOENIX], 1);
421 }
422
423 getPmove(move) {
424 if (
425 move.vanish.length == 2 &&
426 move.appear.length == 2 &&
427 move.appear[0].c != move.appear[1].c
428 ) {
429 // In-place pass:
430 return {
431 start: move.start,
432 end: move.end
433 };
434 }
435 return null;
436 }
437
438 oppositePasses(m1, m2) {
439 return (
440 m1.start.x == m2.end.x &&
441 m1.start.y == m2.end.y &&
442 m1.end.x == m2.start.x &&
443 m1.end.y == m2.start.y
444 );
445 }
446
447 filterValid(moves) {
448 const L = this.pmoves.length;
449 const lp = this.pmoves[L-1];
450 if (!lp) return moves;
451 return moves.filter(m => {
452 return (
453 m.vanish.length == 1 ||
454 m.appear.length == 1 ||
455 m.appear[0].c == m.appear[1].c ||
456 !this.oppositePasses(lp, m)
457 );
458 });
459 }
460
461 // isAttacked: unused here (no checks)
462
463 postPlay(move) {
464 this.pmoves.push(this.getPmove(move));
465 }
466
467 postUndo() {
468 this.pmoves.pop();
469 }
470
471 getCheckSquares() {
472 return [];
473 }
474
475 getCurrentScore() {
476 // Turn has changed:
477 const color = V.GetOppCol(this.turn);
478 const lastRank = (color == "w" ? 0 : 8);
479 if ([3,4,5].some(
480 i => {
481 return (
482 Object.keys(V.HAS_BALL_DECODE).includes(
483 this.board[lastRank][i].charAt(1)) &&
484 this.getColor(lastRank, i) == color
485 );
486 }
487 )) {
488 // Goal scored!
489 return color == "w" ? "1-0" : "0-1";
490 }
491 if (this.atLeastOneMove()) return "*";
492 // Stalemate (quite unlikely?)
493 return "1/2";
494 }
495
496 static get VALUES() {
497 return {
498 p: 1,
499 r: 3,
500 n: 3,
501 b: 2,
502 q: 5,
503 h: 3,
504 a: 0 //ball: neutral
505 };
506 }
507
508 static get SEARCH_DEPTH() {
509 return 2;
510 }
511
512 evalPosition() {
513 // Count material:
514 let evaluation = super.evalPosition();
515 if (this.board[4][4] == V.BALL)
516 // Ball not captured yet
517 return evaluation;
518 // Ponder depending on ball position
519 for (let i=0; i<9; i++) {
520 for (let j=0; j<9; j++) {
521 if (Object.keys(V.HAS_BALL_DECODE).includes(this.board[i][j][1]))
522 return evaluation/2 + (this.getColor(i, j) == "w" ? 8 - i : -i);
523 }
524 }
525 return 0; //never reached
526 }
527
528 getNotation(move) {
529 const finalSquare = V.CoordsToSquare(move.end);
530 if (move.appear.length == 2)
531 // A pass: special notation
532 return V.CoordsToSquare(move.start) + "P" + finalSquare;
533 const piece = this.getPiece(move.start.x, move.start.y);
534 if (piece == V.PAWN) {
535 // Pawn move
536 let notation = "";
537 if (move.vanish.length > move.appear.length) {
538 // Capture
539 const startColumn = V.CoordToColumn(move.start.y);
540 notation = startColumn + "x" + finalSquare;
541 }
542 else notation = finalSquare;
543 if (![V.PAWN, V.HAS_BALL_CODE[V.PAWN]].includes(move.appear[0].p)) {
544 // Promotion
545 const promotePiece =
546 V.HAS_BALL_DECODE[move.appear[0].p] || move.appear[0].p;
547 notation += "=" + promotePiece.toUpperCase();
548 }
549 return notation;
550 }
551 // Piece movement
552 return (
553 piece.toUpperCase() +
554 (move.vanish.length > move.appear.length ? "x" : "") +
555 finalSquare
556 );
557 }
558
559 };