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
,
22 setOtherVariables(fenParsed
) {
23 super.setOtherVariables(fenParsed
);
27 getPotentialMovesFrom(sq
) {
28 // Remove direct captures:
29 return super.getPotentialMovesFrom(sq
)
30 .filter(m
=> m
.vanish
.length
== m
.appear
.length
);
33 // Called "recursively" before a move is played, until no effect
34 computeNextMove(move) {
35 if (move.appear
.length
> 0)
37 const color
= this.turn
;
38 const oppCol
= C
.GetOppTurn(this.turn
);
40 start: this.curMove
.end
,
41 end: this.curMove
.end
,
45 this.playOnBoard(move);
46 for (let i
=0; i
<this.size
.x
; i
++) {
47 for (let j
=0; j
<this.size
.y
; j
++) {
48 if (this.getColor(i
, j
) == oppCol
&& this.isMated(i
, j
, color
)) {
50 new PiPo({x: i
, y: j
, c: oppCol
, p: this.getPiece(i
, j
)})
55 this.undoOnBoard(move);
56 move.next
= (mv
.vanish
.length
> 0 ? mv : null);
59 // is piece on square x,y mated by color?
60 isMated(x
, y
, color
) {
61 const myColor
= C
.GetOppTurn(color
);
62 if (!super.underAttack([x
, y
], color
))
64 for (let i
=0; i
<this.size
.x
; i
++) {
65 for (let j
=0; j
<this.size
.y
; j
++) {
66 if (this.getColor(i
, j
) == myColor
) {
67 const movesIJ
= super.getPotentialMovesFrom([i
, j
], myColor
);
68 for (let move of movesIJ
) {
69 this.playOnBoard(move);
70 let testSquare
= [x
, y
];
71 if (i
== x
&& j
== y
) {
72 // The mated-candidate has moved itself
73 testSquare
= [move.end
.x
, move.end
.y
]; }
74 const res
= this.underAttack(testSquare
, color
);
75 this.undoOnBoard(move);
86 return false; //not relevant here
90 return moves
; //TODO?: over-simplification to be fixed later