Commit | Line | Data |
---|---|---|
829f6574 | 1 | import { ChessRules } from "@/base_rules"; |
5fde3a01 | 2 | import { randInt } from "@/utils/alea"; |
829f6574 | 3 | |
b406466b | 4 | export class Doublemove2Rules extends ChessRules { |
6808d7a1 | 5 | static IsGoodEnpassant(enpassant) { |
e71161fb BA |
6 | const squares = enpassant.split(","); |
7 | if (squares.length > 2) return false; | |
8 | for (let sq of squares) { | |
9 | if (sq != "-") { | |
dac39588 | 10 | const ep = V.SquareToCoords(sq); |
6808d7a1 | 11 | if (isNaN(ep.x) || !V.OnBoard(ep)) return false; |
dac39588 BA |
12 | } |
13 | } | |
14 | return true; | |
15 | } | |
6e62b1c7 | 16 | |
dac39588 | 17 | // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn) |
6808d7a1 | 18 | getEnpassantFen() { |
e71161fb BA |
19 | return this.epSquares[this.epSquares.length - 1].map( |
20 | epsq => epsq === undefined | |
21 | ? "-" //no en-passant | |
22 | : V.CoordsToSquare(epsq) | |
23 | ).join(","); | |
dac39588 | 24 | } |
6e62b1c7 | 25 | |
6808d7a1 | 26 | setOtherVariables(fen) { |
dac39588 BA |
27 | const parsedFen = V.ParseFen(fen); |
28 | this.setFlags(parsedFen.flags); | |
e71161fb BA |
29 | this.epSquares = [parsedFen.enpassant.split(",").map(sq => { |
30 | if (sq != "-") return V.SquareToCoords(sq); | |
31 | return undefined; | |
32 | })]; | |
3a2a7b5f | 33 | this.scanKings(fen); |
dac39588 BA |
34 | // Extract subTurn from turn indicator: "w" (first move), or |
35 | // "w1" or "w2" white subturn 1 or 2, and same for black | |
af34341d BA |
36 | this.turn = parsedFen.turn; |
37 | this.subTurn = 1; | |
dac39588 | 38 | } |
6e62b1c7 | 39 | |
32f6285e | 40 | getEnpassantCaptures([x, y], shiftX) { |
dac39588 | 41 | let moves = []; |
dac39588 BA |
42 | // En passant: always OK if subturn 1, |
43 | // OK on subturn 2 only if enPassant was played at subturn 1 | |
44 | // (and if there are two e.p. squares available). | |
45 | const Lep = this.epSquares.length; | |
6808d7a1 | 46 | const epSquares = this.epSquares[Lep - 1]; //always at least one element |
dac39588 BA |
47 | let epSqs = []; |
48 | epSquares.forEach(sq => { | |
6808d7a1 | 49 | if (sq) epSqs.push(sq); |
dac39588 | 50 | }); |
6808d7a1 | 51 | if (epSqs.length == 0) return moves; |
32f6285e | 52 | const oppCol = V.GetOppCol(this.getColor(x, y)); |
6808d7a1 BA |
53 | for (let sq of epSqs) { |
54 | if ( | |
55 | this.subTurn == 1 || | |
56 | (epSqs.length == 2 && | |
57 | // Was this en-passant capture already played at subturn 1 ? | |
58 | // (Or maybe the opponent filled the en-passant square with a piece) | |
59 | this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY) | |
60 | ) { | |
61 | if ( | |
62 | sq.x == x + shiftX && | |
63 | Math.abs(sq.y - y) == 1 && | |
dac39588 | 64 | // Add condition "enemy pawn must be present" |
6808d7a1 BA |
65 | this.getPiece(x, sq.y) == V.PAWN && |
66 | this.getColor(x, sq.y) == oppCol | |
67 | ) { | |
68 | let epMove = this.getBasicMove([x, y], [sq.x, sq.y]); | |
dac39588 BA |
69 | epMove.vanish.push({ |
70 | x: x, | |
71 | y: sq.y, | |
6808d7a1 | 72 | p: "p", |
dac39588 BA |
73 | c: oppCol |
74 | }); | |
75 | moves.push(epMove); | |
76 | } | |
77 | } | |
78 | } | |
dac39588 BA |
79 | return moves; |
80 | } | |
6e62b1c7 | 81 | |
34bfe151 | 82 | isAttacked() { |
bc0b9205 | 83 | // Goal is king capture => no checks |
b406466b BA |
84 | return false; |
85 | } | |
86 | ||
87 | filterValid(moves) { | |
bc0b9205 BA |
88 | return moves; |
89 | } | |
90 | ||
91 | getCheckSquares() { | |
92 | return []; | |
93 | } | |
94 | ||
95 | getCurrentScore() { | |
96 | const color = this.turn; | |
97 | if (this.kingPos[color][0] < 0) return (color == 'w' ? "0-1" : "1-0"); | |
98 | return "*"; | |
b406466b BA |
99 | } |
100 | ||
6808d7a1 | 101 | play(move) { |
dac39588 | 102 | move.flags = JSON.stringify(this.aggregateFlags()); |
dac39588 BA |
103 | V.PlayOnBoard(this.board, move); |
104 | const epSq = this.getEpSquare(move); | |
b406466b BA |
105 | if (this.subTurn == 2) { |
106 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; | |
107 | lastEpsq.push(epSq); | |
dac39588 | 108 | this.turn = V.GetOppCol(this.turn); |
b406466b BA |
109 | } |
110 | else { | |
dac39588 | 111 | this.epSquares.push([epSq]); |
9842aca2 | 112 | this.movesCount++; |
964eda04 BA |
113 | if ( |
114 | this.movesCount == 1 || | |
115 | // King is captured at subTurn 1? | |
116 | (move.vanish.length == 2 && move.vanish[1].p == V.KING) | |
117 | ) { | |
118 | this.turn = "b"; | |
119 | } | |
dac39588 | 120 | } |
b406466b | 121 | if (this.movesCount > 1) this.subTurn = 3 - this.subTurn; |
3a2a7b5f BA |
122 | this.postPlay(move); |
123 | } | |
124 | ||
125 | postPlay(move) { | |
b406466b | 126 | const c = move.vanish[0].c; |
3a2a7b5f BA |
127 | const piece = move.vanish[0].p; |
128 | const firstRank = c == "w" ? V.size.x - 1 : 0; | |
129 | ||
737a5daf | 130 | if (piece == V.KING) { |
bc0b9205 | 131 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; |
0d5335de | 132 | this.castleFlags[c] = [V.size.y, V.size.y]; |
3a2a7b5f BA |
133 | return; |
134 | } | |
135 | const oppCol = V.GetOppCol(c); | |
964eda04 | 136 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) { |
bc0b9205 BA |
137 | // Opponent's king is captured, game over |
138 | this.kingPos[oppCol] = [-1, -1]; | |
964eda04 BA |
139 | move.captureKing = true; //for undo |
140 | } | |
3a2a7b5f BA |
141 | const oppFirstRank = V.size.x - 1 - firstRank; |
142 | if ( | |
143 | move.start.x == firstRank && //our rook moves? | |
144 | this.castleFlags[c].includes(move.start.y) | |
145 | ) { | |
146 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
147 | this.castleFlags[c][flagIdx] = V.size.y; | |
bc0b9205 | 148 | } |
737a5daf | 149 | if ( |
3a2a7b5f BA |
150 | move.end.x == oppFirstRank && //we took opponent rook? |
151 | this.castleFlags[oppCol].includes(move.end.y) | |
152 | ) { | |
153 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
154 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
155 | } | |
dac39588 | 156 | } |
6e62b1c7 | 157 | |
6808d7a1 | 158 | undo(move) { |
dac39588 BA |
159 | this.disaggregateFlags(JSON.parse(move.flags)); |
160 | V.UndoOnBoard(this.board, move); | |
964eda04 | 161 | if (this.subTurn == 2 || this.movesCount == 1 || !!move.captureKing) { |
dac39588 | 162 | this.epSquares.pop(); |
9842aca2 | 163 | this.movesCount--; |
b406466b BA |
164 | if (this.movesCount == 0) this.turn = "w"; |
165 | } | |
166 | else { | |
6808d7a1 | 167 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; |
dac39588 | 168 | lastEpsq.pop(); |
b406466b | 169 | this.turn = V.GetOppCol(this.turn); |
dac39588 | 170 | } |
b406466b | 171 | if (this.movesCount > 0) this.subTurn = 3 - this.subTurn; |
bc0b9205 | 172 | this.postUndo(move); |
dac39588 | 173 | } |
6e62b1c7 | 174 | |
bc0b9205 BA |
175 | postUndo(move) { |
176 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) | |
177 | // Opponent's king was captured | |
178 | this.kingPos[move.vanish[1].c] = [move.vanish[1].x, move.vanish[1].y]; | |
179 | super.postUndo(move); | |
dac39588 | 180 | } |
0596f5e7 | 181 | |
bc0b9205 | 182 | // No alpha-beta here, just adapted min-max at depth 2(+1) |
6808d7a1 | 183 | getComputerMove() { |
bc0b9205 | 184 | const maxeval = V.INFINITY; |
dac39588 | 185 | const color = this.turn; |
bc0b9205 BA |
186 | const oppCol = V.GetOppCol(this.turn); |
187 | ||
188 | // Search best (half) move for opponent turn | |
189 | const getBestMoveEval = () => { | |
190 | let score = this.getCurrentScore(); | |
191 | if (score != "*") return maxeval * (score == "1-0" ? 1 : -1); | |
192 | let moves = this.getAllValidMoves(); | |
193 | let res = oppCol == "w" ? -maxeval : maxeval; | |
194 | for (let m of moves) { | |
195 | this.play(m); | |
196 | score = this.getCurrentScore(); | |
197 | if (score != "*") { | |
198 | // King captured | |
199 | this.undo(m); | |
200 | return maxeval * (score == "1-0" ? 1 : -1); | |
201 | } | |
202 | const evalPos = this.evalPosition(); | |
203 | res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos); | |
204 | this.undo(m); | |
205 | } | |
206 | return res; | |
207 | }; | |
208 | ||
5d75c82c BA |
209 | const moves11 = this.getAllValidMoves(); |
210 | if (this.movesCount == 0) | |
211 | // First white move at random: | |
212 | return moves11[randInt(moves11.length)]; | |
737a5daf BA |
213 | let doubleMove = null; |
214 | let bestEval = Number.POSITIVE_INFINITY * (color == 'w' ? -1 : 1); | |
5d75c82c | 215 | // Rank moves using a min-max at depth 2 |
6808d7a1 | 216 | for (let i = 0; i < moves11.length; i++) { |
dac39588 | 217 | this.play(moves11[i]); |
5d75c82c | 218 | const moves12 = this.getAllValidMoves(); |
b406466b BA |
219 | for (let j = 0; j < moves12.length; j++) { |
220 | this.play(moves12[j]); | |
737a5daf BA |
221 | // Small fluctuations to uniformize play a little |
222 | const evalM = getBestMoveEval() + 0.05 - Math.random() / 10 | |
223 | if ( | |
224 | (color == 'w' && evalM > bestEval) || | |
225 | (color == 'b' && evalM < bestEval) | |
226 | ) { | |
227 | doubleMove = [moves11[i], moves12[j]]; | |
228 | bestEval = evalM; | |
229 | } | |
b406466b | 230 | this.undo(moves12[j]); |
dac39588 BA |
231 | } |
232 | this.undo(moves11[i]); | |
233 | } | |
737a5daf BA |
234 | // TODO: not always the best move played (why ???) |
235 | return doubleMove; | |
dac39588 | 236 | } |
6808d7a1 | 237 | }; |