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]];
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 // "Sliders": at most 3 steps
415 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
417 outerLoop: for (let step
of steps
) {
421 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
422 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
423 if (oneStep
|| stepCount
== 3) continue outerLoop
;
428 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
429 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
434 getPotentialPhoenixMoves(sq
) {
435 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
440 move.vanish
.length
== 2 &&
441 move.appear
.length
== 2 &&
442 move.appear
[0].c
!= move.appear
[1].c
453 oppositePasses(m1
, m2
) {
455 m1
.start
.x
== m2
.end
.x
&&
456 m1
.start
.y
== m2
.end
.y
&&
457 m1
.end
.x
== m2
.start
.x
&&
458 m1
.end
.y
== m2
.start
.y
463 const L
= this.pmoves
.length
;
464 const lp
= this.pmoves
[L
-1];
465 if (!lp
) return moves
;
466 return moves
.filter(m
=> {
468 m
.vanish
.length
== 1 ||
469 m
.appear
.length
== 1 ||
470 m
.appear
[0].c
== m
.appear
[1].c
||
471 !this.oppositePasses(lp
, m
)
476 // isAttacked: unused here (no checks)
479 this.pmoves
.push(this.getPmove(move));
492 const color
= V
.GetOppCol(this.turn
);
493 const lastRank
= (color
== "w" ? 0 : 8);
497 Object
.keys(V
.HAS_BALL_DECODE
).includes(
498 this.board
[lastRank
][i
].charAt(1)) &&
499 this.getColor(lastRank
, i
) == color
504 return color
== "w" ? "1-0" : "0-1";
506 if (this.atLeastOneMove()) return "*";
507 // Stalemate (quite unlikely?)
511 static get VALUES() {
523 static get SEARCH_DEPTH() {
529 let evaluation
= super.evalPosition();
530 if (this.board
[4][4] == V
.BALL
)
531 // Ball not captured yet
533 // Ponder depending on ball position
534 for (let i
=0; i
<9; i
++) {
535 for (let j
=0; j
<9; j
++) {
536 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
537 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
540 return 0; //never reached
544 const finalSquare
= V
.CoordsToSquare(move.end
);
545 if (move.appear
.length
== 2)
546 // A pass: special notation
547 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
548 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
549 if (piece
== V
.PAWN
) {
552 if (move.vanish
.length
> move.appear
.length
) {
554 const startColumn
= V
.CoordToColumn(move.start
.y
);
555 notation
= startColumn
+ "x" + finalSquare
;
557 else notation
= finalSquare
;
558 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
561 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
562 notation
+= "=" + promotePiece
.toUpperCase();
568 piece
.toUpperCase() +
569 (move.vanish
.length
> move.appear
.length
? "x" : "") +