Draft of Grand chess. Not bad, but style to adjust (10%), and castle + pawn captures...
[vchess.git] / public / javascripts / variants / Grand.js
1 //https://www.chessvariants.com/large.dir/freeling.html
2 class GrandRules extends ChessRules
3 {
4 static getPpath(b)
5 {
6 const V = VariantRules;
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 [10,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) == VariantRules.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 VariantRules.MARSHALL:
49 return this.getPotentialMarshallMoves([x,y]);
50 case VariantRules.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 V = VariantRules;
64 const [sizeX,sizeY] = VariantRules.size;
65 const shift = (color == "w" ? -1 : 1);
66 const startRanks = (color == "w" ? [sizeY-2,sizeY-3] : [1,2]);
67 const lastRanks = (color == "w" ? [0,1,2] : [sizeY-1,sizeY-2,sizeY-3]);
68
69 if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRanks[0])
70 {
71 // Normal moves
72 if (this.board[x+shift][y] == V.EMPTY)
73 {
74 moves.push(this.getBasicMove([x,y], [x+shift,y]));
75 if (startRanks.includes(x) && this.board[x+2*shift][y] == V.EMPTY)
76 {
77 // Two squares jump
78 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
79 if (x == startRanks[0] && this.board[x+2*shift][y] == V.EMPTY)
80 {
81 // 3-squares jump
82 moves.push(this.getBasicMove([x,y], [x+3*shift,y]));
83 }
84 }
85 }
86 // Captures
87 if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
88 moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
89 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
90 moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
91 }
92
93 if (lastRanks.includes(x+shift))
94 {
95 // Promotion
96 let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
97 promotionPieces.forEach(p => {
98 if (!this.captures[color][p] || this.captures[color][p]==0)
99 return;
100 // Normal move
101 if (this.board[x+shift][y] == V.EMPTY)
102 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
103 // Captures
104 if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
105 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
106 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
107 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
108 });
109 }
110
111 // En passant
112 const Lep = this.epSquares.length;
113 const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
114 if (!!epSquare)
115 {
116 for (let epsq of epSquare)
117 {
118 // TODO: some redundant checks
119 if (epsq.x == x+shift && Math.abs(epsq.y - y) == 1)
120 {
121 let epStep = epsq.y - y;
122 var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
123 enpassantMove.vanish.push({
124 x: x,
125 y: y+epStep,
126 p: 'p',
127 c: this.getColor(x,y+epStep)
128 });
129 moves.push(enpassantMove);
130 }
131 }
132 }
133
134 return moves;
135 }
136
137 getPotentialMarshallMoves(sq)
138 {
139 const V = VariantRules;
140 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat(
141 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"));
142 }
143
144 getPotentialCardinalMoves(sq)
145 {
146 const V = VariantRules;
147 return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat(
148 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"));
149 }
150
151 isAttacked(sq, colors)
152 {
153 return super.isAttacked(sq, colors)
154 || this.isAttackedByMarshall(sq, colors)
155 || this.isAttackedByCardinal(sq, colors);
156 }
157
158 isAttackedByMarshall(sq, colors)
159 {
160 const V = VariantRules;
161 return this.isAttackedBySlideNJump(sq, colors, V.MARSHALL, V.steps[V.ROOK])
162 || this.isAttackedBySlideNJump(sq, colors, V.MARSHALL, V.steps[V.KNIGHT], "oneStep");
163 }
164
165 isAttackedByCardinal(sq, colors)
166 {
167 const V = VariantRules;
168 return this.isAttackedBySlideNJump(sq, colors, V.CARDINAL, V.steps[V.BISHOP])
169 || this.isAttackedBySlideNJump(sq, colors, V.CARDINAL, V.steps[V.KNIGHT], "oneStep");
170 }
171
172 play(move, ingame)
173 {
174 super.play(move, ingame);
175 if (move.vanish.length==2 && move.appear.length==1
176 && move.vanish[1].p != VariantRules.PAWN)
177 {
178 // Capture: update this.captures
179 if (!this.captures[move.vanish[1].c][move.vanish[1].p])
180 this.captures[move.vanish[1].c][move.vanish[1].p] = 1;
181 else
182 this.captures[move.vanish[1].c][move.vanish[1].p]++;
183 }
184 }
185
186 undo(move)
187 {
188 super.undo(move);
189 if (move.vanish.length==2 && move.appear.length==1
190 && move.vanish[1].p != VariantRules.PAWN)
191 {
192 // Capture: update this.captures
193 this.captures[move.vanish[1].c][move.vanish[1].p] =
194 Math.max(0, this.captures[move.vanish[1].c][move.vanish[1].p]-1);
195 }
196 }
197
198 static get VALUES() {
199 return Object.assign(
200 ChessRules.VALUES,
201 {'c': 5, 'm': 7} //experimental
202 );
203 }
204
205 // TODO: this function could be generalized and shared better
206 static GenRandInitFen()
207 {
208 let pieces = [new Array(10), new Array(10)];
209 // Shuffle pieces on first and last rank
210 for (let c = 0; c <= 1; c++)
211 {
212 let positions = _.range(10);
213
214 // Get random squares for bishops
215 let randIndex = 2 * _.random(4);
216 let bishop1Pos = positions[randIndex];
217 // The second bishop must be on a square of different color
218 let randIndex_tmp = 2 * _.random(4) + 1;
219 let bishop2Pos = positions[randIndex_tmp];
220 // Remove chosen squares
221 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
222 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
223
224 // Get random squares for knights
225 randIndex = _.random(7);
226 let knight1Pos = positions[randIndex];
227 positions.splice(randIndex, 1);
228 randIndex = _.random(6);
229 let knight2Pos = positions[randIndex];
230 positions.splice(randIndex, 1);
231
232 // Get random square for queen
233 randIndex = _.random(5);
234 let queenPos = positions[randIndex];
235 positions.splice(randIndex, 1);
236
237 // ...random square for marshall
238 randIndex = _.random(4);
239 let marshallPos = positions[randIndex];
240 positions.splice(randIndex, 1);
241
242 // ...random square for cardinal
243 randIndex = _.random(3);
244 let cardinalPos = positions[randIndex];
245 positions.splice(randIndex, 1);
246
247 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
248 let rook1Pos = positions[0];
249 let kingPos = positions[1];
250 let rook2Pos = positions[2];
251
252 // Finally put the shuffled pieces in the board array
253 pieces[c][rook1Pos] = 'r';
254 pieces[c][knight1Pos] = 'n';
255 pieces[c][bishop1Pos] = 'b';
256 pieces[c][queenPos] = 'q';
257 pieces[c][marshallPos] = 'm';
258 pieces[c][cardinalPos] = 'c';
259 pieces[c][kingPos] = 'k';
260 pieces[c][bishop2Pos] = 'b';
261 pieces[c][knight2Pos] = 'n';
262 pieces[c][rook2Pos] = 'r';
263 }
264 let fen = pieces[0].join("") +
265 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
266 pieces[1].join("").toUpperCase() +
267 " 1111";
268 return fen;
269 }
270 }