Commit | Line | Data |
---|---|---|
4f7723a1 BA |
1 | class 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 == "-") | |
51882959 | 46 | this.epSquares = [ [undefined] ]; |
6e62b1c7 BA |
47 | else |
48 | { | |
49 | let res = []; | |
50 | const squares = parsedFen.enpassant.split(","); | |
51 | for (let sq of squares) | |
52 | res.push(V.SquareToCoords(sq)); | |
6e62b1c7 BA |
53 | this.epSquares = [ res ]; |
54 | } | |
55 | this.scanKingsRooks(fen); | |
56 | // Extract subTurn from turn indicator: "w" (first move), or | |
57 | // "w1" or "w2" white subturn 1 or 2, and same for black | |
58 | const fullTurn = V.ParseFen(fen).turn; | |
59 | this.startAtFirstMove = (fullTurn == "w"); | |
60 | this.turn = fullTurn[0]; | |
61 | this.subTurn = (fullTurn[1] || 1); | |
62 | } | |
63 | ||
64 | getPotentialPawnMoves([x,y]) | |
65 | { | |
66 | const color = this.turn; | |
67 | let moves = []; | |
68 | const [sizeX,sizeY] = [V.size.x,V.size.y]; | |
69 | const shiftX = (color == "w" ? -1 : 1); | |
70 | const firstRank = (color == 'w' ? sizeX-1 : 0); | |
71 | const startRank = (color == "w" ? sizeX-2 : 1); | |
72 | const lastRank = (color == "w" ? 0 : sizeX-1); | |
73 | const pawnColor = this.getColor(x,y); //can be different for checkered | |
69f3d801 BA |
74 | const finalPieces = x + shiftX == lastRank |
75 | ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] | |
76 | : [V.PAWN]; | |
6e62b1c7 | 77 | |
69f3d801 BA |
78 | // One square forward |
79 | if (this.board[x+shiftX][y] == V.EMPTY) | |
6e62b1c7 | 80 | { |
69f3d801 | 81 | for (let piece of finalPieces) |
6e62b1c7 | 82 | { |
69f3d801 BA |
83 | moves.push(this.getBasicMove([x,y], [x+shiftX,y], |
84 | {c:pawnColor,p:piece})); | |
6e62b1c7 | 85 | } |
69f3d801 BA |
86 | // Next condition because pawns on 1st rank can generally jump |
87 | if ([startRank,firstRank].includes(x) | |
88 | && this.board[x+2*shiftX][y] == V.EMPTY) | |
6e62b1c7 | 89 | { |
69f3d801 BA |
90 | // Two squares jump |
91 | moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); | |
92 | } | |
93 | } | |
94 | // Captures | |
95 | for (let shiftY of [-1,1]) | |
96 | { | |
97 | if (y + shiftY >= 0 && y + shiftY < sizeY | |
98 | && this.board[x+shiftX][y+shiftY] != V.EMPTY | |
99 | && this.canTake([x,y], [x+shiftX,y+shiftY])) | |
100 | { | |
101 | for (let piece of finalPieces) | |
6e62b1c7 | 102 | { |
69f3d801 BA |
103 | moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], |
104 | {c:pawnColor,p:piece})); | |
6e62b1c7 BA |
105 | } |
106 | } | |
107 | } | |
108 | ||
109 | // En passant: always OK if subturn 1, | |
110 | // OK on subturn 2 only if enPassant was played at subturn 1 | |
111 | // (and if there are two e.p. squares available). | |
112 | const Lep = this.epSquares.length; | |
113 | const epSquares = this.epSquares[Lep-1]; //always at least one element | |
114 | let epSqs = []; | |
115 | epSquares.forEach(sq => { | |
116 | if (!!sq) | |
117 | epSqs.push(sq); | |
118 | }); | |
119 | if (epSqs.length == 0) | |
120 | return moves; | |
121 | for (let sq of epSqs) | |
122 | { | |
123 | if (this.subTurn == 1 || (epSqs.length == 2 && | |
124 | // Was this en-passant capture already played at subturn 1 ? | |
69f3d801 | 125 | // (Or maybe the opponent filled the en-passant square with a piece) |
6e62b1c7 BA |
126 | this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY)) |
127 | { | |
128 | if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1) | |
129 | { | |
130 | let epMove = this.getBasicMove([x,y], [sq.x,sq.y]); | |
131 | epMove.vanish.push({ | |
132 | x: x, | |
133 | y: sq.y, | |
134 | p: 'p', | |
135 | c: this.getColor(x,sq.y) | |
136 | }); | |
137 | moves.push(epMove); | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | return moves; | |
143 | } | |
144 | ||
145 | play(move, ingame) | |
146 | { | |
6e62b1c7 | 147 | if (!!ingame) |
1b61a94d | 148 | { |
6e62b1c7 | 149 | move.notation = [this.getNotation(move), this.getLongNotation(move)]; |
1b61a94d BA |
150 | // In this special case, we also need the "move color": |
151 | move.color = this.turn; | |
152 | } | |
6e62b1c7 | 153 | move.flags = JSON.stringify(this.aggregateFlags()); |
6e62b1c7 | 154 | V.PlayOnBoard(this.board, move); |
51882959 | 155 | const epSq = this.getEpSquare(move); |
6e62b1c7 | 156 | if (this.startAtFirstMove && this.moves.length == 0) |
51882959 | 157 | { |
6e62b1c7 | 158 | this.turn = "b"; |
51882959 BA |
159 | this.epSquares.push([epSq]); |
160 | } | |
6e62b1c7 BA |
161 | // Does this move give check on subturn 1? If yes, skip subturn 2 |
162 | else if (this.subTurn==1 && this.underCheck(this.getOppCol(this.turn))) | |
163 | { | |
6e62b1c7 | 164 | this.turn = this.getOppCol(this.turn); |
51882959 | 165 | this.epSquares.push([epSq]); |
6e62b1c7 BA |
166 | move.checkOnSubturn1 = true; |
167 | } | |
168 | else | |
169 | { | |
170 | if (this.subTurn == 2) | |
51882959 | 171 | { |
6e62b1c7 | 172 | this.turn = this.getOppCol(this.turn); |
51882959 BA |
173 | let lastEpsq = this.epSquares[this.epSquares.length-1]; |
174 | lastEpsq.push(epSq); | |
175 | } | |
176 | else | |
177 | this.epSquares.push([epSq]); | |
6e62b1c7 BA |
178 | this.subTurn = 3 - this.subTurn; |
179 | } | |
180 | this.moves.push(move); | |
181 | this.updateVariables(move); | |
182 | if (!!ingame) | |
183 | move.hash = hex_md5(this.getFen()); | |
6e62b1c7 BA |
184 | } |
185 | ||
186 | undo(move) | |
187 | { | |
188 | this.disaggregateFlags(JSON.parse(move.flags)); | |
6e62b1c7 BA |
189 | V.UndoOnBoard(this.board, move); |
190 | if (this.startAtFirstMove && this.moves.length == 1) | |
51882959 | 191 | { |
6e62b1c7 | 192 | this.turn = "w"; |
51882959 BA |
193 | this.epSquares.pop(); |
194 | } | |
6e62b1c7 BA |
195 | else if (move.checkOnSubturn1) |
196 | { | |
197 | this.turn = this.getOppCol(this.turn); | |
198 | this.subTurn = 1; | |
51882959 | 199 | this.epSquares.pop(); |
6e62b1c7 BA |
200 | } |
201 | else | |
202 | { | |
203 | if (this.subTurn == 1) | |
51882959 | 204 | { |
6e62b1c7 | 205 | this.turn = this.getOppCol(this.turn); |
51882959 BA |
206 | let lastEpsq = this.epSquares[this.epSquares.length-1]; |
207 | lastEpsq.pop(); | |
208 | } | |
209 | else | |
210 | this.epSquares.pop(); | |
6e62b1c7 BA |
211 | this.subTurn = 3 - this.subTurn; |
212 | } | |
213 | this.moves.pop(); | |
214 | this.unupdateVariables(move); | |
6e62b1c7 BA |
215 | } |
216 | ||
217 | // NOTE: GenRandInitFen() is OK, | |
218 | // since at first move turn indicator is just "w" | |
219 | ||
220 | // No alpha-beta here, just adapted min-max at depth 2(+1) | |
221 | getComputerMove() | |
222 | { | |
78bab51e BA |
223 | if (this.subTurn == 2) |
224 | return null; //TODO: imperfect interface setup | |
225 | ||
6e62b1c7 BA |
226 | const maxeval = V.INFINITY; |
227 | const color = this.turn; | |
228 | const oppCol = this.getOppCol(this.turn); | |
51882959 | 229 | |
6e62b1c7 BA |
230 | // Search best (half) move for opponent turn |
231 | const getBestMoveEval = () => { | |
51882959 | 232 | const turnBefore = this.turn + this.subTurn; |
6e62b1c7 BA |
233 | let moves = this.getAllValidMoves(); |
234 | if (moves.length == 0) | |
235 | { | |
236 | const score = this.checkGameEnd(); | |
237 | if (score == "1/2") | |
238 | return 0; | |
239 | return maxeval * (score == "1-0" ? 1 : -1); | |
240 | } | |
241 | let res = (oppCol == "w" ? -maxeval : maxeval); | |
242 | for (let m of moves) | |
243 | { | |
244 | this.play(m); | |
51882959 BA |
245 | // Now turn is oppCol,2 if m doesn't give check |
246 | // Otherwise it's color,1. In both cases the next test makes sense | |
6e62b1c7 BA |
247 | if (!this.atLeastOneMove()) |
248 | { | |
249 | const score = this.checkGameEnd(); | |
250 | if (score == "1/2") | |
251 | res = (oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0)); | |
252 | else | |
253 | { | |
254 | // Found a mate | |
6e62b1c7 BA |
255 | this.undo(m); |
256 | return maxeval * (score == "1-0" ? 1 : -1); | |
257 | } | |
258 | } | |
259 | const evalPos = this.evalPosition(); | |
260 | res = (oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos)); | |
6e62b1c7 BA |
261 | this.undo(m); |
262 | } | |
263 | return res; | |
264 | }; | |
265 | ||
266 | let moves11 = this.getAllValidMoves(); | |
267 | let doubleMoves = []; | |
268 | // Rank moves using a min-max at depth 2 | |
269 | for (let i=0; i<moves11.length; i++) | |
270 | { | |
6e62b1c7 BA |
271 | this.play(moves11[i]); |
272 | if (this.turn != color) | |
273 | { | |
274 | // We gave check with last move: search the best opponent move | |
275 | doubleMoves.push({moves:[moves11[i]], eval:getBestMoveEval()}); | |
276 | } | |
277 | else | |
278 | { | |
279 | let moves12 = this.getAllValidMoves(); | |
280 | for (let j=0; j<moves12.length; j++) | |
281 | { | |
282 | this.play(moves12[j]); | |
283 | doubleMoves.push({ | |
284 | moves:[moves11[i],moves12[j]], | |
285 | eval:getBestMoveEval()}); | |
286 | this.undo(moves12[j]); | |
287 | } | |
288 | } | |
289 | this.undo(moves11[i]); | |
290 | } | |
291 | ||
292 | doubleMoves.sort( (a,b) => { | |
293 | return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); | |
294 | let candidates = [0]; //indices of candidates moves | |
295 | for (let i=1; | |
296 | i<doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval; | |
297 | i++) | |
298 | { | |
299 | candidates.push(i); | |
300 | } | |
78bab51e | 301 | |
6e62b1c7 BA |
302 | const selected = doubleMoves[_.sample(candidates, 1)].moves; |
303 | if (selected.length == 1) | |
304 | return selected[0]; | |
305 | return selected; | |
306 | } | |
1b61a94d BA |
307 | |
308 | getPGN(mycolor, score, fenStart, mode) | |
309 | { | |
310 | let pgn = ""; | |
311 | pgn += '[Site "vchess.club"]<br>'; | |
312 | const opponent = mode=="human" ? "Anonymous" : "Computer"; | |
313 | pgn += '[Variant "' + variant + '"]<br>'; | |
314 | pgn += '[Date "' + getDate(new Date()) + '"]<br>'; | |
315 | pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>'; | |
316 | pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>'; | |
317 | pgn += '[FenStart "' + fenStart + '"]<br>'; | |
318 | pgn += '[Fen "' + this.getFen() + '"]<br>'; | |
319 | pgn += '[Result "' + score + '"]<br><br>'; | |
320 | ||
321 | let counter = 1; | |
322 | let i = 0; | |
323 | while (i < this.moves.length) | |
324 | { | |
325 | pgn += (counter++) + "."; | |
326 | for (let color of ["w","b"]) | |
327 | { | |
328 | let move = ""; | |
329 | while (i < this.moves.length && this.moves[i].color == color) | |
330 | move += this.moves[i++].notation[0] + ","; | |
331 | move = move.slice(0,-1); //remove last comma | |
332 | pgn += move + (i < this.moves.length-1 ? " " : ""); | |
333 | } | |
334 | } | |
335 | pgn += "<br><br>"; | |
336 | ||
337 | // "Complete moves" PGN (helping in ambiguous cases) | |
338 | counter = 1; | |
339 | i = 0; | |
340 | while (i < this.moves.length) | |
341 | { | |
342 | pgn += (counter++) + "."; | |
343 | for (let color of ["w","b"]) | |
344 | { | |
345 | let move = ""; | |
346 | while (i < this.moves.length && this.moves[i].color == color) | |
347 | move += this.moves[i++].notation[1] + ","; | |
348 | move = move.slice(0,-1); //remove last comma | |
349 | pgn += move + (i < this.moves.length-1 ? " " : ""); | |
350 | } | |
351 | } | |
352 | ||
353 | return pgn; | |
354 | } | |
4f7723a1 BA |
355 | } |
356 | ||
357 | const VariantRules = MarseilleRules; |