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 // 'b' is already taken:
42 // Special code for "something to fill space" (around goals)
43 // --> If goal is outside the board (current prototype: it's inside)
44 // static get FILL() {
48 static get HAS_BALL_CODE() {
60 static get HAS_BALL_DECODE() {
73 return ChessRules
.PIECES
75 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
80 if (b
== V
.BALL
) return 'a';
81 return ChessRules
.board2fen(b
);
85 if (f
== 'a') return V
.BALL
;
86 return ChessRules
.fen2board(f
);
89 // Check that exactly one ball is on the board
90 // + at least one piece per color.
91 static IsGoodPosition(position
) {
92 if (position
.length
== 0) return false;
93 const rows
= position
.split("/");
94 if (rows
.length
!= V
.size
.x
) return false;
95 let pieces
= { "w": 0, "b": 0 };
96 const withBall
= Object
.keys(V
.HAS_BALL_DECODE
).concat([V
.BALL
]);
98 for (let row
of rows
) {
100 for (let i
= 0; i
< row
.length
; i
++) {
101 const lowerRi
= row
[i
].toLowerCase();
102 if (V
.PIECES
.includes(lowerRi
)) {
103 if (lowerRi
!= V
.BALL
) pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
104 if (withBall
.includes(lowerRi
)) ballCount
++;
107 const num
= parseInt(row
[i
]);
108 if (isNaN(num
)) return false;
112 if (sumElts
!= V
.size
.y
) return false;
114 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
122 Object
.keys(V
.HAS_BALL_DECODE
)
125 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
129 canTake([x1
, y1
], [x2
, y2
]) {
130 if (this.getColor(x1
, y1
) !== this.getColor(x2
, y2
)) {
131 // The piece holding the ball cannot capture:
133 !(Object
.keys(V
.HAS_BALL_DECODE
)
134 .includes(this.board
[x1
][y1
].charAt(1)))
137 // Pass: possible only if one of the friendly pieces has the ball
139 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x1
][y1
].charAt(1)) ||
140 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x2
][y2
].charAt(1))
148 static GenRandInitFen(randomness
) {
150 return "hbnrqrnhb/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/HBNRQRNHB w 0 -";
152 let pieces
= { w: new Array(9), b: new Array(9) };
153 for (let c
of ["w", "b"]) {
154 if (c
== 'b' && randomness
== 1) {
155 pieces
['b'] = pieces
['w'];
159 // Get random squares for every piece, with bishops and phoenixes
160 // on different colors:
161 let positions
= shuffle(ArrayFun
.range(9));
162 const composition
= ['b', 'b', 'h', 'h', 'n', 'n', 'r', 'r', 'q'];
163 let rem2
= positions
[0] % 2;
164 if (rem2
== positions
[1] % 2) {
165 // Fix bishops (on different colors)
166 for (let i
=4; i
<9; i
++) {
167 if (positions
[i
] % 2 != rem2
)
168 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
171 rem2
= positions
[2] % 2;
172 if (rem2
== positions
[3] % 2) {
173 // Fix phoenixes too:
174 for (let i
=4; i
<9; i
++) {
175 if (positions
[i
] % 2 != rem2
)
176 [positions
[3], positions
[i
]] = [positions
[i
], positions
[3]];
179 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
182 pieces
["b"].join("") +
183 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
184 pieces
["w"].join("").toUpperCase() +
185 // En-passant allowed, but no flags
193 return { x: 9, y: 9 };
197 const p
= this.board
[i
][j
].charAt(1);
198 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
199 return V
.HAS_BALL_DECODE
[p
];
204 return Object
.assign(
223 // Because of the ball, getPiece() could be wrong:
224 // use board[x][y][1] instead (always valid).
225 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
226 const initColor
= this.getColor(sx
, sy
);
227 const initPiece
= this.board
[sx
][sy
].charAt(1);
233 c: tr
? tr
.c : initColor
,
234 p: tr
? tr
.p : initPiece
247 // Fix "ball holding" indication in case of promotions:
248 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
249 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
251 // The opponent piece disappears if we take it
252 if (this.board
[ex
][ey
] != V
.EMPTY
) {
257 c: this.getColor(ex
, ey
),
258 p: this.board
[ex
][ey
].charAt(1)
263 // Post-processing: maybe the ball was taken, or a piece + ball,
264 // or maybe a pass (ball <--> piece)
265 if (mv
.vanish
.length
== 2) {
268 mv
.vanish
[1].c
== 'a' ||
269 // Capture a ball-holding piece? If friendly one, then adjust
270 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
272 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
273 if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
274 // "Capturing" self => pass
275 mv
.appear
[0].x
= mv
.start
.x
;
276 mv
.appear
[0].y
= mv
.start
.y
;
281 p: V
.HAS_BALL_DECODE
[mv
.vanish
[1].p
],
286 } else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
287 // Pass the ball: the passing unit does not disappear
288 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
289 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
290 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
292 // Else: standard capture
298 // NOTE: if a pawn is captured en-passant, he doesn't hold the ball
299 // So base implementation is fine.
301 getPotentialMovesFrom([x
, y
]) {
302 if (this.getPiece(x
, y
) == V
.PHOENIX
)
303 return this.getPotentialPhoenixMoves([x
, y
]);
304 return super.getPotentialMovesFrom([x
, y
]);
307 // "Sliders": at most 3 steps
308 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
310 outerLoop: for (let step
of steps
) {
314 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
315 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
316 if (oneStep
|| stepCount
== 3) continue outerLoop
;
321 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
322 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
327 getPotentialPhoenixMoves(sq
) {
328 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
335 // isAttacked: unused here (no checks)
342 const color
= V
.GetOppCol(this.turn
);
343 const lastRank
= (color
== "w" ? 0 : 8);
347 Object
.keys(V
.HAS_BALL_DECODE
).includes(
348 this.board
[lastRank
][i
].charAt(1)) &&
349 this.getColor(lastRank
, i
) == color
354 return color
== "w" ? "1-0" : "0-1";
356 if (this.atLeastOneMove()) return "*";
357 // Stalemate (quite unlikely?)
361 static get VALUES() {
373 static get SEARCH_DEPTH() {
379 let evaluation
= super.evalPosition();
380 if (this.board
[4][4] == V
.BALL
)
381 // Ball not captured yet
383 // Ponder depending on ball position
384 for (let i
=0; i
<9; i
++) {
385 for (let j
=0; j
<9; j
++) {
386 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
387 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
390 return 0; //never reached
394 const finalSquare
= V
.CoordsToSquare(move.end
);
395 if (move.appear
.length
== 2)
396 // A pass: special notation
397 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
398 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
399 if (piece
== V
.PAWN
) {
402 if (move.vanish
.length
> move.appear
.length
) {
404 const startColumn
= V
.CoordToColumn(move.start
.y
);
405 notation
= startColumn
+ "x" + finalSquare
;
407 else notation
= finalSquare
;
408 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
411 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
412 notation
+= "=" + promotePiece
.toUpperCase();
418 piece
.toUpperCase() +
419 (move.vanish
.length
> move.appear
.length
? "x" : "") +