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
{
21 static get PawnSpecs() {
25 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.PHOENIX
]) }
29 static get HasFlags() {
33 static get PHOENIX() {
38 // Ball is already taken:
42 static get HAS_BALL_CODE() {
54 static get HAS_BALL_DECODE() {
67 return ChessRules
.PIECES
69 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
74 if (b
== V
.BALL
) return 'a';
75 return ChessRules
.board2fen(b
);
79 if (f
== 'a') return V
.BALL
;
80 return ChessRules
.fen2board(f
);
83 static ParseFen(fen
) {
85 ChessRules
.ParseFen(fen
),
86 { pmove: fen
.split(" ")[4] }
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
]);
99 for (let row
of rows
) {
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
++;
108 const num
= parseInt(row
[i
]);
109 if (isNaN(num
)) return false;
113 if (sumElts
!= V
.size
.y
) return false;
115 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
120 static IsGoodFen(fen
) {
121 if (!ChessRules
.IsGoodFen(fen
)) return false;
122 const fenParts
= fen
.split(" ");
123 if (fenParts
.length
!= 5) return false;
125 fenParts
[4] != "-" &&
126 !fenParts
[4].match(/^([a-i][1-9]){2,2}$/)
136 Object
.keys(V
.HAS_BALL_DECODE
)
139 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
145 m
.vanish
.length
== 2 &&
146 m
.appear
.length
== 2 &&
147 m
.appear
[0].c
!= m
.appear
[1].c
149 // Take ball in place (from opponent)
150 return "Ball/inplace";
152 return super.getPPpath(m
);
155 canTake([x1
, y1
], [x2
, y2
]) {
156 if (this.getColor(x1
, y1
) !== this.getColor(x2
, y2
)) {
157 // The piece holding the ball cannot capture:
159 !(Object
.keys(V
.HAS_BALL_DECODE
)
160 .includes(this.board
[x1
][y1
].charAt(1)))
163 // Pass: possible only if one of the friendly pieces has the ball
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))
171 return super.getFen() + " " + this.getPmoveFen();
175 return super.getFenForRepeat() + "_" + this.getPmoveFen();
179 const L
= this.pmoves
.length
;
180 if (!this.pmoves
[L
-1]) return "-";
182 V
.CoordsToSquare(this.pmoves
[L
-1].start
) +
183 V
.CoordsToSquare(this.pmoves
[L
-1].end
)
187 static GenRandInitFen(randomness
) {
189 return "hbnrqrnhb/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/HBNRQRNHB w 0 - -";
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'];
198 // Get random squares for every piece, with bishops and phoenixes
199 // on different colors:
200 let positions
= shuffle(ArrayFun
.range(9));
201 const composition
= ['b', 'b', 'h', 'h', 'n', 'n', 'r', 'r', 'q'];
202 let rem2
= positions
[0] % 2;
203 if (rem2
== positions
[1] % 2) {
204 // Fix bishops (on different colors)
205 for (let i
=4; i
<9; i
++) {
206 if (positions
[i
] % 2 != rem2
)
207 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
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]];
218 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
221 pieces
["b"].join("") +
222 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
223 pieces
["w"].join("").toUpperCase() +
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)
238 start: V
.SquareToCoords(pmove
.substr(0, 2)),
239 end: V
.SquareToCoords(pmove
.substr(2))
246 return { x: 9, y: 9 };
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
];
257 return Object
.assign(
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);
286 c: tr
? tr
.c : initColor
,
287 p: tr
? tr
.p : initPiece
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
];
304 // The opponent piece disappears if we take it
305 if (this.board
[ex
][ey
] != V
.EMPTY
) {
310 c: this.getColor(ex
, ey
),
311 p: this.board
[ex
][ey
].charAt(1)
316 // Post-processing: maybe the ball was taken, or a piece + ball,
317 // or maybe a pass (ball <--> piece)
318 if (mv
.vanish
.length
== 2) {
321 mv
.vanish
[1].c
== 'a' ||
322 // Capture a ball-holding piece? If friendly one, then adjust
323 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
325 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
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
;
334 p: V
.HAS_BALL_DECODE
[mv
.vanish
[1].p
],
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
];
345 // Else: standard capture
351 // NOTE: if a pawn captures en-passant, he doesn't hold the ball
352 // So base implementation is fine.
354 getPotentialMovesFrom([x
, y
]) {
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
) {
363 m
.vanish
.length
== 2 &&
364 m
.vanish
[1].p
!= 'a' &&
365 Object
.keys(V
.HAS_BALL_DECODE
).includes(m
.appear
[0].p
)
367 const color
= this.turn
;
368 const oppCol
= V
.GetOppCol(color
);
382 p: V
.HAS_BALL_DECODE
[m
.vanish
[1].p
]
399 end: { x: m
.end
.x
, y: m
.end
.y
}
408 // "Sliders": at most 3 steps
409 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
411 outerLoop: for (let step
of steps
) {
415 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
416 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
417 if (oneStep
|| stepCount
== 3) continue outerLoop
;
422 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
423 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
428 getPotentialPhoenixMoves(sq
) {
429 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
434 move.vanish
.length
== 2 &&
435 move.appear
.length
== 2 &&
436 move.appear
[0].c
!= move.appear
[1].c
447 oppositePasses(m1
, m2
) {
449 m1
.start
.x
== m2
.end
.x
&&
450 m1
.start
.y
== m2
.end
.y
&&
451 m1
.end
.x
== m2
.start
.x
&&
452 m1
.end
.y
== m2
.start
.y
457 const L
= this.pmoves
.length
;
458 const lp
= this.pmoves
[L
-1];
459 if (!lp
) return moves
;
460 return moves
.filter(m
=> {
462 m
.vanish
.length
== 1 ||
463 m
.appear
.length
== 1 ||
464 m
.appear
[0].c
== m
.appear
[1].c
||
465 !this.oppositePasses(lp
, m
)
470 // isAttacked: unused here (no checks)
473 this.pmoves
.push(this.getPmove(move));
486 const color
= V
.GetOppCol(this.turn
);
487 const lastRank
= (color
== "w" ? 0 : 8);
491 Object
.keys(V
.HAS_BALL_DECODE
).includes(
492 this.board
[lastRank
][i
].charAt(1)) &&
493 this.getColor(lastRank
, i
) == color
498 return color
== "w" ? "1-0" : "0-1";
500 if (this.atLeastOneMove()) return "*";
501 // Stalemate (quite unlikely?)
505 static get VALUES() {
517 static get SEARCH_DEPTH() {
523 let evaluation
= super.evalPosition();
524 if (this.board
[4][4] == V
.BALL
)
525 // Ball not captured yet
527 // Ponder depending on ball position
528 for (let i
=0; i
<9; i
++) {
529 for (let j
=0; j
<9; j
++) {
530 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
531 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
534 return 0; //never reached
538 const finalSquare
= V
.CoordsToSquare(move.end
);
539 if (move.appear
.length
== 2)
540 // A pass: special notation
541 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
542 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
543 if (piece
== V
.PAWN
) {
546 if (move.vanish
.length
> move.appear
.length
) {
548 const startColumn
= V
.CoordToColumn(move.start
.y
);
549 notation
= startColumn
+ "x" + finalSquare
;
551 else notation
= finalSquare
;
552 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
555 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
556 notation
+= "=" + promotePiece
.toUpperCase();
562 piece
.toUpperCase() +
563 (move.vanish
.length
> move.appear
.length
? "x" : "") +