Finish base_rules.js refactoring
[xogo.git] / variants / Benedict / class.js
... / ...
CommitLineData
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 {
8 select: C.Options.select,
9 input: [
10 {
11 label: "Cleopatra",
12 variable: "cleopatra",
13 type: "checkbox",
14 defaut: false
15 }
16 ],
17 styles: [
18 "balance",
19 "cylinder",
20 "dark",
21 "doublemove",
22 "progressive",
23 "zen"
24 ]
25 };
26 }
27
28 get hasEnpassant() {
29 return false;
30 }
31
32 canTake() {
33 return false;
34 }
35
36 pieces(color, x, y) {
37 if (!this.options["cleopatra"])
38 return super.pieces(color, x, y);
39 const allSpecs = super.pieces(color, x, y);
40 return Object.assign({},
41 allSpecs,
42 {'q': Object.assign({}, allSpecs['q'], {"class": "cleopatra"})}
43 );
44 }
45
46 postProcessPotentialMoves(moves) {
47 moves.forEach(m => {
48 m.flips = [];
49 if (!this.options["cleopatra"] || m.vanish[0].p == 'q') {
50 super.playOnBoard(m);
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 );
56 if (this.options["zen"]) {
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);
63 }
64 super.undoOnBoard(m);
65 attacks.forEach(a => m.flips.push({x: a.sq[0], y: a.sq[1]}));
66 }
67 });
68 return moves;
69 }
70
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
87 // Moves cannot flip our king's color, so all are valid
88 filterValid(moves) {
89 return moves;
90 }
91
92 // A king under (regular) check flips color, and the game is over.
93 underCheck() {
94 return false;
95 }
96
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
105};