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
{
6 static get PawnSpecs() {
10 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.PHOENIX
]) }
14 static get HasFlags() {
18 static get PHOENIX() {
23 // 'b' is already taken:
27 // Special code for "something to fill space" (around goals)
28 // --> If goal is outside the board (current prototype: it's inside)
29 // static get FILL() {
33 static get HAS_BALL_CODE() {
45 static get HAS_BALL_DECODE() {
58 return ChessRules
.PIECES
60 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
65 if (b
== V
.BALL
) return 'a';
66 return ChessRules
.board2fen(b
);
70 if (f
== 'a') return V
.BALL
;
71 return ChessRules
.fen2board(f
);
74 // Check that exactly one ball is on the board
75 // + at least one piece per color.
76 static IsGoodPosition(position
) {
77 if (position
.length
== 0) return false;
78 const rows
= position
.split("/");
79 if (rows
.length
!= V
.size
.x
) return false;
80 let pieces
= { "w": 0, "b": 0 };
81 const withBall
= Object
.keys(V
.HAS_BALL_DECODE
).concat([V
.BALL
]);
83 for (let row
of rows
) {
85 for (let i
= 0; i
< row
.length
; i
++) {
86 const lowerRi
= row
[i
].toLowerCase();
87 if (V
.PIECES
.includes(lowerRi
)) {
88 if (lowerRi
!= V
.BALL
) pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
89 if (withBall
.includes(lowerRi
)) ballCount
++;
92 const num
= parseInt(row
[i
]);
93 if (isNaN(num
)) return false;
97 if (sumElts
!= V
.size
.y
) return false;
99 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
107 Object
.keys(V
.HAS_BALL_DECODE
)
110 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
114 canTake([x1
, y1
], [x2
, y2
]) {
115 if (this.getColor(x1
, y1
) !== this.getColor(x2
, y2
)) {
116 // The piece holding the ball cannot capture:
118 !(Object
.keys(V
.HAS_BALL_DECODE
)
119 .includes(this.board
[x1
][y1
].charAt(1)))
122 // Pass: possible only if one of the friendly pieces has the ball
124 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x1
][y1
].charAt(1)) ||
125 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x2
][y2
].charAt(1))
129 getCheckSquares(color
) {
133 static GenRandInitFen(randomness
) {
135 return "hbnrqrnhb/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/HBNRQRNHB w 0 -";
137 let pieces
= { w: new Array(9), b: new Array(9) };
138 for (let c
of ["w", "b"]) {
139 if (c
== 'b' && randomness
== 1) {
140 pieces
['b'] = pieces
['w'];
144 // Get random squares for every piece, with bishops and phoenixes
145 // on different colors:
146 let positions
= shuffle(ArrayFun
.range(9));
147 const composition
= ['b', 'b', 'h', 'h', 'n', 'n', 'r', 'r', 'q'];
148 let rem2
= positions
[0] % 2;
149 if (rem2
== positions
[1] % 2) {
150 // Fix bishops (on different colors)
151 for (let i
=4; i
<9; i
++) {
152 if (positions
[i
] % 2 != rem2
)
153 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
156 rem2
= positions
[2] % 2;
157 if (rem2
== positions
[3] % 2) {
158 // Fix phoenixes too:
159 for (let i
=4; i
<9; i
++) {
160 if (positions
[i
] % 2 != rem2
)
161 [positions
[3], positions
[i
]] = [positions
[i
], positions
[3]];
164 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
167 pieces
["b"].join("") +
168 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
169 pieces
["w"].join("").toUpperCase() +
170 // En-passant allowed, but no flags
178 return { x: 9, y: 9 };
182 const p
= this.board
[i
][j
].charAt(1);
183 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
184 return V
.HAS_BALL_DECODE
[p
];
189 return Object
.assign(
208 // Because of the ball, getPiece() could be wrong:
209 // use board[x][y][1] instead (always valid).
210 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
211 const initColor
= this.getColor(sx
, sy
);
212 const initPiece
= this.board
[sx
][sy
].charAt(1);
218 c: tr
? tr
.c : initColor
,
219 p: tr
? tr
.p : initPiece
232 // Fix "ball holding" indication in case of promotions:
233 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
234 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
236 // The opponent piece disappears if we take it
237 if (this.board
[ex
][ey
] != V
.EMPTY
) {
242 c: this.getColor(ex
, ey
),
243 p: this.board
[ex
][ey
].charAt(1)
248 // Post-processing: maybe the ball was taken, or a piece + ball,
249 // or maybe a pass (ball <--> piece)
250 if (mv
.vanish
.length
== 2) {
253 mv
.vanish
[1].c
== 'a' ||
254 // Capture a ball-holding piece? If friendly one, then adjust
255 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
257 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
258 if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
259 // "Capturing" self => pass
260 mv
.appear
[0].x
= mv
.start
.x
;
261 mv
.appear
[0].y
= mv
.start
.y
;
266 p: V
.HAS_BALL_DECODE
[mv
.vanish
[1].p
],
271 } else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
272 // Pass the ball: the passing unit does not disappear
273 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
274 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
275 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
277 // Else: standard capture
283 // NOTE: if a pawn is captured en-passant, he doesn't hold the ball
284 // So base implementation is fine.
286 getPotentialMovesFrom([x
, y
]) {
287 if (this.getPiece(x
, y
) == V
.PHOENIX
)
288 return this.getPotentialPhoenixMoves([x
, y
]);
289 return super.getPotentialMovesFrom([x
, y
]);
292 // "Sliders": at most 3 steps
293 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
295 outerLoop: for (let step
of steps
) {
299 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
300 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
301 if (oneStep
|| stepCount
== 3) continue outerLoop
;
306 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
307 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
312 getPotentialPhoenixMoves(sq
) {
313 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
320 // isAttacked: unused here (no checks)
327 const color
= V
.GetOppCol(this.turn
);
328 const lastRank
= (color
== "w" ? 0 : 8);
332 Object
.keys(V
.HAS_BALL_DECODE
).includes(
333 this.board
[lastRank
][i
].charAt(1)) &&
334 this.getColor(lastRank
, i
) == color
339 return color
== "w" ? "1-0" : "0-1";
341 if (this.atLeastOneMove()) return "*";
342 // Stalemate (quite unlikely?)
346 static get VALUES() {
358 static get SEARCH_DEPTH() {
364 let evaluation
= super.evalPosition();
365 if (this.board
[4][4] == V
.BALL
)
366 // Ball not captured yet
368 // Ponder depending on ball position
369 for (let i
=0; i
<9; i
++) {
370 for (let j
=0; j
<9; j
++) {
371 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
372 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
375 return 0; //never reached
379 const finalSquare
= V
.CoordsToSquare(move.end
);
380 if (move.appear
.length
== 2)
381 // A pass: special notation
382 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
383 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
384 if (piece
== V
.PAWN
) {
387 if (move.vanish
.length
> move.appear
.length
) {
389 const startColumn
= V
.CoordToColumn(move.start
.y
);
390 notation
= startColumn
+ "x" + finalSquare
;
392 else notation
= finalSquare
;
393 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
396 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
397 notation
+= "=" + promotePiece
.toUpperCase();
403 piece
.toUpperCase() +
404 (move.vanish
.length
> move.appear
.length
? "x" : "") +