1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
4 export default class BenedictRules
extends ChessRules
{
8 select: C
.Options
.select
,
28 // Find potential captures from a square
29 // follow steps from x,y until something is met.
31 const [color
, piece
] = [this.getColor(x
, y
), this.getPiece(x
, y
)];
32 const oppCol
= C
.GetOppCol(color
);
34 const specs
= this.pieces(color
, x
, y
)[piece
];
35 const attacks
= specs
.attack
|| specs
.moves
;
36 for (let a
of attacks
) {
37 outerLoop: for (let step
of a
.steps
) {
38 let [i
, j
] = [x
+ step
[0], this.getY(y
+ step
[1])];
40 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
41 if (a
.range
<= nbSteps
++)
44 j
= this.getY(j
+ step
[1]);
47 this.onBoard(i
, j
) && this.getColor(i
, j
) == oppCol
&&
48 (!this.options
["zen"] || this.getPieceType(i
, j
) == "k")
50 squares
[C
.CoordsToSquare({x: i
, y: j
})] = true;
54 return Object
.keys(squares
);
57 postProcessPotentialMoves(moves
) {
60 let attacks
= this.findAttacks([m
.end
.x
, m
.end
.y
])
61 if (this.options
["zen"]) {
63 super.findCapturesOn([m
.end
.x
, m
.end
.y
], {zen: true}).forEach(c
=> {
64 endSquares
[C
.CoordsToSquare(c
.end
)] = true;
66 Array
.prototype.push
.apply(attacks
, Object
.keys(endSquares
));
70 attacks
.map(C
.SquareToCoords
).forEach(a
=> {
71 m
.flips
.push({x: a
.x
, y: a
.y
});
78 super.playOnBoard(move);
79 this.flipColorOf(move.flips
);
82 super.undoOnBoard(move);
83 this.flipColorOf(move.flips
);
87 for (let xy
of flips
) {
88 const newColor
= C
.GetOppCol(this.getColor(xy
.x
, xy
.y
));
89 this.board
[xy
.x
][xy
.y
] = newColor
+ this.board
[xy
.x
][xy
.y
][1];
94 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
95 // If enemy king is flipped: game over
96 const oppCol
= C
.GetOppCol(move.vanish
[0].c
);
97 const oppKingPos
= this.searchKingPos(oppCol
);
98 if (oppKingPos
[0] < 0) {
104 super.postPlay(move);
107 // Moves cannot flip our king's color, so all are valid
112 // A king under (regular) check flips color, and the game is over.
117 playVisual(move, r
) {
118 super.playVisual(move, r
);
119 move.flips
.forEach(f
=> {
120 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("white");
121 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("black");