Some code cleaning + clarifying (TODO: work on variables names)
[vchess.git] / public / javascripts / variants / Grand.js
1 // NOTE: initial setup differs from the original; see
2 // https://www.chessvariants.com/large.dir/freeling.html
3 class GrandRules extends ChessRules
4 {
5 static getPpath(b)
6 {
7 const V = VariantRules;
8 return ([V.MARSHALL,V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b;
9 }
10
11 initVariables(fen)
12 {
13 super.initVariables(fen);
14 this.captures = { "w": {}, "b": {} }; //for promotions
15 }
16
17 static get size() { return [10,10]; }
18
19 static get MARSHALL() { return 'm'; } //rook+knight
20 static get CARDINAL() { return 'c'; } //bishop+knight
21
22 // En-passant after 2-sq or 3-sq jumps
23 getEpSquare(move)
24 {
25 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
26 if (this.getPiece(sx,sy) == VariantRules.PAWN && Math.abs(sx - ex) >= 2)
27 {
28 const step = (ex-sx) / Math.abs(ex-sx);
29 let res = [{
30 x: sx + step,
31 y: sy
32 }];
33 if (sx + 2*step != ex) //3-squares move
34 {
35 res.push({
36 x: sx + 2*step,
37 y: sy
38 });
39 }
40 return res;
41 }
42 return undefined; //default
43 }
44
45 getPotentialMovesFrom([x,y])
46 {
47 switch (this.getPiece(x,y))
48 {
49 case VariantRules.MARSHALL:
50 return this.getPotentialMarshallMoves([x,y]);
51 case VariantRules.CARDINAL:
52 return this.getPotentialCardinalMoves([x,y]);
53 default:
54 return super.getPotentialMovesFrom([x,y])
55 }
56 }
57
58 // Special pawn rules: promotions to captured friendly pieces,
59 // optional on ranks 8-9 and mandatory on rank 10.
60 getPotentialPawnMoves([x,y])
61 {
62 const color = this.turn;
63 let moves = [];
64 const V = VariantRules;
65 const [sizeX,sizeY] = VariantRules.size;
66 const shift = (color == "w" ? -1 : 1);
67 const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]);
68 const lastRanks = (color == "w" ? [0,1,2] : [sizeX-1,sizeX-2,sizeX-3]);
69
70 if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRanks[0])
71 {
72 // Normal moves
73 if (this.board[x+shift][y] == V.EMPTY)
74 {
75 moves.push(this.getBasicMove([x,y], [x+shift,y]));
76 if (startRanks.includes(x) && this.board[x+2*shift][y] == V.EMPTY)
77 {
78 // Two squares jump
79 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
80 if (x == startRanks[0] && this.board[x+3*shift][y] == V.EMPTY)
81 {
82 // 3-squares jump
83 moves.push(this.getBasicMove([x,y], [x+3*shift,y]));
84 }
85 }
86 }
87 // Captures
88 if (y>0 && this.canTake([x,y], [x+shift,y-1])
89 && this.board[x+shift][y-1] != V.EMPTY)
90 {
91 moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
92 }
93 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
94 && this.board[x+shift][y+1] != V.EMPTY)
95 {
96 moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
97 }
98 }
99
100 if (lastRanks.includes(x+shift))
101 {
102 // Promotion
103 let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN,V.MARSHALL,V.CARDINAL];
104 promotionPieces.forEach(p => {
105 if (!this.captures[color][p] || this.captures[color][p]==0)
106 return;
107 // Normal move
108 if (this.board[x+shift][y] == V.EMPTY)
109 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
110 // Captures
111 if (y>0 && this.canTake([x,y], [x+shift,y-1])
112 && this.board[x+shift][y-1] != V.EMPTY)
113 {
114 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
115 }
116 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
117 && this.board[x+shift][y+1] != V.EMPTY)
118 {
119 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
120 }
121 });
122 }
123
124 // En passant
125 const Lep = this.epSquares.length;
126 const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
127 if (!!epSquare)
128 {
129 for (let epsq of epSquare)
130 {
131 // TODO: some redundant checks
132 if (epsq.x == x+shift && Math.abs(epsq.y - y) == 1)
133 {
134 let epStep = epsq.y - y;
135 var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
136 enpassantMove.vanish.push({
137 x: x,
138 y: y+epStep,
139 p: 'p',
140 c: this.getColor(x,y+epStep)
141 });
142 moves.push(enpassantMove);
143 }
144 }
145 }
146
147 return moves;
148 }
149
150 // TODO: different castle?
151
152 getPotentialMarshallMoves(sq)
153 {
154 const V = VariantRules;
155 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat(
156 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"));
157 }
158
159 getPotentialCardinalMoves(sq)
160 {
161 const V = VariantRules;
162 return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat(
163 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"));
164 }
165
166 isAttacked(sq, colors)
167 {
168 return super.isAttacked(sq, colors)
169 || this.isAttackedByMarshall(sq, colors)
170 || this.isAttackedByCardinal(sq, colors);
171 }
172
173 isAttackedByMarshall(sq, colors)
174 {
175 const V = VariantRules;
176 return this.isAttackedBySlideNJump(sq, colors, V.MARSHALL, V.steps[V.ROOK])
177 || this.isAttackedBySlideNJump(
178 sq, colors, V.MARSHALL, V.steps[V.KNIGHT], "oneStep");
179 }
180
181 isAttackedByCardinal(sq, colors)
182 {
183 const V = VariantRules;
184 return this.isAttackedBySlideNJump(sq, colors, V.CARDINAL, V.steps[V.BISHOP])
185 || this.isAttackedBySlideNJump(
186 sq, colors, V.CARDINAL, V.steps[V.KNIGHT], "oneStep");
187 }
188
189 updateVariables(move)
190 {
191 super.updateVariables(move);
192 if (move.vanish.length==2 && move.appear.length==1
193 && move.vanish[1].p != VariantRules.PAWN)
194 {
195 // Capture: update this.captures
196 if (!this.captures[move.vanish[1].c][move.vanish[1].p])
197 this.captures[move.vanish[1].c][move.vanish[1].p] = 1;
198 else
199 this.captures[move.vanish[1].c][move.vanish[1].p]++;
200 }
201 }
202
203 unupdateVariables(move)
204 {
205 super.unupdateVariables(move);
206 if (move.vanish.length==2 && move.appear.length==1
207 && move.vanish[1].p != VariantRules.PAWN)
208 {
209 this.captures[move.vanish[1].c][move.vanish[1].p] =
210 Math.max(0, this.captures[move.vanish[1].c][move.vanish[1].p]-1);
211 }
212 }
213
214 static get VALUES() {
215 return Object.assign(
216 ChessRules.VALUES,
217 {'c': 5, 'm': 7} //experimental
218 );
219 }
220
221 static get SEARCH_DEPTH() { return 2; }
222
223 // TODO: this function could be generalized and shared better
224 static GenRandInitFen()
225 {
226 let pieces = { "w": new Array(10), "b": new Array(10) };
227 // Shuffle pieces on first and last rank
228 for (let c of ["w","b"])
229 {
230 let positions = _.range(10);
231
232 // Get random squares for bishops
233 let randIndex = 2 * _.random(4);
234 let bishop1Pos = positions[randIndex];
235 // The second bishop must be on a square of different color
236 let randIndex_tmp = 2 * _.random(4) + 1;
237 let bishop2Pos = positions[randIndex_tmp];
238 // Remove chosen squares
239 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
240 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
241
242 // Get random squares for knights
243 randIndex = _.random(7);
244 let knight1Pos = positions[randIndex];
245 positions.splice(randIndex, 1);
246 randIndex = _.random(6);
247 let knight2Pos = positions[randIndex];
248 positions.splice(randIndex, 1);
249
250 // Get random square for queen
251 randIndex = _.random(5);
252 let queenPos = positions[randIndex];
253 positions.splice(randIndex, 1);
254
255 // ...random square for marshall
256 randIndex = _.random(4);
257 let marshallPos = positions[randIndex];
258 positions.splice(randIndex, 1);
259
260 // ...random square for cardinal
261 randIndex = _.random(3);
262 let cardinalPos = positions[randIndex];
263 positions.splice(randIndex, 1);
264
265 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
266 let rook1Pos = positions[0];
267 let kingPos = positions[1];
268 let rook2Pos = positions[2];
269
270 // Finally put the shuffled pieces in the board array
271 pieces[c][rook1Pos] = 'r';
272 pieces[c][knight1Pos] = 'n';
273 pieces[c][bishop1Pos] = 'b';
274 pieces[c][queenPos] = 'q';
275 pieces[c][marshallPos] = 'm';
276 pieces[c][cardinalPos] = 'c';
277 pieces[c][kingPos] = 'k';
278 pieces[c][bishop2Pos] = 'b';
279 pieces[c][knight2Pos] = 'n';
280 pieces[c][rook2Pos] = 'r';
281 }
282 let fen = pieces["b"].join("") +
283 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
284 pieces["w"].join("").toUpperCase() +
285 " 1111";
286 return fen;
287 }
288 }