2bc60e739bb624095d1285bee6a94ef15d84c635
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
,
12 variable: "cleopatra",
37 if (!this.options
["cleopatra"])
38 return super.pieces(color
, x
, y
);
39 return Object
.assign({}, super.pieces(color
, x
, y
), {
45 [0, 1], [0, -1], [1, 0], [-1, 0],
46 [1, 1], [1, -1], [-1, 1], [-1, -1]
54 // Find potential captures from a square
55 // follow steps from x,y until something is met.
57 const [color
, piece
] = [this.getColor(x
, y
), this.getPiece(x
, y
)];
58 const oppCol
= C
.GetOppCol(color
);
60 const specs
= this.pieces(color
, x
, y
)[piece
];
61 const attacks
= specs
.attack
|| specs
.moves
;
62 for (let a
of attacks
) {
63 outerLoop: for (let step
of a
.steps
) {
64 let [i
, j
] = [x
+ step
[0], this.getY(y
+ step
[1])];
66 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
67 if (a
.range
<= nbSteps
++)
70 j
= this.getY(j
+ step
[1]);
73 this.onBoard(i
, j
) && this.getColor(i
, j
) == oppCol
&&
74 (!this.options
["zen"] || this.getPieceType(i
, j
) == "k")
76 squares
[C
.CoordsToSquare({x: i
, y: j
})] = true;
80 return Object
.keys(squares
);
83 postProcessPotentialMoves(moves
) {
86 if (!this.options
["cleopatra"] || m
.vanish
[0].p
== 'q') {
88 let attacks
= this.findAttacks([m
.end
.x
, m
.end
.y
])
89 if (this.options
["zen"]) {
91 super.findCapturesOn([m
.end
.x
, m
.end
.y
], {zen: true}).forEach(c
=> {
92 endSquares
[C
.CoordsToSquare(c
.end
)] = true;
94 Array
.prototype.push
.apply(attacks
, Object
.keys(endSquares
));
97 attacks
.map(C
.SquareToCoords
).forEach(a
=> {
98 m
.flips
.push({x: a
.x
, y: a
.y
});
106 super.playOnBoard(move);
107 this.flipColorOf(move.flips
);
110 super.undoOnBoard(move);
111 this.flipColorOf(move.flips
);
115 for (let xy
of flips
) {
116 const newColor
= C
.GetOppCol(this.getColor(xy
.x
, xy
.y
));
117 this.board
[xy
.x
][xy
.y
] = newColor
+ this.board
[xy
.x
][xy
.y
][1];
122 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
123 // If enemy king is flipped: game over
124 const oppCol
= C
.GetOppCol(move.vanish
[0].c
);
125 const oppKingPos
= this.searchKingPos(oppCol
);
126 if (oppKingPos
[0] < 0) {
132 super.postPlay(move);
135 // Moves cannot flip our king's color, so all are valid
140 // A king under (regular) check flips color, and the game is over.
145 playVisual(move, r
) {
146 super.playVisual(move, r
);
147 move.flips
.forEach(f
=> {
148 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("white");
149 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("black");