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