base_rules.js refactoring. draft state (untested)
[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,
e2be4b04
BA
9 input: [
10 {
11 label: "Cleopatra",
12 variable: "cleopatra",
13 type: "checkbox",
14 defaut: false
15 }
16 ],
b99ce1fb
BA
17 styles: [
18 "balance",
19 "cylinder",
20 "dark",
21 "doublemove",
22 "progressive",
23 "zen"
24 ]
41534b92
BA
25 };
26 }
27
28 get hasEnpassant() {
29 return false;
30 }
31
082e639a
BA
32 canTake() {
33 return false;
34 }
35
e2be4b04
BA
36 pieces(color, x, y) {
37 if (!this.options["cleopatra"])
38 return super.pieces(color, x, y);
65cf1690
BA
39 const allSpecs = super.pieces(color, x, y);
40 return Object.assign({},
41 allSpecs,
42 {'q': Object.assign({}, allSpecs['q'], {"class": "cleopatra"})}
43 );
e2be4b04
BA
44 }
45
41534b92 46 postProcessPotentialMoves(moves) {
41534b92 47 moves.forEach(m => {
e2be4b04
BA
48 m.flips = [];
49 if (!this.options["cleopatra"] || m.vanish[0].p == 'q') {
50 super.playOnBoard(m);
af9c9be3
BA
51 let attacks = super.findDestSquares(
52 [m.end.x, m.end.y],
53 {attackOnly: true, segments: false},
54 ([x, y] => this.canTake([m.end.x, m.end.y], [x, y]))
55 );
e2be4b04 56 if (this.options["zen"]) {
af9c9be3
BA
57 const zenAttacks = super.findCapturesOn(
58 [m.end.x, m.end.y],
59 {segments: false},
60 ([x, y] => this.canTake([m.end.x, m.end.y], [x, y]))
61 );
62 Array.prototype.push.apply(attacks, zenAttacks);
e2be4b04
BA
63 }
64 super.undoOnBoard(m);
af9c9be3 65 attacks.forEach(a => m.flips.push({x: a.sq[0], y: a.sq[1]}));
41534b92 66 }
41534b92
BA
67 });
68 return moves;
69 }
70
6997e386
BA
71 playOnBoard(move) {
72 super.playOnBoard(move);
73 this.flipColorOf(move.flips);
74 }
75 undoOnBoard(move) {
76 super.undoOnBoard(move);
77 this.flipColorOf(move.flips);
78 }
79
80 flipColorOf(flips) {
81 for (let xy of flips) {
82 const newColor = C.GetOppCol(this.getColor(xy.x, xy.y));
83 this.board[xy.x][xy.y] = newColor + this.board[xy.x][xy.y][1];
84 }
85 }
86
6997e386 87 // Moves cannot flip our king's color, so all are valid
41534b92 88 filterValid(moves) {
41534b92
BA
89 return moves;
90 }
91
6997e386 92 // A king under (regular) check flips color, and the game is over.
082e639a 93 underCheck() {
41534b92
BA
94 return false;
95 }
96
6997e386
BA
97 playVisual(move, r) {
98 super.playVisual(move, r);
99 move.flips.forEach(f => {
100 this.g_pieces[f.x][f.y].classList.toggle("white");
101 this.g_pieces[f.x][f.y].classList.toggle("black");
102 });
103 }
104
41534b92 105};