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
,
25 // Find potential captures from a square
26 // follow steps from x,y until something is met.
28 const [color
, piece
] = [this.getColor(x
, y
), this.getPiece(x
, y
)];
29 const oppCol
= C
.GetOppCol(color
);
31 const specs
= this.pieces(color
, x
, y
)[piece
];
32 const attacks
= specs
.attack
|| specs
.moves
;
33 for (let a
of attacks
) {
34 outerLoop: for (let step
of a
.steps
) {
35 let [i
, j
] = [x
+ step
[0], this.computeY(y
+ step
[1])];
37 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
38 if (a
.range
<= nbSteps
++)
41 j
= this.computeY(j
+ step
[1]);
44 this.onBoard(i
, j
) && this.getColor(i
, j
) == oppCol
&&
45 (!this.options
["zen"] || this.getPieceType(i
, j
) == "k")
47 squares
[C
.CoordsToSquare({x: i
, y: j
})] = true;
51 return Object
.keys(squares
);
54 postProcessPotentialMoves(moves
) {
55 if (moves
.length
== 0)
57 const color
= this.getColor(moves
[0].start
.x
, moves
[0].start
.y
);
58 const oppCol
= C
.GetOppCol(color
);
59 // Remove captures (NOTE: altering canTake has side effects,
60 // Benedict is still based on captures even if they are forbidden):
61 moves
= super.postProcessPotentialMoves(moves
)
62 .filter(m
=> this.board
[m
.end
.x
][m
.end
.y
] == "");
65 let attacks
= this.findAttacks([m
.end
.x
, m
.end
.y
])
66 if (this.options
["zen"]) {
68 super.findCapturesOn([m
.end
.x
, m
.end
.y
], true).forEach(c
=> {
69 endSquares
[C
.CoordsToSquare(c
.end
)] = true;
71 Array
.prototype.push
.apply(attacks
, Object
.keys(endSquares
));
75 attacks
.map(C
.SquareToCoords
).forEach(a
=> {
76 m
.flips
.push({x: a
.x
, y: a
.y
});
83 super.playOnBoard(move);
84 this.flipColorOf(move.flips
);
87 super.undoOnBoard(move);
88 this.flipColorOf(move.flips
);
92 for (let xy
of flips
) {
93 const newColor
= C
.GetOppCol(this.getColor(xy
.x
, xy
.y
));
94 this.board
[xy
.x
][xy
.y
] = newColor
+ this.board
[xy
.x
][xy
.y
][1];
99 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
100 // If enemy king is flipped: game over
101 const oppCol
= C
.GetOppCol(move.vanish
[0].c
);
102 const oppKingPos
= this.searchKingPos(oppCol
);
103 if (oppKingPos
[0] < 0) {
109 super.postPlay(move);
112 // Moves cannot flip our king's color, so all are valid
117 // A king under (regular) check flips color, and the game is over.
118 underCheck(square
, color
) {
122 playVisual(move, r
) {
123 super.playVisual(move, r
);
124 move.flips
.forEach(f
=> {
125 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("white");
126 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("black");