68667e132296272b8351cf5e13b5366d4b7c0733
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
,
29 // Find potential captures from a square
30 // follow steps from x,y until something is met.
32 const [color
, piece
] = [this.getColor(x
, y
), this.getPiece(x
, y
)];
33 const oppCol
= C
.GetOppCol(color
);
35 const specs
= this.pieces(color
, x
, y
)[piece
];
36 const attacks
= specs
.attack
|| specs
.moves
;
37 for (let a
of attacks
) {
38 outerLoop: for (let step
of a
.steps
) {
39 let [i
, j
] = [x
+ step
[0], this.computeY(y
+ step
[1])];
41 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
42 if (a
.range
<= nbSteps
++)
45 j
= this.computeY(j
+ step
[1]);
48 this.onBoard(i
, j
) && this.getColor(i
, j
) == oppCol
&&
49 (!this.options
["zen"] || this.getPieceType(i
, j
) == "k")
51 squares
[C
.CoordsToSquare({x: i
, y: j
})] = true;
55 return Object
.keys(squares
);
58 postProcessPotentialMoves(moves
) {
61 let attacks
= this.findAttacks([m
.end
.x
, m
.end
.y
])
62 if (this.options
["zen"]) {
64 super.findCapturesOn([m
.end
.x
, m
.end
.y
], {zen: true}).forEach(c
=> {
65 endSquares
[C
.CoordsToSquare(c
.end
)] = true;
67 Array
.prototype.push
.apply(attacks
, Object
.keys(endSquares
));
71 attacks
.map(C
.SquareToCoords
).forEach(a
=> {
72 m
.flips
.push({x: a
.x
, y: a
.y
});
79 super.playOnBoard(move);
80 this.flipColorOf(move.flips
);
83 super.undoOnBoard(move);
84 this.flipColorOf(move.flips
);
88 for (let xy
of flips
) {
89 const newColor
= C
.GetOppCol(this.getColor(xy
.x
, xy
.y
));
90 this.board
[xy
.x
][xy
.y
] = newColor
+ this.board
[xy
.x
][xy
.y
][1];
95 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
96 // If enemy king is flipped: game over
97 const oppCol
= C
.GetOppCol(move.vanish
[0].c
);
98 const oppKingPos
= this.searchKingPos(oppCol
);
99 if (oppKingPos
[0] < 0) {
105 super.postPlay(move);
108 // Moves cannot flip our king's color, so all are valid
113 // A king under (regular) check flips color, and the game is over.
118 playVisual(move, r
) {
119 super.playVisual(move, r
);
120 move.flips
.forEach(f
=> {
121 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("white");
122 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("black");