Start Absoption
[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
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)];
cc2c7183 41 const oppCol = C.GetOppCol(color);
41534b92
BA
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)
cc2c7183 54 squares[C.CoordsToSquare({x: i, y: j})] = true;
41534b92
BA
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);
cc2c7183 63 const oppCol = C.GetOppCol(color);
41534b92
BA
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 => {
cc2c7183 71 endSquares[C.CoordsToSquare(c.end)] = true;
41534b92
BA
72 });
73 attacks = Object.keys(endSquares);
74 }
75 else attacks = this.findAttacks([m.end.x, m.end.y])
76 this.undoOnBoard(m);
cc2c7183 77 attacks.map(C.SquareToCoords).forEach(a => {
41534b92
BA
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))
cc2c7183 89 return moves.filter(m => m.vanish.every(v => v.p != C.KING));
41534b92
BA
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};