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