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 const allSpecs
= super.pieces(color
, x
, y
);
40 return Object
.assign({},
42 {'q': Object
.assign({}, allSpecs
['q'], {"class": "cleopatra"})}
46 // Find potential captures from a square
47 // follow steps from x,y until something is met.
49 const [color
, piece
] = [this.getColor(x
, y
), this.getPiece(x
, y
)];
50 const oppCol
= C
.GetOppCol(color
);
52 const specs
= this.pieces(color
, x
, y
)[piece
];
53 const attacks
= specs
.attack
|| specs
.moves
;
54 for (let a
of attacks
) {
55 outerLoop: for (let step
of a
.steps
) {
56 let [i
, j
] = [x
+ step
[0], this.getY(y
+ step
[1])];
58 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
59 if (a
.range
<= nbSteps
++)
62 j
= this.getY(j
+ step
[1]);
65 this.onBoard(i
, j
) && this.getColor(i
, j
) == oppCol
&&
66 (!this.options
["zen"] || this.getPieceType(i
, j
) == "k")
68 squares
[C
.CoordsToSquare({x: i
, y: j
})] = true;
72 return Object
.keys(squares
);
75 postProcessPotentialMoves(moves
) {
78 if (!this.options
["cleopatra"] || m
.vanish
[0].p
== 'q') {
80 let attacks
= this.findAttacks([m
.end
.x
, m
.end
.y
])
81 if (this.options
["zen"]) {
83 super.findCapturesOn([m
.end
.x
, m
.end
.y
], {zen: true}).forEach(c
=> {
84 endSquares
[C
.CoordsToSquare(c
.end
)] = true;
86 Array
.prototype.push
.apply(attacks
, Object
.keys(endSquares
));
89 attacks
.map(C
.SquareToCoords
).forEach(a
=> {
90 m
.flips
.push({x: a
.x
, y: a
.y
});
98 super.playOnBoard(move);
99 this.flipColorOf(move.flips
);
102 super.undoOnBoard(move);
103 this.flipColorOf(move.flips
);
107 for (let xy
of flips
) {
108 const newColor
= C
.GetOppCol(this.getColor(xy
.x
, xy
.y
));
109 this.board
[xy
.x
][xy
.y
] = newColor
+ this.board
[xy
.x
][xy
.y
][1];
114 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
115 // If enemy king is flipped: game over
116 const oppCol
= C
.GetOppCol(move.vanish
[0].c
);
117 const oppKingPos
= this.searchKingPos(oppCol
);
118 if (oppKingPos
[0] < 0) {
124 super.postPlay(move);
127 // Moves cannot flip our king's color, so all are valid
132 // A king under (regular) check flips color, and the game is over.
137 playVisual(move, r
) {
138 super.playVisual(move, r
);
139 move.flips
.forEach(f
=> {
140 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("white");
141 this.g_pieces
[f
.x
][f
.y
].classList
.toggle("black");