Reorganize folders (untested Baroque). Draft Go
[xogo.git] / variants / Baroque / class.js
CommitLineData
0e466aac
BA
1import ChessRules from "/base_rules.js";
2import GiveawayRules from "/variants/Giveaway/class.js";
a89fb4e9 3import AbstractSpecialCaptureRules from "/variants/_SpecialCaptures.js";
0e466aac
BA
4import {Random} from "/utils/alea.js";
5import PiPo from "/utils/PiPo.js";
6import Move from "/utils/Move.js";
7
a89fb4e9 8export default class BaroqueRules extends AbstractSpecialCaptureRules {
0e466aac
BA
9
10 static get Options() {
11 return {
12 select: C.Options.Select,
13 input: [
14 {
15 label: "Capture king",
16 variable: "taking",
17 type: "checkbox",
18 defaut: false
19 }
20 ],
21 styles: [
22 "balance",
23 "capture",
24 "crazyhouse",
25 "cylinder",
26 "doublemove",
27 "progressive",
28 "recycle",
29 "teleport"
30 ]
31 };
32 }
33
34 get hasFlags() {
35 return false;
36 }
0e466aac
BA
37
38 genRandInitBaseFen() {
39 if (this.options["randomness"] == 0)
40 return "rnbkqbnm/pppppppp/8/8/8/8/PPPPPPPP/MNBQKBNR";
41 const options = Object.assign({mode: "suicide"}, this.options);
42 const gr = new GiveawayRules({options: options, genFenOnly: true});
43 let res = gr.genRandInitBaseFen();
44 let immPos = {};
45 for (let c of ['w', 'b']) {
46 const rookChar = (c == 'w' ? 'R' : 'r');
47 switch (Random.randInt(2)) {
48 case 0:
49 immPos[c] = res.fen.indexOf(rookChar);
50 break;
51 case 1:
52 immPos[c] = res.fen.lastIndexOf(rookChar);
53 break;
54 }
55 }
56 res.fen = res.fen.substring(0, immPos['b']) + 'i' +
57 res.fen.substring(immPos['b'] + 1, immPos['w']) + 'I' +
58 res.fen.substring(immPos['w'] + 1);
59 return res;
60 }
61
0e466aac
BA
62 pieces() {
63 return Object.assign({},
64 super.pieces(),
65 {
66 'p': {
a89fb4e9 67 "class": "pawn", //pincer
0e466aac
BA
68 moves: [
69 {steps: [[0, 1], [0, -1], [1, 0], [-1, 0]]}
70 ]
71 },
72 'r': {
a89fb4e9 73 "class": "rook", //coordinator
0e466aac
BA
74 moves: [
75 {
76 steps: [
77 [1, 0], [0, 1], [-1, 0], [0, -1],
78 [1, 1], [1, -1], [-1, 1], [-1, -1]
79 ]
80 }
81 ]
82 },
83 'n': {
a89fb4e9 84 "class": "knight", //long-leaper
0e466aac
BA
85 moveas: 'r'
86 },
87 'b': {
a89fb4e9 88 "class": "bishop", //chameleon
0e466aac
BA
89 moveas: 'r'
90 },
91 'q': {
a89fb4e9 92 "class": "queen", //withdrawer
0e466aac
BA
93 moveas: 'r'
94 },
95 'i': {
96 "class": "immobilizer",
a89fb4e9 97 moveas: 'r'
0e466aac
BA
98 }
99 }
100 );
101 }
102
103 // Is piece on square (x,y) immobilized?
104 isImmobilized([x, y]) {
105 const piece = this.getPiece(x, y);
106 const color = this.getColor(x, y);
107 const oppCol = C.GetOppCol(color);
108 const adjacentSteps = this.pieces()['k'].moves[0].steps;
109 for (let step of adjacentSteps) {
110 const [i, j] = [x + step[0], this.getY(y + step[1])];
111 if (
112 this.onBoard(i, j) &&
113 this.board[i][j] != "" &&
114 this.getColor(i, j) == oppCol
115 ) {
116 const oppPiece = this.getPiece(i, j);
117 if (oppPiece == 'i') {
118 // Moving is possible only if this immobilizer is neutralized
119 for (let step2 of adjacentSteps) {
120 const [i2, j2] = [i + step2[0], this.getY(j + step2[1])];
121 if (i2 == x && j2 == y)
122 continue; //skip initial piece!
123 if (
124 this.onBoard(i2, j2) &&
125 this.board[i2][j2] != "" &&
126 this.getColor(i2, j2) == color
127 ) {
128 if (['b', 'i'].includes(this.getPiece(i2, j2)))
129 return false;
130 }
131 }
132 return true; //immobilizer isn't neutralized
133 }
134 // Chameleons can't be immobilized twice,
135 // because there is only one immobilizer
136 if (oppPiece == 'b' && piece == 'i')
137 return true;
138 }
139 }
140 return false;
141 }
142
143 canTake([x1, y1], [x2, y2]) {
144 // Deactivate standard captures, except for king:
145 return (
146 this.getPiece(x1, y1) == 'k' &&
147 this.getColor(x1, y1) != this.getColor(x2, y2)
148 );
149 }
150
151 postProcessPotentialMoves(moves) {
152 if (moves.length == 0)
153 return [];
154 switch (moves[0].vanish[0].p) {
155 case 'p':
a89fb4e9 156 this.addPincerCaptures(moves);
0e466aac
BA
157 break;
158 case 'r':
a89fb4e9 159 this.addCoordinatorCaptures(moves);
0e466aac
BA
160 break;
161 case 'n':
162 const [x, y] = [moves[0].start.x, moves[0].start.y];
a89fb4e9 163 moves = moves.concat(this.getLeaperCaptures([x, y]));
0e466aac
BA
164 break;
165 case 'b':
a89fb4e9 166 moves = this.getChameleonCaptures(moves, "pull");
0e466aac
BA
167 break;
168 case 'q':
a89fb4e9 169 this.addPushmePullyouCaptures(moves, false, "pull");
0e466aac
BA
170 break;
171 }
172 return moves;
173 }
174
0e466aac 175};