1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class BaroqueRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
15 return ChessRules
.PIECES
.concat([V
.IMMOBILIZER
]);
20 //'m' for Immobilizer (I is too similar to 1)
21 return "Baroque/" + b
;
22 return b
; //usual piece
25 // No castling, but checks, so keep track of kings
26 setOtherVariables(fen
) {
27 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
28 const fenParts
= fen
.split(" ");
29 const position
= fenParts
[0].split("/");
30 for (let i
= 0; i
< position
.length
; i
++) {
32 for (let j
= 0; j
< position
[i
].length
; j
++) {
33 switch (position
[i
].charAt(j
)) {
35 this.kingPos
["b"] = [i
, k
];
38 this.kingPos
["w"] = [i
, k
];
41 const num
= parseInt(position
[i
].charAt(j
));
42 if (!isNaN(num
)) k
+= num
- 1;
50 static get IMMOBILIZER() {
53 // Although other pieces keep their names here for coding simplicity,
55 // - a "rook" is a coordinator, capturing by coordinating with the king
56 // - a "knight" is a long-leaper, capturing as in draughts
57 // - a "bishop" is a chameleon, capturing as its prey
58 // - a "queen" is a withdrawer, capturing by moving away from pieces
60 // Is piece on square (x,y) immobilized?
61 isImmobilized([x
, y
]) {
62 const piece
= this.getPiece(x
, y
);
63 const color
= this.getColor(x
, y
);
64 const oppCol
= V
.GetOppCol(color
);
65 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
66 for (let step
of adjacentSteps
) {
67 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
70 this.board
[i
][j
] != V
.EMPTY
&&
71 this.getColor(i
, j
) == oppCol
73 const oppPiece
= this.getPiece(i
, j
);
74 if (oppPiece
== V
.IMMOBILIZER
) {
75 // Moving is possible only if this immobilizer is neutralized
76 for (let step2
of adjacentSteps
) {
77 const [i2
, j2
] = [i
+ step2
[0], j
+ step2
[1]];
78 if (i2
== x
&& j2
== y
) continue; //skip initial piece!
81 this.board
[i2
][j2
] != V
.EMPTY
&&
82 this.getColor(i2
, j2
) == color
84 if ([V
.BISHOP
, V
.IMMOBILIZER
].includes(this.getPiece(i2
, j2
)))
88 return true; //immobilizer isn't neutralized
90 // Chameleons can't be immobilized twice, because there is only one immobilizer
91 if (oppPiece
== V
.BISHOP
&& piece
== V
.IMMOBILIZER
) return true;
97 getPotentialMovesFrom([x
, y
]) {
98 // Pre-check: is thing on this square immobilized?
99 if (this.isImmobilized([x
, y
])) return [];
100 switch (this.getPiece(x
, y
)) {
102 return this.getPotentialImmobilizerMoves([x
, y
]);
104 return super.getPotentialMovesFrom([x
, y
]);
108 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
109 const piece
= this.getPiece(x
, y
);
111 outerLoop: for (let step
of steps
) {
114 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
115 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
116 if (oneStep
!== undefined) continue outerLoop
;
120 // Only king can take on occupied square:
121 if (piece
== V
.KING
&& V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
122 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
127 // Modify capturing moves among listed pawn moves
128 addPawnCaptures(moves
, byChameleon
) {
129 const steps
= V
.steps
[V
.ROOK
];
130 const color
= this.turn
;
131 const oppCol
= V
.GetOppCol(color
);
133 if (!!byChameleon
&& m
.start
.x
!= m
.end
.x
&& m
.start
.y
!= m
.end
.y
) return; //chameleon not moving as pawn
134 // Try capturing in every direction
135 for (let step
of steps
) {
136 const sq2
= [m
.end
.x
+ 2 * step
[0], m
.end
.y
+ 2 * step
[1]];
138 V
.OnBoard(sq2
[0], sq2
[1]) &&
139 this.board
[sq2
[0]][sq2
[1]] != V
.EMPTY
&&
140 this.getColor(sq2
[0], sq2
[1]) == color
143 const sq1
= [m
.end
.x
+ step
[0], m
.end
.y
+ step
[1]];
145 this.board
[sq1
[0]][sq1
[1]] != V
.EMPTY
&&
146 this.getColor(sq1
[0], sq1
[1]) == oppCol
148 const piece1
= this.getPiece(sq1
[0], sq1
[1]);
149 if (!byChameleon
|| piece1
== V
.PAWN
) {
166 getPotentialPawnMoves([x
, y
]) {
167 let moves
= super.getPotentialRookMoves([x
, y
]);
168 this.addPawnCaptures(moves
);
172 addRookCaptures(moves
, byChameleon
) {
173 const color
= this.turn
;
174 const oppCol
= V
.GetOppCol(color
);
175 const kp
= this.kingPos
[color
];
177 // Check piece-king rectangle (if any) corners for enemy pieces
178 if (m
.end
.x
== kp
[0] || m
.end
.y
== kp
[1]) return; //"flat rectangle"
179 const corner1
= [m
.end
.x
, kp
[1]];
180 const corner2
= [kp
[0], m
.end
.y
];
181 for (let [i
, j
] of [corner1
, corner2
]) {
182 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == oppCol
) {
183 const piece
= this.getPiece(i
, j
);
184 if (!byChameleon
|| piece
== V
.ROOK
) {
200 getPotentialRookMoves(sq
) {
201 let moves
= super.getPotentialQueenMoves(sq
);
202 this.addRookCaptures(moves
);
206 getKnightCaptures(startSquare
, byChameleon
) {
207 // Look in every direction for captures
208 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
209 const color
= this.turn
;
210 const oppCol
= V
.GetOppCol(color
);
212 const [x
, y
] = [startSquare
[0], startSquare
[1]];
213 const piece
= this.getPiece(x
, y
); //might be a chameleon!
214 outerLoop: for (let step
of steps
) {
215 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
216 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
222 this.getColor(i
, j
) == color
||
223 (!!byChameleon
&& this.getPiece(i
, j
) != V
.KNIGHT
)
227 // last(thing), cur(thing) : stop if "cur" is our color, or beyond board limits,
228 // or if "last" isn't empty and cur neither. Otherwise, if cur is empty then
229 // add move until cur square; if cur is occupied then stop if !!byChameleon and
230 // the square not occupied by a leaper.
232 let cur
= [i
+ step
[0], j
+ step
[1]];
233 let vanished
= [new PiPo({ x: x
, y: y
, c: color
, p: piece
})];
234 while (V
.OnBoard(cur
[0], cur
[1])) {
235 if (this.board
[last
[0]][last
[1]] != V
.EMPTY
) {
236 const oppPiece
= this.getPiece(last
[0], last
[1]);
237 if (!!byChameleon
&& oppPiece
!= V
.KNIGHT
) continue outerLoop
;
240 new PiPo({ x: last
[0], y: last
[1], c: oppCol
, p: oppPiece
})
243 if (this.board
[cur
[0]][cur
[1]] != V
.EMPTY
) {
245 this.getColor(cur
[0], cur
[1]) == color
||
246 this.board
[last
[0]][last
[1]] != V
.EMPTY
248 //TODO: redundant test
254 appear: [new PiPo({ x: cur
[0], y: cur
[1], c: color
, p: piece
})],
255 vanish: JSON
.parse(JSON
.stringify(vanished
)), //TODO: required?
256 start: { x: x
, y: y
},
257 end: { x: cur
[0], y: cur
[1] }
261 last
= [last
[0] + step
[0], last
[1] + step
[1]];
262 cur
= [cur
[0] + step
[0], cur
[1] + step
[1]];
269 getPotentialKnightMoves(sq
) {
270 return super.getPotentialQueenMoves(sq
).concat(this.getKnightCaptures(sq
));
274 getPotentialBishopMoves([x
, y
]) {
276 .getPotentialQueenMoves([x
, y
])
277 .concat(this.getKnightCaptures([x
, y
], "asChameleon"));
278 // No "king capture" because king cannot remain under check
279 this.addPawnCaptures(moves
, "asChameleon");
280 this.addRookCaptures(moves
, "asChameleon");
281 this.addQueenCaptures(moves
, "asChameleon");
282 // Post-processing: merge similar moves, concatenating vanish arrays
283 let mergedMoves
= {};
285 const key
= m
.end
.x
+ V
.size
.x
* m
.end
.y
;
286 if (!mergedMoves
[key
]) mergedMoves
[key
] = m
;
288 for (let i
= 1; i
< m
.vanish
.length
; i
++)
289 mergedMoves
[key
].vanish
.push(m
.vanish
[i
]);
292 // Finally return an array
294 Object
.keys(mergedMoves
).forEach(k
=> {
295 moves
.push(mergedMoves
[k
]);
300 addQueenCaptures(moves
, byChameleon
) {
301 if (moves
.length
== 0) return;
302 const [x
, y
] = [moves
[0].start
.x
, moves
[0].start
.y
];
303 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
304 let capturingDirections
= [];
305 const color
= this.turn
;
306 const oppCol
= V
.GetOppCol(color
);
307 adjacentSteps
.forEach(step
=> {
308 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
311 this.board
[i
][j
] != V
.EMPTY
&&
312 this.getColor(i
, j
) == oppCol
&&
313 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
315 capturingDirections
.push(step
);
320 m
.end
.x
!= x
? (m
.end
.x
- x
) / Math
.abs(m
.end
.x
- x
) : 0,
321 m
.end
.y
!= y
? (m
.end
.y
- y
) / Math
.abs(m
.end
.y
- y
) : 0
323 // NOTE: includes() and even _.isEqual() functions fail...
324 // TODO: this test should be done only once per direction
326 capturingDirections
.some(dir
=> {
327 return dir
[0] == -step
[0] && dir
[1] == -step
[1];
330 const [i
, j
] = [x
- step
[0], y
- step
[1]];
335 p: this.getPiece(i
, j
),
344 getPotentialQueenMoves(sq
) {
345 let moves
= super.getPotentialQueenMoves(sq
);
346 this.addQueenCaptures(moves
);
350 getPotentialImmobilizerMoves(sq
) {
351 // Immobilizer doesn't capture
352 return super.getPotentialQueenMoves(sq
);
355 // isAttacked() is OK because the immobilizer doesn't take
357 isAttackedByPawn([x
, y
], color
) {
358 // Square (x,y) must be surroundable by two enemy pieces,
359 // and one of them at least should be a pawn (moving).
364 const steps
= V
.steps
[V
.ROOK
];
365 for (let dir
of dirs
) {
366 const [i1
, j1
] = [x
- dir
[0], y
- dir
[1]]; //"before"
367 const [i2
, j2
] = [x
+ dir
[0], y
+ dir
[1]]; //"after"
368 if (V
.OnBoard(i1
, j1
) && V
.OnBoard(i2
, j2
)) {
371 this.board
[i1
][j1
] != V
.EMPTY
&&
372 this.getColor(i1
, j1
) == color
&&
373 this.board
[i2
][j2
] == V
.EMPTY
377 this.board
[i2
][j2
] != V
.EMPTY
&&
378 this.getColor(i2
, j2
) == color
&&
379 this.board
[i1
][j1
] == V
.EMPTY
382 // Search a movable enemy pawn landing on the empty square
383 for (let step
of steps
) {
384 let [ii
, jj
] = this.board
[i1
][j1
] == V
.EMPTY
? [i1
, j1
] : [i2
, j2
];
385 let [i3
, j3
] = [ii
+ step
[0], jj
+ step
[1]];
386 while (V
.OnBoard(i3
, j3
) && this.board
[i3
][j3
] == V
.EMPTY
) {
392 this.getColor(i3
, j3
) == color
&&
393 this.getPiece(i3
, j3
) == V
.PAWN
&&
394 !this.isImmobilized([i3
, j3
])
405 isAttackedByRook([x
, y
], color
) {
406 // King must be on same column or row,
407 // and a rook should be able to reach a capturing square
408 const sameRow
= x
== this.kingPos
[color
][0];
409 const sameColumn
= y
== this.kingPos
[color
][1];
410 if (sameRow
|| sameColumn
) {
411 // Look for the enemy rook (maximum 1)
412 for (let i
= 0; i
< V
.size
.x
; i
++) {
413 for (let j
= 0; j
< V
.size
.y
; j
++) {
415 this.board
[i
][j
] != V
.EMPTY
&&
416 this.getColor(i
, j
) == color
&&
417 this.getPiece(i
, j
) == V
.ROOK
419 if (this.isImmobilized([i
, j
])) return false; //because only one rook
420 // Can it reach a capturing square?
421 // Easy but quite suboptimal way (TODO): generate all moves (turn is OK)
422 const moves
= this.getPotentialMovesFrom([i
, j
]);
423 for (let move of moves
) {
425 (sameRow
&& move.end
.y
== y
) ||
426 (sameColumn
&& move.end
.x
== x
)
437 isAttackedByKnight([x
, y
], color
) {
438 // Square (x,y) must be on same line as a knight,
439 // and there must be empty square(s) behind.
440 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
441 outerLoop: for (let step
of steps
) {
442 const [i0
, j0
] = [x
+ step
[0], y
+ step
[1]];
443 if (V
.OnBoard(i0
, j0
) && this.board
[i0
][j0
] == V
.EMPTY
) {
444 // Try in opposite direction:
445 let [i
, j
] = [x
- step
[0], y
- step
[1]];
446 while (V
.OnBoard(i
, j
)) {
447 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
451 if (V
.OnBoard(i
, j
)) {
452 if (this.getColor(i
, j
) == color
) {
454 this.getPiece(i
, j
) == V
.KNIGHT
&&
455 !this.isImmobilized([i
, j
])
460 // [else] Our color, could be captured *if there was an empty space*
461 if (this.board
[i
+ step
[0]][j
+ step
[1]] != V
.EMPTY
)
472 isAttackedByBishop([x
, y
], color
) {
473 // We cheat a little here: since this function is used exclusively for
474 // the king, it's enough to check the immediate surrounding of the square.
475 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
476 for (let step
of adjacentSteps
) {
477 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
480 this.board
[i
][j
] != V
.EMPTY
&&
481 this.getColor(i
, j
) == color
&&
482 this.getPiece(i
, j
) == V
.BISHOP
484 return true; //bishops are never immobilized
490 isAttackedByQueen([x
, y
], color
) {
491 // Square (x,y) must be adjacent to a queen, and the queen must have
492 // some free space in the opposite direction from (x,y)
493 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
494 for (let step
of adjacentSteps
) {
495 const sq2
= [x
+ 2 * step
[0], y
+ 2 * step
[1]];
496 if (V
.OnBoard(sq2
[0], sq2
[1]) && this.board
[sq2
[0]][sq2
[1]] == V
.EMPTY
) {
497 const sq1
= [x
+ step
[0], y
+ step
[1]];
499 this.board
[sq1
[0]][sq1
[1]] != V
.EMPTY
&&
500 this.getColor(sq1
[0], sq1
[1]) == color
&&
501 this.getPiece(sq1
[0], sq1
[1]) == V
.QUEEN
&&
502 !this.isImmobilized(sq1
)
511 isAttackedByKing([x
, y
], color
) {
512 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
513 for (let step
of steps
) {
514 let rx
= x
+ step
[0],
518 this.getPiece(rx
, ry
) === V
.KING
&&
519 this.getColor(rx
, ry
) == color
&&
520 !this.isImmobilized([rx
, ry
])
528 static get VALUES() {
540 static get SEARCH_DEPTH() {
544 static GenRandInitFen(randomness
) {
547 return "rnbqkbnrm/pppppppp/8/8/8/8/PPPPPPPP/MNBKQBNR w 0";
549 let pieces
= { w: new Array(8), b: new Array(8) };
550 // Shuffle pieces on first and last rank
551 for (let c
of ["w", "b"]) {
552 if (c
== 'b' && randomness
== 1) {
553 pieces
['b'] = pieces
['w'];
557 // Get random squares for every piece, totally freely
558 let positions
= shuffle(ArrayFun
.range(8));
559 const composition
= ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'm'];
560 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
563 pieces
["b"].join("") +
564 "/pppppppp/8/8/8/8/PPPPPPPP/" +
565 pieces
["w"].join("").toUpperCase() +
571 const initialSquare
= V
.CoordsToSquare(move.start
);
572 const finalSquare
= V
.CoordsToSquare(move.end
);
573 let notation
= undefined;
574 if (move.appear
[0].p
== V
.PAWN
) {
575 // Pawn: generally ambiguous short notation, so we use full description
576 notation
= "P" + initialSquare
+ finalSquare
;
577 } else if (move.appear
[0].p
== V
.KING
)
578 notation
= "K" + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
;
579 else notation
= move.appear
[0].p
.toUpperCase() + finalSquare
;
580 // Add a capture mark (not describing what is captured...):
581 if (move.vanish
.length
> 1 && move.appear
[0].p
!= V
.KING
) notation
+= "X";