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
)
140 .concat(['a', 'w']); //TODO: 'w' for backward compatibility - to remove
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(options
) {
190 if (options
.randomness
== 0)
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' && options
.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]];
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]];
222 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
225 pieces
["b"].join("") +
226 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
227 pieces
["w"].join("").toUpperCase() +
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)
242 start: V
.SquareToCoords(pmove
.substr(0, 2)),
243 end: V
.SquareToCoords(pmove
.substr(2))
250 return { x: 9, y: 9 };
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
];
261 return Object
.assign(
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);
290 c: tr
? tr
.c : initColor
,
291 p: tr
? tr
.p : initPiece
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
];
308 // The opponent piece disappears if we take it
309 if (this.board
[ex
][ey
] != V
.EMPTY
) {
314 c: this.getColor(ex
, ey
),
315 p: this.board
[ex
][ey
].charAt(1)
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) {
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
)
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
;
338 p: V
.HAS_BALL_DECODE
[mv
.vanish
[1].p
],
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
];
350 // Else: standard capture
356 // NOTE: if a pawn captures en-passant, he doesn't hold the ball
357 // So base implementation is fine.
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
) {
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
)
373 const color
= this.turn
;
374 const oppCol
= V
.GetOppCol(color
);
388 p: V
.HAS_BALL_DECODE
[m
.vanish
[1].p
]
405 end: { x: m
.end
.x
, y: m
.end
.y
}
414 getSlideNJumpMoves(sq
, steps
, nbSteps
) {
415 // "Sliders": at most 3 steps
416 return super.getSlideNJumpMoves(sq
, steps
, !nbSteps
? 3 : 1);
419 getPotentialPhoenixMoves(sq
) {
420 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], 1);
425 move.vanish
.length
== 2 &&
426 move.appear
.length
== 2 &&
427 move.appear
[0].c
!= move.appear
[1].c
438 oppositePasses(m1
, m2
) {
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
448 const L
= this.pmoves
.length
;
449 const lp
= this.pmoves
[L
-1];
450 if (!lp
) return moves
;
451 return moves
.filter(m
=> {
453 m
.vanish
.length
== 1 ||
454 m
.appear
.length
== 1 ||
455 m
.appear
[0].c
== m
.appear
[1].c
||
456 !this.oppositePasses(lp
, m
)
461 // isAttacked: unused here (no checks)
464 this.pmoves
.push(this.getPmove(move));
477 const color
= V
.GetOppCol(this.turn
);
478 const lastRank
= (color
== "w" ? 0 : 8);
482 Object
.keys(V
.HAS_BALL_DECODE
).includes(
483 this.board
[lastRank
][i
].charAt(1)) &&
484 this.getColor(lastRank
, i
) == color
489 return color
== "w" ? "1-0" : "0-1";
491 if (this.atLeastOneMove()) return "*";
492 // Stalemate (quite unlikely?)
496 static get VALUES() {
508 static get SEARCH_DEPTH() {
514 let evaluation
= super.evalPosition();
515 if (this.board
[4][4] == V
.BALL
)
516 // Ball not captured yet
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
);
525 return 0; //never reached
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
) {
537 if (move.vanish
.length
> move.appear
.length
) {
539 const startColumn
= V
.CoordToColumn(move.start
.y
);
540 notation
= startColumn
+ "x" + finalSquare
;
542 else notation
= finalSquare
;
543 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
546 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
547 notation
+= "=" + promotePiece
.toUpperCase();
553 piece
.toUpperCase() +
554 (move.vanish
.length
> move.appear
.length
? "x" : "") +