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 // Capture enemy or pass ball to friendly pieces
117 this.getColor(x1
, y1
) !== this.getColor(x2
, y2
) ||
118 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x1
][y1
].charAt(1))
122 getCheckSquares(color
) {
126 static GenRandInitFen(randomness
) {
128 return "rnbcqcnbr/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/RNBCQCNBR w 0 -";
130 let pieces
= { w: new Array(9), b: new Array(9) };
131 for (let c
of ["w", "b"]) {
132 if (c
== 'b' && randomness
== 1) {
133 pieces
['b'] = pieces
['w'];
137 // Get random squares for every piece, totally freely
138 let positions
= shuffle(ArrayFun
.range(9));
139 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'c', 'c', 'q'];
140 const rem2
= positions
[0] % 2;
141 if (rem2
== positions
[1] % 2) {
142 // Fix bishops (on different colors)
143 for (let i
=2; i
<9; i
++) {
144 if (positions
[i
] % 2 != rem2
)
145 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
148 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
151 pieces
["b"].join("") +
152 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
153 pieces
["w"].join("").toUpperCase() +
154 // En-passant allowed, but no flags
162 return { x: 9, y: 9 };
166 const p
= this.board
[i
][j
].charAt(1);
167 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
168 return V
.HAS_BALL_DECODE
[p
];
173 return Object
.assign(
192 // Because of the ball, getPiece() could be wrong:
193 // use board[x][y][1] instead (always valid).
194 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
195 const initColor
= this.getColor(sx
, sy
);
196 const initPiece
= this.board
[sx
][sy
].charAt(1);
202 c: tr
? tr
.c : initColor
,
203 p: tr
? tr
.p : initPiece
216 // Fix "ball holding" indication in case of promotions:
217 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
218 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
220 // The opponent piece disappears if we take it
221 if (this.board
[ex
][ey
] != V
.EMPTY
) {
226 c: this.getColor(ex
, ey
),
227 p: this.board
[ex
][ey
].charAt(1)
232 // Post-processing: maybe the ball was taken, or a piece + ball
233 if (mv
.vanish
.length
== 2) {
236 mv
.vanish
[1].c
== 'a' ||
237 // Capture a ball-holding piece?
238 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
240 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
241 } else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
242 // Pass the ball: the passing unit does not disappear
243 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
244 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
245 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
247 // Else: standard capture
253 // NOTE: if a pawn is captured en-passant, he doesn't hold the ball
254 // So base implementation is fine.
256 getPotentialMovesFrom([x
, y
]) {
257 if (this.getPiece(x
, y
) == V
.PHOENIX
)
258 return this.getPotentialPhoenixMoves([x
, y
]);
259 return super.getPotentialMovesFrom([x
, y
]);
262 // "Sliders": at most 2 steps
263 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
265 outerLoop: for (let step
of steps
) {
269 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
270 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
271 if (oneStep
|| stepCount
== 2) continue outerLoop
;
276 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
277 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
282 getPotentialPhoenixMoves(sq
) {
283 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.PHOENIX
], "oneStep");
290 // isAttacked: unused here (no checks)
297 const color
= V
.GetOppCol(this.turn
);
298 const lastRank
= (color
== "w" ? 0 : 8);
302 Object
.keys(V
.HAS_BALL_DECODE
).includes(
303 this.board
[lastRank
][i
].charAt(1)) &&
304 this.getColor(lastRank
, i
) == color
309 return color
== "w" ? "1-0" : "0-1";
311 if (this.atLeastOneMove()) return "*";
312 // Stalemate (quite unlikely?)
316 static get VALUES() {
328 static get SEARCH_DEPTH() {
334 let evaluation
= super.evalPosition();
335 if (this.board
[4][4] == V
.BALL
)
336 // Ball not captured yet
338 // Ponder depending on ball position
339 for (let i
=0; i
<9; i
++) {
340 for (let j
=0; j
<9; j
++) {
341 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
342 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
345 return 0; //never reached
349 const finalSquare
= V
.CoordsToSquare(move.end
);
350 if (move.appear
.length
== 2)
351 // A pass: special notation
352 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
353 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
354 if (piece
== V
.PAWN
) {
357 if (move.vanish
.length
> move.appear
.length
) {
359 const startColumn
= V
.CoordToColumn(move.start
.y
);
360 notation
= startColumn
+ "x" + finalSquare
;
362 else notation
= finalSquare
;
363 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
366 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
367 notation
+= "=" + promotePiece
.toUpperCase();
373 piece
.toUpperCase() +
374 (move.vanish
.length
> move.appear
.length
? "x" : "") +