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