1bb7731c8cbdeda13a23b801f19c4b426b9c2608
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
.CHAMPION
]) }
14 static get HasFlags() {
17 static get HasCastle() {
21 static get CHAMPION() {
26 // 'b' is already taken:
30 // Special code for "something to fill space" (around goals)
31 // --> If goal is outside the board (current prototype: it's inside)
32 // static get FILL() {
36 static get HAS_BALL_CODE() {
48 static get HAS_BALL_DECODE() {
61 return ChessRules
.PIECES
63 .concat(Object
.keys(V
.HAS_BALL_DECODE
))
68 if (b
== V
.BALL
) return 'a';
69 return ChessRules
.board2fen(b
);
73 if (f
== 'a') return V
.BALL
;
74 return ChessRules
.fen2board(f
);
77 // Check that exactly one ball is on the board
78 // + at least one piece per color.
79 static IsGoodPosition(position
) {
80 if (position
.length
== 0) return false;
81 const rows
= position
.split("/");
82 if (rows
.length
!= V
.size
.x
) return false;
83 let pieces
= { "w": 0, "b": 0 };
84 const withBall
= Object
.keys(V
.HAS_BALL_DECODE
).concat([V
.BALL
]);
86 for (let row
of rows
) {
88 for (let i
= 0; i
< row
.length
; i
++) {
89 const lowerRi
= row
[i
].toLowerCase();
90 if (V
.PIECES
.includes(lowerRi
)) {
91 if (lowerRi
!= V
.BALL
) pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
92 if (withBall
.includes(lowerRi
)) ballCount
++;
95 const num
= parseInt(row
[i
]);
96 if (isNaN(num
)) return false;
100 if (sumElts
!= V
.size
.y
) return false;
102 if (ballCount
!= 1 || Object
.values(pieces
).some(v
=> v
== 0))
110 Object
.keys(V
.HAS_BALL_DECODE
)
111 .concat([V
.CHAMPION
])
113 if (withPrefix
.includes(b
[1])) prefix
= "Ball/";
117 canTake([x1
, y1
], [x2
, y2
]) {
118 // Capture enemy or pass ball to friendly pieces
120 this.getColor(x1
, y1
) !== this.getColor(x2
, y2
) ||
121 Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[x1
][y1
].charAt(1))
125 getCheckSquares(color
) {
129 static GenRandInitFen(randomness
) {
131 return "rnbhqhnbr/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/RNBHQHNBR w 0 -";
133 let pieces
= { w: new Array(9), b: new Array(9) };
134 for (let c
of ["w", "b"]) {
135 if (c
== 'b' && randomness
== 1) {
136 pieces
['b'] = pieces
['w'];
140 // Get random squares for every piece, totally freely
141 let positions
= shuffle(ArrayFun
.range(9));
142 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'h', 'h', 'q'];
143 const rem2
= positions
[0] % 2;
144 if (rem2
== positions
[1] % 2) {
145 // Fix bishops (on different colors)
146 for (let i
=2; i
<9; i
++) {
147 if (positions
[i
] % 2 != rem2
)
148 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
151 for (let i
= 0; i
< 9; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
154 pieces
["b"].join("") +
155 "/ppppppppp/9/9/4a4/9/9/PPPPPPPPP/" +
156 pieces
["w"].join("").toUpperCase() +
157 // En-passant allowed, but no flags
165 return { x: 9, y: 9 };
169 const p
= this.board
[i
][j
].charAt(1);
170 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(p
))
171 return V
.HAS_BALL_DECODE
[p
];
176 return Object
.assign(
179 // Add champion moves
199 // Because of the ball, getPiece() could be wrong:
200 // use board[x][y][1] instead (always valid).
201 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
202 const initColor
= this.getColor(sx
, sy
);
203 const initPiece
= this.board
[sx
][sy
].charAt(1);
209 c: tr
? tr
.c : initColor
,
210 p: tr
? tr
.p : initPiece
223 // Fix "ball holding" indication in case of promotions:
224 if (!!tr
&& Object
.keys(V
.HAS_BALL_DECODE
).includes(initPiece
))
225 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[tr
.p
];
227 // The opponent piece disappears if we take it
228 if (this.board
[ex
][ey
] != V
.EMPTY
) {
233 c: this.getColor(ex
, ey
),
234 p: this.board
[ex
][ey
].charAt(1)
239 // Post-processing: maybe the ball was taken, or a piece + ball
240 if (mv
.vanish
.length
== 2) {
243 mv
.vanish
[1].c
== 'a' ||
244 // Capture a ball-holding piece?
245 Object
.keys(V
.HAS_BALL_DECODE
).includes(mv
.vanish
[1].p
)
247 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.appear
[0].p
];
248 } else if (mv
.vanish
[1].c
== mv
.vanish
[0].c
) {
249 // Pass the ball: the passing unit does not disappear
250 mv
.appear
.push(JSON
.parse(JSON
.stringify(mv
.vanish
[0])));
251 mv
.appear
[0].p
= V
.HAS_BALL_CODE
[mv
.vanish
[1].p
];
252 mv
.appear
[1].p
= V
.HAS_BALL_DECODE
[mv
.appear
[1].p
];
254 // Else: standard capture
260 // NOTE: if a pawn is captured en-passant, he doesn't hold the ball
261 // So base implementation is fine.
263 getPotentialMovesFrom([x
, y
]) {
264 if (this.getPiece(x
, y
) == V
.CHAMPION
)
265 return this.getPotentialChampionMoves([x
, y
]);
266 return super.getPotentialMovesFrom([x
, y
]);
269 // "Sliders": at most 2 steps
270 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
272 outerLoop: for (let step
of steps
) {
276 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
277 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
278 if (oneStep
|| stepCount
== 2) continue outerLoop
;
283 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
284 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
289 getPotentialChampionMoves(sq
) {
290 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.CHAMPION
], "oneStep");
297 // isAttacked: unused here (no checks)
304 const color
= V
.GetOppCol(this.turn
);
305 const lastRank
= (color
== "w" ? 0 : 8);
309 Object
.keys(V
.HAS_BALL_DECODE
).includes(
310 this.board
[lastRank
][i
].charAt(1)) &&
311 this.getColor(lastRank
, i
) == color
316 return color
== "w" ? "1-0" : "0-1";
318 if (this.atLeastOneMove()) return "*";
319 // Stalemate (quite unlikely?)
323 static get VALUES() {
335 static get SEARCH_DEPTH() {
341 let evaluation
= super.evalPosition();
342 if (this.board
[4][4] == V
.BALL
)
343 // Ball not captured yet
345 // Ponder depending on ball position
346 for (let i
=0; i
<9; i
++) {
347 for (let j
=0; j
<9; j
++) {
348 if (Object
.keys(V
.HAS_BALL_DECODE
).includes(this.board
[i
][j
][1]))
349 return evaluation
/2 + (this.getColor(i
, j
) == "w" ? 8 - i : -i
);
352 return 0; //never reached
356 const finalSquare
= V
.CoordsToSquare(move.end
);
357 if (move.appear
.length
== 2)
358 // A pass: special notation
359 return V
.CoordsToSquare(move.start
) + "P" + finalSquare
;
360 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
361 if (piece
== V
.PAWN
) {
364 if (move.vanish
.length
> move.appear
.length
) {
366 const startColumn
= V
.CoordToColumn(move.start
.y
);
367 notation
= startColumn
+ "x" + finalSquare
;
369 else notation
= finalSquare
;
370 if (![V
.PAWN
, V
.HAS_BALL_CODE
[V
.PAWN
]].includes(move.appear
[0].p
)) {
373 V
.HAS_BALL_DECODE
[move.appear
[0].p
] || move.appear
[0].p
;
374 notation
+= "=" + promotePiece
.toUpperCase();
380 piece
.toUpperCase() +
381 (move.vanish
.length
> move.appear
.length
? "x" : "") +