1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
3 import Move
from "/utils/Move.js";
5 export default class AllmateRules
extends ChessRules
{
9 select: C
.Options
.select
,
25 setOtherVariables(fenParsed
) {
26 super.setOtherVariables(fenParsed
);
30 getPotentialMovesFrom(sq
) {
31 // Remove direct captures:
32 return super.getPotentialMovesFrom(sq
)
33 .filter(m
=> m
.vanish
.length
== m
.appear
.length
);
36 // Called "recursively" before a move is played, until no effect
37 computeNextMove(move) {
38 if (move.appear
.length
> 0)
40 const color
= this.turn
;
41 const oppCol
= C
.GetOppCol(this.turn
);
43 start: this.curMove
.end
,
44 end: this.curMove
.end
,
48 this.playOnBoard(move);
49 for (let i
=0; i
<this.size
.x
; i
++) {
50 for (let j
=0; j
<this.size
.y
; j
++) {
51 if (this.getColor(i
, j
) == oppCol
&& this.isMated(i
, j
, color
)) {
53 new PiPo({x: i
, y: j
, c: oppCol
, p: this.getPiece(i
, j
)})
58 this.undoOnBoard(move);
59 move.next
= (mv
.vanish
.length
> 0 ? mv : null);
62 // is piece on square x,y mated by color?
63 isMated(x
, y
, color
) {
64 const myColor
= C
.GetOppCol(color
);
65 if (!this.underCheck([x
, y
], color
))
67 for (let i
=0; i
<this.size
.x
; i
++) {
68 for (let j
=0; j
<this.size
.y
; j
++) {
69 if (this.getColor(i
, j
) == myColor
) {
70 const movesIJ
= super.getPotentialMovesFrom([i
, j
], myColor
);
71 for (let move of movesIJ
) {
72 this.playOnBoard(move);
73 let testSquare
= [x
, y
];
74 if (i
== x
&& j
== y
) {
75 // The mated-candidate has moved itself
76 testSquare
= [move.end
.x
, move.end
.y
]; }
77 const res
= this.underCheck(testSquare
, color
);
78 this.undoOnBoard(move);
88 // No "under check" conditions in castling
90 return super.getCastleMoves(sq
, null, "castleInCheck");
94 return moves
; //TODO?: over-simplification to be fixed later