1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class BallRules
extends ChessRules
{
22 static get PawnSpecs() {
26 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.PHOENIX
]) }
30 static get HasFlags() {
34 static get PHOENIX() {
39 // 'b' is already taken:
43 static get HAS_BALL_CODE() {
55 static get HAS_BALL_DECODE() {
68 return ChessRules
.PIECES
70 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
75 if (b
== V
.BALL
) return 'a';
76 return ChessRules
.board2fen(b
);
80 if (f
== 'a') return V
.BALL
;
81 return ChessRules
.fen2board(f
);
84 static ParseFen(fen
) {
86 ChessRules
.ParseFen(fen
),
87 { pmove: fen
.split(" ")[4] }
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']);
100 for (let row
of rows
) {
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
++;
110 const num
= parseInt(row
[i
], 10);
111 if (isNaN(num
)) return false;
115 if (sumElts
!= V
.size
.y
) return false;
117 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
122 static IsGoodFen(fen
) {
123 if (!ChessRules
.IsGoodFen(fen
)) return false;
124 const fenParts
= fen
.split(" ");
125 if (fenParts
.length
!= 5) return false;
127 fenParts
[4] != "-" &&
128 !fenParts
[4].match(/^([a-i][1-9]){2,2}$/)
138 Object
.keys(V
.HAS_BALL_DECODE
)
141 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
147 m
.vanish
.length
== 2 &&
148 m
.appear
.length
== 2 &&
149 m
.appear
[0].c
!= m
.appear
[1].c
151 // Take ball in place (from opponent)
152 return "Ball/inplace";
154 return super.getPPpath(m
);
157 canTake([x1
, y1
], [x2
, y2
]) {
158 if (this.getColor(x1
, y1
) !== this.getColor(x2
, y2
)) {
159 // The piece holding the ball cannot capture:
161 !(Object
.keys(V
.HAS_BALL_DECODE
)
162 .includes(this.board
[x1
][y1
].charAt(1)))
165 // Pass: possible only if one of the friendly pieces has the ball
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))
173 return super.getFen() + " " + this.getPmoveFen();
177 return super.getFenForRepeat() + "_" + this.getPmoveFen();
181 const L
= this.pmoves
.length
;
182 if (!this.pmoves
[L
-1]) return "-";
184 V
.CoordsToSquare(this.pmoves
[L
-1].start
) +
185 V
.CoordsToSquare(this.pmoves
[L
-1].end
)
189 static GenRandInitFen(randomness
) {
191 return "hbnrqrnhb/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/HBNRQRNHB w 0 - -";
193 let pieces
= { w: new Array(9), b: new Array(9) };
194 for (let c
of ["w", "b"]) {
195 if (c
== 'b' && randomness
== 1) {
196 pieces
['b'] = pieces
['w'];
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]];
212 rem2
= positions
[2] % 2;
213 if (rem2
== positions
[3] % 2) {
214 // Fix phoenixes too:
215 for (let i
=4; i
<9; i
++) {
216 if (positions
[i
] % 2 != rem2
)
217 [positions
[3], positions
[i
]] = [positions
[i
], positions
[3]];
220 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
223 pieces
["b"].join("") +
224 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
225 pieces
["w"].join("").toUpperCase() +
232 setOtherVariables(fen
) {
233 super.setOtherVariables(fen
);
234 const pmove
= V
.ParseFen(fen
).pmove
;
235 // Local stack of "pass moves" (no need for appear & vanish)
240 start: V
.SquareToCoords(pmove
.substr(0, 2)),
241 end: V
.SquareToCoords(pmove
.substr(2))
248 return { x: 9, y: 9 };
252 const p
= this.board
[i
][j
].charAt(1);
253 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
254 return V
.HAS_BALL_DECODE
[p
];
259 return Object
.assign(
278 // Because of the ball, getPiece() could be wrong:
279 // use board[x][y][1] instead (always valid).
280 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
281 const initColor
= this.getColor(sx
, sy
);
282 const initPiece
= this.board
[sx
][sy
].charAt(1);
288 c: tr
? tr
.c : initColor
,
289 p: tr
? tr
.p : initPiece
302 // Fix "ball holding" indication in case of promotions:
303 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
304 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
306 // The opponent piece disappears if we take it
307 if (this.board
[ex
][ey
] != V
.EMPTY
) {
312 c: this.getColor(ex
, ey
),
313 p: this.board
[ex
][ey
].charAt(1)
318 // Post-processing: maybe the ball was taken, or a piece + ball,
319 // or maybe a pass (ball <--> piece)
320 if (mv
.vanish
.length
== 2) {
323 mv
.vanish
[1].c
== 'a' ||
324 // Capture a ball-holding piece? If friendly one, then adjust
325 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
327 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
328 if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
329 // "Capturing" self => pass
330 mv
.appear
[0].x
= mv
.start
.x
;
331 mv
.appear
[0].y
= mv
.start
.y
;
336 p: V
.HAS_BALL_DECODE
[mv
.vanish
[1].p
],
342 else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
343 // Pass the ball: the passing unit does not disappear
344 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
345 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
346 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
348 // Else: standard capture
354 // NOTE: if a pawn captures en-passant, he doesn't hold the ball
355 // So base implementation is fine.
357 getPotentialMovesFrom([x
, y
]) {
358 let moves
= undefined;
359 const piece
= this.getPiece(x
, y
);
360 if (piece
== V
.PHOENIX
)
361 moves
= this.getPotentialPhoenixMoves([x
, y
]);
362 else moves
= super.getPotentialMovesFrom([x
, y
]);
363 // Add "taking ball in place" move (at most one in list)
364 for (let m
of moves
) {
366 m
.vanish
.length
== 2 &&
367 m
.vanish
[1].p
!= 'a' &&
368 m
.vanish
[0].c
!= m
.vanish
[1].c
&&
369 Object
.keys(V
.HAS_BALL_DECODE
).includes(m
.appear
[0].p
)
371 const color
= this.turn
;
372 const oppCol
= V
.GetOppCol(color
);
386 p: V
.HAS_BALL_DECODE
[m
.vanish
[1].p
]
403 end: { x: m
.end
.x
, y: m
.end
.y
}
412 // "Sliders": at most 3 steps
413 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
415 outerLoop: for (let step
of steps
) {
419 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
420 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
421 if (oneStep
|| stepCount
== 3) continue outerLoop
;
426 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
427 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
432 getPotentialPhoenixMoves(sq
) {
433 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
438 move.vanish
.length
== 2 &&
439 move.appear
.length
== 2 &&
440 move.appear
[0].c
!= move.appear
[1].c
451 oppositePasses(m1
, m2
) {
453 m1
.start
.x
== m2
.end
.x
&&
454 m1
.start
.y
== m2
.end
.y
&&
455 m1
.end
.x
== m2
.start
.x
&&
456 m1
.end
.y
== m2
.start
.y
461 const L
= this.pmoves
.length
;
462 const lp
= this.pmoves
[L
-1];
463 if (!lp
) return moves
;
464 return moves
.filter(m
=> {
466 m
.vanish
.length
== 1 ||
467 m
.appear
.length
== 1 ||
468 m
.appear
[0].c
== m
.appear
[1].c
||
469 !this.oppositePasses(lp
, m
)
474 // isAttacked: unused here (no checks)
477 this.pmoves
.push(this.getPmove(move));
490 const color
= V
.GetOppCol(this.turn
);
491 const lastRank
= (color
== "w" ? 0 : 8);
495 Object
.keys(V
.HAS_BALL_DECODE
).includes(
496 this.board
[lastRank
][i
].charAt(1)) &&
497 this.getColor(lastRank
, i
) == color
502 return color
== "w" ? "1-0" : "0-1";
504 if (this.atLeastOneMove()) return "*";
505 // Stalemate (quite unlikely?)
509 static get VALUES() {
521 static get SEARCH_DEPTH() {
527 let evaluation
= super.evalPosition();
528 if (this.board
[4][4] == V
.BALL
)
529 // Ball not captured yet
531 // Ponder depending on ball position
532 for (let i
=0; i
<9; i
++) {
533 for (let j
=0; j
<9; j
++) {
534 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
535 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
538 return 0; //never reached
542 const finalSquare
= V
.CoordsToSquare(move.end
);
543 if (move.appear
.length
== 2)
544 // A pass: special notation
545 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
546 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
547 if (piece
== V
.PAWN
) {
550 if (move.vanish
.length
> move.appear
.length
) {
552 const startColumn
= V
.CoordToColumn(move.start
.y
);
553 notation
= startColumn
+ "x" + finalSquare
;
555 else notation
= finalSquare
;
556 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
559 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
560 notation
+= "=" + promotePiece
.toUpperCase();
566 piece
.toUpperCase() +
567 (move.vanish
.length
> move.appear
.length
? "x" : "") +