Avoid score redundancy in PGN
[vchess.git] / public / javascripts / variants / Wildebeest.js
CommitLineData
a37076f1 1//https://www.chessvariants.com/large.dir/wildebeest.html
8a196305 2class WildebeestRules extends ChessRules
a37076f1
BA
3{
4 static getPpath(b)
5 {
6 const V = VariantRules;
7 return ([V.CAMEL,V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
8 }
9
8a196305
BA
10 static get size() { return [10,11]; }
11
a37076f1
BA
12 static get CAMEL() { return 'c'; }
13 static get WILDEBEEST() { return 'w'; }
14
15 static get steps() {
16 return Object.assign(
17 ChessRules.steps, //add camel moves:
18 {'c': [ [-3,-1],[-3,1],[-1,-3],[-1,3],[1,-3],[1,3],[3,-1],[3,1] ]}
19 );
20 }
21
8a196305 22 // En-passant after 2-sq or 3-sq jumps
a37076f1
BA
23 getEpSquare(move)
24 {
25 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
8a196305 26 if (this.getPiece(sx,sy) == VariantRules.PAWN && Math.abs(sx - ex) >= 2)
a37076f1 27 {
8a196305
BA
28 const step = (ex-sx) / Math.abs(ex-sx);
29 let res = [{
30 x: sx + step,
a37076f1 31 y: sy
8a196305
BA
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;
a37076f1
BA
41 }
42 return undefined; //default
43 }
44
45 getPotentialMovesFrom([x,y])
46 {
47 switch (this.getPiece(x,y))
48 {
49 case VariantRules.CAMEL:
50 return this.getPotentialCamelMoves([x,y]);
51 case VariantRules.WILDEBEEST:
52 return this.getPotentialWildebeestMoves([x,y]);
53 default:
54 return super.getPotentialMovesFrom([x,y])
55 }
56 }
57
8a196305 58 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
a37076f1
BA
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);
cf130369
BA
66 const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]);
67 const lastRank = (color == "w" ? 0 : sizeX-1);
a37076f1
BA
68
69 if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
70 {
71 // Normal moves
72 if (this.board[x+shift][y] == V.EMPTY)
73 {
74 moves.push(this.getBasicMove([x,y], [x+shift,y]));
8a196305 75 if (startRanks.includes(x) && this.board[x+2*shift][y] == V.EMPTY)
a37076f1
BA
76 {
77 // Two squares jump
78 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
8a196305
BA
79 if (x == startRanks[0] && this.board[x+3*shift][y] == V.EMPTY)
80 {
81 // 3-squares jump
82 moves.push(this.getBasicMove([x,y], [x+3*shift,y]));
83 }
a37076f1
BA
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 (x+shift == lastRank)
94 {
95 // Promotion
8a196305 96 let promotionPieces = [V.QUEEN,V.WILDEBEEST];
a37076f1
BA
97 promotionPieces.forEach(p => {
98 // Normal move
99 if (this.board[x+shift][y] == V.EMPTY)
100 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
101 // Captures
102 if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
103 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
104 if (y<sizeY-1 && 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 });
107 }
108
109 // En passant
110 const Lep = this.epSquares.length;
111 const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
8a196305 112 if (!!epSquare)
a37076f1 113 {
8a196305
BA
114 for (let epsq of epSquare)
115 {
116 // TODO: some redundant checks
117 if (epsq.x == x+shift && Math.abs(epsq.y - y) == 1)
118 {
119 let epStep = epsq.y - y;
120 var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
121 enpassantMove.vanish.push({
122 x: x,
123 y: y+epStep,
124 p: 'p',
125 c: this.getColor(x,y+epStep)
126 });
127 moves.push(enpassantMove);
128 }
129 }
a37076f1
BA
130 }
131
132 return moves;
133 }
134
8a196305
BA
135 // TODO: wildebeest castle
136
a37076f1
BA
137 getPotentialCamelMoves(sq)
138 {
139 return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.CAMEL], "oneStep");
140 }
141
142 getPotentialWildebeestMoves(sq)
143 {
144 const V = VariantRules;
8a196305 145 return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
a37076f1
BA
146 }
147
a37076f1
BA
148 isAttacked(sq, colors)
149 {
8a196305 150 return super.isAttacked(sq, colors)
a37076f1
BA
151 || this.isAttackedByCamel(sq, colors)
152 || this.isAttackedByWildebeest(sq, colors);
153 }
154
155 isAttackedByCamel(sq, colors)
156 {
157 return this.isAttackedBySlideNJump(sq, colors,
cf130369 158 VariantRules.CAMEL, VariantRules.steps[VariantRules.CAMEL], "oneStep");
a37076f1
BA
159 }
160
161 isAttackedByWildebeest(sq, colors)
162 {
163 const V = VariantRules;
164 return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST,
cf130369 165 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
a37076f1
BA
166 }
167
cf130369
BA
168 checkGameEnd()
169 {
170 // No valid move: game is lost (stalemate is a win)
171 return this.turn == "w" ? "0-1" : "1-0";
172 }
efb20746 173
a37076f1
BA
174 static get VALUES() {
175 return Object.assign(
176 ChessRules.VALUES,
177 {'c': 3, 'w': 7} //experimental
178 );
179 }
180
3c09dc49
BA
181 static get SEARCH_DEPTH() { return 2; }
182
a37076f1
BA
183 static GenRandInitFen()
184 {
8a196305 185 let pieces = [new Array(10), new Array(10)];
a37076f1
BA
186 // Shuffle pieces on first and last rank
187 for (let c = 0; c <= 1; c++)
188 {
8a196305 189 let positions = _.range(11);
a37076f1 190
dca02599
BA
191 // Get random squares for bishops + camels (different colors)
192 let randIndexes = _.sample(_.range(6), 2).map(i => { return 2*i; });
193 let bishop1Pos = positions[randIndexes[0]];
194 let camel1Pos = positions[randIndexes[1]];
195 // The second bishop (camel) must be on a square of different color
196 let randIndexes_tmp = _.sample(_.range(5), 2).map(i => { return 2*i+1; });
197 let bishop2Pos = positions[randIndexes_tmp[0]];
198 let camel2Pos = positions[randIndexes_tmp[1]];
a37076f1 199 // Remove chosen squares
dca02599
BA
200 for (let idx of randIndexes.concat(randIndexes_tmp).sort((a,b) => { return b-a; }))
201 positions.splice(idx, 1);
a37076f1
BA
202
203 // Get random squares for knights
dca02599 204 let randIndex = _.random(6);
a37076f1
BA
205 let knight1Pos = positions[randIndex];
206 positions.splice(randIndex, 1);
dca02599 207 randIndex = _.random(5);
a37076f1
BA
208 let knight2Pos = positions[randIndex];
209 positions.splice(randIndex, 1);
210
211 // Get random square for queen
8a196305 212 randIndex = _.random(4);
dca02599 213 let queenPos = positions[randIndex];
8a196305
BA
214 positions.splice(randIndex, 1);
215
216 // ...random square for wildebeest
217 randIndex = _.random(3);
218 let wildebeestPos = positions[randIndex];
219 positions.splice(randIndex, 1);
220
a37076f1
BA
221 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
222 let rook1Pos = positions[0];
223 let kingPos = positions[1];
224 let rook2Pos = positions[2];
225
226 // Finally put the shuffled pieces in the board array
227 pieces[c][rook1Pos] = 'r';
228 pieces[c][knight1Pos] = 'n';
229 pieces[c][bishop1Pos] = 'b';
230 pieces[c][queenPos] = 'q';
8a196305
BA
231 pieces[c][camel1Pos] = 'c';
232 pieces[c][camel2Pos] = 'c';
233 pieces[c][wildebeestPos] = 'w';
a37076f1
BA
234 pieces[c][kingPos] = 'k';
235 pieces[c][bishop2Pos] = 'b';
236 pieces[c][knight2Pos] = 'n';
237 pieces[c][rook2Pos] = 'r';
238 }
239 let fen = pieces[0].join("") +
8a196305 240 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
a37076f1 241 pieces[1].join("").toUpperCase() +
8a196305 242 " 1111";
a37076f1
BA
243 return fen;
244 }
245}