1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { WildebeestRules
} from "@/variants/Wildebeest";
3 import { ArrayFun
} from "@/utils/array";
4 import { shuffle
} from "@/utils/alea";
6 export class BallRules
extends ChessRules
{
7 static get PawnSpecs() {
11 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.WILDEBEEST
]) }
15 static get HasFlags() {
18 static get HasCastle() {
22 static get WILDEBEEST() {
27 // 'b' is already taken:
31 // Special code for "something to fill space" (around goals)
32 // --> If goal is outside the board (current prototype: it's inside)
33 // static get FILL() {
37 static get HAS_BALL_CODE() {
49 static get HAS_BALL_DECODE() {
62 return ChessRules
.PIECES
63 .concat([V
.WILDEBEEST
])
64 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
69 if (b
== V
.BALL
) return 'a';
70 return ChessRules
.board2fen(b
);
74 if (f
== 'a') return V
.BALL
;
75 return ChessRules
.fen2board(f
);
78 // Check that exactly one ball is on the board
79 // + at least one piece per color.
80 static IsGoodPosition(position
) {
81 if (position
.length
== 0) return false;
82 const rows
= position
.split("/");
83 if (rows
.length
!= V
.size
.x
) return false;
84 let pieces
= { "w": 0, "b": 0 };
85 const withBall
= Object
.keys(V
.HAS_BALL_DECODE
).concat([V
.BALL
]);
87 for (let row
of rows
) {
89 for (let i
= 0; i
< row
.length
; i
++) {
90 const lowerRi
= row
[i
].toLowerCase();
91 if (V
.PIECES
.includes(lowerRi
)) {
92 if (lowerRi
!= V
.BALL
) pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
93 if (withBall
.includes(lowerRi
)) ballCount
++;
96 const num
= parseInt(row
[i
]);
97 if (isNaN(num
)) return false;
101 if (sumElts
!= V
.size
.y
) return false;
103 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
111 Object
.keys(V
.HAS_BALL_DECODE
)
112 .concat([V
.WILDEBEEST
])
114 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
118 canTake([x1
, y1
], [x2
, y2
]) {
119 // Capture enemy or pass ball to friendly pieces
121 this.getColor(x1
, y1
) !== this.getColor(x2
, y2
) ||
122 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x1
][y1
].charAt(1))
126 getCheckSquares(color
) {
130 static GenRandInitFen(randomness
) {
132 return "rnbqkwbnr/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/RNBQKWBNR w 0 -";
134 let pieces
= { w: new Array(9), b: new Array(9) };
135 for (let c
of ["w", "b"]) {
136 if (c
== 'b' && randomness
== 1) {
137 pieces
['b'] = pieces
['w'];
141 // Get random squares for every piece, totally freely
142 let positions
= shuffle(ArrayFun
.range(9));
143 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q', 'w'];
144 const rem2
= positions
[0] % 2;
145 if (rem2
== positions
[1] % 2) {
146 // Fix bishops (on different colors)
147 for (let i
=2; i
<9; i
++) {
148 if (positions
[i
] % 2 != rem2
)
149 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
152 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
155 pieces
["b"].join("") +
156 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
157 pieces
["w"].join("").toUpperCase() +
158 // En-passant allowed, but no flags
166 return { x: 9, y: 9 };
170 const p
= this.board
[i
][j
].charAt(1);
171 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
172 return V
.HAS_BALL_DECODE
[p
];
177 return WildebeestRules
.steps
;
180 // Because of the ball, getPiece() could be wrong:
181 // use board[x][y][1] instead (always valid).
182 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
183 const initColor
= this.getColor(sx
, sy
);
184 const initPiece
= this.board
[sx
][sy
].charAt(1);
190 c: tr
? tr
.c : initColor
,
191 p: tr
? tr
.p : initPiece
204 // Fix "ball holding" indication in case of promotions:
205 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
206 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
208 // The opponent piece disappears if we take it
209 if (this.board
[ex
][ey
] != V
.EMPTY
) {
214 c: this.getColor(ex
, ey
),
215 p: this.board
[ex
][ey
].charAt(1)
220 // Post-processing: maybe the ball was taken, or a piece + ball
221 if (mv
.vanish
.length
== 2) {
224 mv
.vanish
[1].c
== 'a' ||
225 // Capture a ball-holding piece?
226 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
228 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
229 } else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
230 // Pass the ball: the passing unit does not disappear
231 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
232 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
233 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
235 // Else: standard capture
241 // NOTE: if a pawn is captured en-passant, he doesn't hold the ball
242 // So base implementation is fine.
244 getPotentialMovesFrom([x
, y
]) {
245 if (this.getPiece(x
, y
) == V
.WILDEBEEST
)
246 return this.getPotentialWildebeestMoves([x
, y
]);
247 return super.getPotentialMovesFrom([x
, y
]);
250 getPotentialWildebeestMoves(sq
) {
251 return this.getSlideNJumpMoves(
253 V
.steps
[V
.KNIGHT
].concat(V
.steps
[WildebeestRules
.CAMEL
]),
262 // isAttacked: unused here (no checks)
269 const color
= V
.GetOppCol(this.turn
);
270 const lastRank
= (color
== "w" ? 0 : 8);
274 Object
.keys(V
.HAS_BALL_DECODE
).includes(
275 this.board
[lastRank
][i
].charAt(1)) &&
276 this.getColor(lastRank
, i
) == color
281 return color
== "w" ? "1-0" : "0-1";
283 if (this.atLeastOneMove()) return "*";
284 // Stalemate (quite unlikely?)
288 static get VALUES() {
301 static get SEARCH_DEPTH() {
307 let evaluation
= super.evalPosition();
308 if (this.board
[4][4] == V
.BALL
)
309 // Ball not captured yet
311 // Ponder depending on ball position
312 for (let i
=0; i
<9; i
++) {
313 for (let j
=0; j
<9; j
++) {
314 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
315 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
318 return 0; //never reached
322 const finalSquare
= V
.CoordsToSquare(move.end
);
323 if (move.appear
.length
== 2)
324 // A pass: special notation
325 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
326 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
327 if (piece
== V
.PAWN
) {
330 if (move.vanish
.length
> move.appear
.length
) {
332 const startColumn
= V
.CoordToColumn(move.start
.y
);
333 notation
= startColumn
+ "x" + finalSquare
;
335 else notation
= finalSquare
;
336 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
339 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
340 notation
+= "=" + promotePiece
.toUpperCase();
346 piece
.toUpperCase() +
347 (move.vanish
.length
> move.appear
.length
? "x" : "") +