MarseilleRules OK
[vchess.git] / public / javascripts / variants / Marseille.js
CommitLineData
4f7723a1
BA
1class MarseilleRules extends ChessRules
2{
6e62b1c7
BA
3 static IsGoodEnpassant(enpassant)
4 {
5 if (enpassant != "-")
6 {
7 const squares = enpassant.split(",");
8 if (squares.length > 2)
9 return false;
10 for (let sq of squares)
11 {
12 const ep = V.SquareToCoords(sq);
13 if (isNaN(ep.x) || !V.OnBoard(ep))
14 return false;
15 }
16 }
17 return true;
18 }
19
20 getTurnFen()
21 {
22 if (this.startAtFirstMove && this.moves.length==0)
23 return "w";
24 return this.turn + this.subTurn;
25 }
26
27 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
28 getEnpassantFen()
29 {
30 const L = this.epSquares.length;
31 if (this.epSquares[L-1].every(epsq => epsq === undefined))
32 return "-"; //no en-passant
33 let res = "";
34 this.epSquares[L-1].forEach(epsq => {
35 if (!!epsq)
36 res += V.CoordsToSquare(epsq) + ",";
37 });
38 return res.slice(0,-1); //remove last comma
39 }
40
41 setOtherVariables(fen)
42 {
43 const parsedFen = V.ParseFen(fen);
44 this.setFlags(parsedFen.flags);
45 if (parsedFen.enpassant == "-")
46 this.epSquares = [ [undefined,undefined] ];
47 else
48 {
49 let res = [];
50 const squares = parsedFen.enpassant.split(",");
51 for (let sq of squares)
52 res.push(V.SquareToCoords(sq));
53 if (res.length == 1)
54 res.push(undefined); //always 2 slots in epSquares[i]
55 this.epSquares = [ res ];
56 }
57 this.scanKingsRooks(fen);
58 // Extract subTurn from turn indicator: "w" (first move), or
59 // "w1" or "w2" white subturn 1 or 2, and same for black
60 const fullTurn = V.ParseFen(fen).turn;
61 this.startAtFirstMove = (fullTurn == "w");
62 this.turn = fullTurn[0];
63 this.subTurn = (fullTurn[1] || 1);
64 }
65
66 getPotentialPawnMoves([x,y])
67 {
68 const color = this.turn;
69 let moves = [];
70 const [sizeX,sizeY] = [V.size.x,V.size.y];
71 const shiftX = (color == "w" ? -1 : 1);
72 const firstRank = (color == 'w' ? sizeX-1 : 0);
73 const startRank = (color == "w" ? sizeX-2 : 1);
74 const lastRank = (color == "w" ? 0 : sizeX-1);
75 const pawnColor = this.getColor(x,y); //can be different for checkered
76
77 if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true
78 {
79 const finalPieces = x + shiftX == lastRank
80 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
81 : [V.PAWN]
82 // One square forward
83 if (this.board[x+shiftX][y] == V.EMPTY)
84 {
85 for (let piece of finalPieces)
86 {
87 moves.push(this.getBasicMove([x,y], [x+shiftX,y],
88 {c:pawnColor,p:piece}));
89 }
90 // Next condition because pawns on 1st rank can generally jump
91 if ([startRank,firstRank].includes(x)
92 && this.board[x+2*shiftX][y] == V.EMPTY)
93 {
94 // Two squares jump
95 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
96 }
97 }
98 // Captures
99 for (let shiftY of [-1,1])
100 {
101 if (y + shiftY >= 0 && y + shiftY < sizeY
102 && this.board[x+shiftX][y+shiftY] != V.EMPTY
103 && this.canTake([x,y], [x+shiftX,y+shiftY]))
104 {
105 for (let piece of finalPieces)
106 {
107 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
108 {c:pawnColor,p:piece}));
109 }
110 }
111 }
112 }
113
114 // En passant: always OK if subturn 1,
115 // OK on subturn 2 only if enPassant was played at subturn 1
116 // (and if there are two e.p. squares available).
117 const Lep = this.epSquares.length;
118 const epSquares = this.epSquares[Lep-1]; //always at least one element
119 let epSqs = [];
120 epSquares.forEach(sq => {
121 if (!!sq)
122 epSqs.push(sq);
123 });
124 if (epSqs.length == 0)
125 return moves;
126 for (let sq of epSqs)
127 {
128 if (this.subTurn == 1 || (epSqs.length == 2 &&
129 // Was this en-passant capture already played at subturn 1 ?
130 this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY))
131 {
132 if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1)
133 {
134 let epMove = this.getBasicMove([x,y], [sq.x,sq.y]);
135 epMove.vanish.push({
136 x: x,
137 y: sq.y,
138 p: 'p',
139 c: this.getColor(x,sq.y)
140 });
141 moves.push(epMove);
142 }
143 }
144 }
145
146 return moves;
147 }
148
149 play(move, ingame)
150 {
151// console.log("play " + this.getNotation(move));
152// console.log(this.turn + " "+ this.subTurn);
153 if (!!ingame)
154 move.notation = [this.getNotation(move), this.getLongNotation(move)];
155 move.flags = JSON.stringify(this.aggregateFlags());
156 let lastEpsq = this.epSquares[this.epSquares.length-1];
157 const epSq = this.getEpSquare(move);
158 if (lastEpsq.length == 1)
159 lastEpsq.push(epSq);
160 else
161 {
162 // New turn
163 let newEpsqs = [epSq];
164 if (this.startAtFirstMove && this.moves.length == 0)
165 newEpsqs.push(undefined); //at first move, to force length==2 (TODO)
166 this.epSquares.push(newEpsqs);
167 }
168 V.PlayOnBoard(this.board, move);
169 if (this.startAtFirstMove && this.moves.length == 0)
170 this.turn = "b";
171 // Does this move give check on subturn 1? If yes, skip subturn 2
172 else if (this.subTurn==1 && this.underCheck(this.getOppCol(this.turn)))
173 {
174 this.epSquares[this.epSquares.length-1].push(undefined);
175 this.turn = this.getOppCol(this.turn);
176 move.checkOnSubturn1 = true;
177 }
178 else
179 {
180 if (this.subTurn == 2)
181 this.turn = this.getOppCol(this.turn);
182 this.subTurn = 3 - this.subTurn;
183 }
184 this.moves.push(move);
185 this.updateVariables(move);
186 if (!!ingame)
187 move.hash = hex_md5(this.getFen());
188 //console.log(move.checkOnSubturn1 + " " +this.turn + " "+ this.subTurn);
189 }
190
191 undo(move)
192 {
193 this.disaggregateFlags(JSON.parse(move.flags));
194 let lastEpsq = this.epSquares[this.epSquares.length-1];
195 if (lastEpsq.length == 2)
196 {
197 if (!!move.checkOnSubturn1 ||
198 (this.startAtFirstMove && this.moves.length == 1))
199 {
200 this.epSquares.pop(); //remove real + artificial e.p. squares
201 }
202 else
203 lastEpsq.pop();
204 }
205 else
206 this.epSquares.pop();
207 V.UndoOnBoard(this.board, move);
208 if (this.startAtFirstMove && this.moves.length == 1)
209 this.turn = "w";
210 else if (move.checkOnSubturn1)
211 {
212 this.turn = this.getOppCol(this.turn);
213 this.subTurn = 1;
214 }
215 else
216 {
217 if (this.subTurn == 1)
218 this.turn = this.getOppCol(this.turn);
219 this.subTurn = 3 - this.subTurn;
220 }
221 this.moves.pop();
222 this.unupdateVariables(move);
223// console.log("UNDO " + this.getNotation(move));
224// console.log(this.turn + " "+ this.subTurn);
225 }
226
227 // NOTE: GenRandInitFen() is OK,
228 // since at first move turn indicator is just "w"
229
230 // No alpha-beta here, just adapted min-max at depth 2(+1)
231 getComputerMove()
232 {
78bab51e
BA
233 if (this.subTurn == 2)
234 return null; //TODO: imperfect interface setup
235
6e62b1c7
BA
236 const maxeval = V.INFINITY;
237 const color = this.turn;
238 const oppCol = this.getOppCol(this.turn);
239
240 // Search best (half) move for opponent turn
241 const getBestMoveEval = () => {
242 let moves = this.getAllValidMoves();
243 if (moves.length == 0)
244 {
245 const score = this.checkGameEnd();
246 if (score == "1/2")
247 return 0;
248 return maxeval * (score == "1-0" ? 1 : -1);
249 }
250 let res = (oppCol == "w" ? -maxeval : maxeval);
251 for (let m of moves)
252 {
253 this.play(m);
254 this.turn = color; //very artificial...
255 if (!this.atLeastOneMove())
256 {
257 const score = this.checkGameEnd();
258 if (score == "1/2")
259 res = (oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0));
260 else
261 {
262 // Found a mate
263 this.turn = oppCol;
264 this.undo(m);
265 return maxeval * (score == "1-0" ? 1 : -1);
266 }
267 }
268 const evalPos = this.evalPosition();
269 res = (oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos));
270 this.turn = oppCol;
271 this.undo(m);
272 }
273 return res;
274 };
275
276 let moves11 = this.getAllValidMoves();
277 let doubleMoves = [];
278 // Rank moves using a min-max at depth 2
279 for (let i=0; i<moves11.length; i++)
280 {
281 moves11[i].eval = (color=="w" ? -1 : 1) * maxeval;
282 this.play(moves11[i]);
283 if (this.turn != color)
284 {
285 // We gave check with last move: search the best opponent move
286 doubleMoves.push({moves:[moves11[i]], eval:getBestMoveEval()});
287 }
288 else
289 {
290 let moves12 = this.getAllValidMoves();
291 for (let j=0; j<moves12.length; j++)
292 {
293 this.play(moves12[j]);
294 doubleMoves.push({
295 moves:[moves11[i],moves12[j]],
296 eval:getBestMoveEval()});
297 this.undo(moves12[j]);
298 }
299 }
300 this.undo(moves11[i]);
301 }
302
303 doubleMoves.sort( (a,b) => {
304 return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
305 let candidates = [0]; //indices of candidates moves
306 for (let i=1;
307 i<doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
308 i++)
309 {
310 candidates.push(i);
311 }
78bab51e 312
6e62b1c7
BA
313 const selected = doubleMoves[_.sample(candidates, 1)].moves;
314 if (selected.length == 1)
315 return selected[0];
316 return selected;
317 }
4f7723a1
BA
318}
319
320const VariantRules = MarseilleRules;