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