On the way to problems: saving state [not functional yet]
[vchess.git] / public / javascripts / variants / Grand.js
CommitLineData
92342261
BA
1// NOTE: initial setup differs from the original; see
2// https://www.chessvariants.com/large.dir/freeling.html
a37076f1
BA
3class GrandRules extends ChessRules
4{
5 static getPpath(b)
6 {
c6052161 7 return ([V.MARSHALL,V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b;
a37076f1
BA
8 }
9
c6052161
BA
10 initVariables(fen)
11 {
12 super.initVariables(fen);
13 this.captures = { "w": {}, "b": {} }; //for promotions
14 }
15
0b7d99ec 16 static get size() { return {x:10,y:10}; }
c6052161 17
a37076f1
BA
18 static get MARSHALL() { return 'm'; } //rook+knight
19 static get CARDINAL() { return 'c'; } //bishop+knight
20
c6052161 21 // En-passant after 2-sq or 3-sq jumps
a37076f1
BA
22 getEpSquare(move)
23 {
24 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
0b7d99ec 25 if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2)
a37076f1 26 {
c6052161
BA
27 const step = (ex-sx) / Math.abs(ex-sx);
28 let res = [{
29 x: sx + step,
a37076f1 30 y: sy
c6052161
BA
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;
a37076f1
BA
40 }
41 return undefined; //default
42 }
43
44 getPotentialMovesFrom([x,y])
45 {
46 switch (this.getPiece(x,y))
47 {
0b7d99ec 48 case V.MARSHALL:
a37076f1 49 return this.getPotentialMarshallMoves([x,y]);
0b7d99ec 50 case V.CARDINAL:
a37076f1
BA
51 return this.getPotentialCardinalMoves([x,y]);
52 default:
53 return super.getPotentialMovesFrom([x,y])
54 }
55 }
56
c6052161
BA
57 // Special pawn rules: promotions to captured friendly pieces,
58 // optional on ranks 8-9 and mandatory on rank 10.
a37076f1
BA
59 getPotentialPawnMoves([x,y])
60 {
c6052161
BA
61 const color = this.turn;
62 let moves = [];
0b7d99ec 63 const [sizeX,sizeY] = [V.size.x,V.size.y];
c6052161 64 const shift = (color == "w" ? -1 : 1);
cf130369
BA
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]);
c6052161
BA
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]));
8a196305 78 if (x == startRanks[0] && this.board[x+3*shift][y] == V.EMPTY)
c6052161
BA
79 {
80 // 3-squares jump
81 moves.push(this.getBasicMove([x,y], [x+3*shift,y]));
82 }
83 }
84 }
85 // Captures
92342261
BA
86 if (y>0 && this.canTake([x,y], [x+shift,y-1])
87 && this.board[x+shift][y-1] != V.EMPTY)
88 {
c6052161 89 moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
92342261
BA
90 }
91 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
92 && this.board[x+shift][y+1] != V.EMPTY)
93 {
c6052161 94 moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
92342261 95 }
c6052161
BA
96 }
97
98 if (lastRanks.includes(x+shift))
99 {
100 // Promotion
8a196305 101 let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN,V.MARSHALL,V.CARDINAL];
c6052161
BA
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
92342261
BA
109 if (y>0 && this.canTake([x,y], [x+shift,y-1])
110 && this.board[x+shift][y-1] != V.EMPTY)
111 {
c6052161 112 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
92342261
BA
113 }
114 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
115 && this.board[x+shift][y+1] != V.EMPTY)
116 {
c6052161 117 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
92342261 118 }
c6052161
BA
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;
a37076f1
BA
146 }
147
8a196305
BA
148 // TODO: different castle?
149
a37076f1
BA
150 getPotentialMarshallMoves(sq)
151 {
a37076f1
BA
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 {
a37076f1
BA
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 {
c6052161 164 return super.isAttacked(sq, colors)
a37076f1
BA
165 || this.isAttackedByMarshall(sq, colors)
166 || this.isAttackedByCardinal(sq, colors);
167 }
168
169 isAttackedByMarshall(sq, colors)
170 {
a37076f1 171 return this.isAttackedBySlideNJump(sq, colors, V.MARSHALL, V.steps[V.ROOK])
92342261
BA
172 || this.isAttackedBySlideNJump(
173 sq, colors, V.MARSHALL, V.steps[V.KNIGHT], "oneStep");
a37076f1
BA
174 }
175
176 isAttackedByCardinal(sq, colors)
177 {
a37076f1 178 return this.isAttackedBySlideNJump(sq, colors, V.CARDINAL, V.steps[V.BISHOP])
92342261
BA
179 || this.isAttackedBySlideNJump(
180 sq, colors, V.CARDINAL, V.steps[V.KNIGHT], "oneStep");
a37076f1
BA
181 }
182
a6abf094 183 updateVariables(move)
c6052161 184 {
a6abf094 185 super.updateVariables(move);
0b7d99ec 186 if (move.vanish.length==2 && move.appear.length==1 && move.vanish[1].p != V.PAWN)
c6052161
BA
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
a6abf094 196 unupdateVariables(move)
c6052161 197 {
a6abf094 198 super.unupdateVariables(move);
0b7d99ec 199 if (move.vanish.length==2 && move.appear.length==1 && move.vanish[1].p != V.PAWN)
c6052161 200 {
c6052161
BA
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
a37076f1
BA
206 static get VALUES() {
207 return Object.assign(
208 ChessRules.VALUES,
209 {'c': 5, 'm': 7} //experimental
210 );
211 }
212
3c09dc49
BA
213 static get SEARCH_DEPTH() { return 2; }
214
c6052161 215 // TODO: this function could be generalized and shared better
a37076f1
BA
216 static GenRandInitFen()
217 {
92342261 218 let pieces = { "w": new Array(10), "b": new Array(10) };
c6052161 219 // Shuffle pieces on first and last rank
92342261 220 for (let c of ["w","b"])
c6052161
BA
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 }
92342261 274 let fen = pieces["b"].join("") +
c6052161 275 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
92342261 276 pieces["w"].join("").toUpperCase() +
c6052161
BA
277 " 1111";
278 return fen;
a37076f1
BA
279 }
280}