After base_rules refactoring. Still a few issues (Dark...)
[xogo.git] / variants / Benedict / class.js
CommitLineData
41534b92
BA
1import ChessRules from "/base_rules.js";
2import PiPo from "/utils/PiPo.js";
3
4export default class BenedictRules extends ChessRules {
5
6 static get Options() {
7 return {
cc2c7183 8 select: C.Options.select,
41534b92 9 check: [],
b99ce1fb
BA
10 styles: [
11 "balance",
12 "cylinder",
13 "dark",
14 "doublemove",
15 "progressive",
16 "zen"
17 ]
41534b92
BA
18 };
19 }
20
21 get hasEnpassant() {
22 return false;
23 }
24
41534b92
BA
25 canTake() {
26 return false;
27 }
28
29 // Find potential captures from a square
30 // follow steps from x,y until something is met.
31 findAttacks([x, y]) {
32 const [color, piece] = [this.getColor(x, y), this.getPiece(x, y)];
cc2c7183 33 const oppCol = C.GetOppCol(color);
41534b92 34 let squares = {};
c9ab0340
BA
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])];
40 let nbSteps = 1;
41 while (this.onBoard(i, j) && this.board[i][j] == "") {
42 if (a.range <= nbSteps++) continue outerLoop;
43 i += step[0];
44 j = this.computeY(j + step[1]);
45 }
46 if (this.onBoard(i, j) && this.getColor(i, j) == oppCol)
47 squares[C.CoordsToSquare({x: i, y: j})] = true;
41534b92 48 }
41534b92
BA
49 }
50 return Object.keys(squares);
51 }
52
53 postProcessPotentialMoves(moves) {
54 if (moves.length == 0) return moves;
55 const [x, y] = [moves[0].end.x, moves[0].end.y];
56 const color = this.getColor(moves[0].start.x, moves[0].start.y);
cc2c7183 57 const oppCol = C.GetOppCol(color);
41534b92
BA
58 moves = super.postProcessPotentialMoves(moves);
59 moves.forEach(m => {
60 this.playOnBoard(m);
61 let attacks;
62 if (this.options["zen"]) {
63 let endSquares = {};
64 super.getZenCaptures(x, y).forEach(c => {
cc2c7183 65 endSquares[C.CoordsToSquare(c.end)] = true;
41534b92
BA
66 });
67 attacks = Object.keys(endSquares);
68 }
69 else attacks = this.findAttacks([m.end.x, m.end.y])
70 this.undoOnBoard(m);
cc2c7183 71 attacks.map(C.SquareToCoords).forEach(a => {
41534b92
BA
72 const p = this.getPiece(a.x, a.y);
73 m.appear.push(new PiPo({x: a.x, y: a.y, c: color, p: p}));
74 m.vanish.push(new PiPo({x: a.x, y: a.y, c: oppCol, p: p}));
75 });
76 });
77 return moves;
78 }
79
80 // Moves cannot flip our king's color, so (almost) all are valid
81 filterValid(moves) {
82 if (this.options["balance"] && [1, 3].includes(this.movesCount))
cc2c7183 83 return moves.filter(m => m.vanish.every(v => v.p != C.KING));
41534b92
BA
84 return moves;
85 }
86
87 // Since it's used just for the king, and there are no captures:
88 underCheck(square, color) {
89 return false;
90 }
91
92};