Commit | Line | Data |
---|---|---|
829f6574 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
b406466b | 3 | export class Doublemove1Rules extends ChessRules { |
6808d7a1 | 4 | static IsGoodEnpassant(enpassant) { |
e71161fb BA |
5 | const squares = enpassant.split(","); |
6 | if (squares.length > 2) return false; | |
7 | for (let sq of squares) { | |
8 | if (sq != "-") { | |
dac39588 | 9 | const ep = V.SquareToCoords(sq); |
6808d7a1 | 10 | if (isNaN(ep.x) || !V.OnBoard(ep)) return false; |
dac39588 BA |
11 | } |
12 | } | |
13 | return true; | |
14 | } | |
6e62b1c7 | 15 | |
dac39588 | 16 | // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn) |
6808d7a1 | 17 | getEnpassantFen() { |
e71161fb BA |
18 | return this.epSquares[this.epSquares.length - 1].map( |
19 | epsq => epsq === undefined | |
20 | ? "-" //no en-passant | |
21 | : V.CoordsToSquare(epsq) | |
22 | ).join(","); | |
dac39588 | 23 | } |
6e62b1c7 | 24 | |
6808d7a1 | 25 | setOtherVariables(fen) { |
dac39588 BA |
26 | const parsedFen = V.ParseFen(fen); |
27 | this.setFlags(parsedFen.flags); | |
e71161fb BA |
28 | this.epSquares = [parsedFen.enpassant.split(",").map(sq => { |
29 | if (sq != "-") return V.SquareToCoords(sq); | |
30 | return undefined; | |
31 | })]; | |
3a2a7b5f | 32 | this.scanKings(fen); |
af34341d BA |
33 | this.turn = parsedFen.turn; |
34 | this.subTurn = 1; | |
dac39588 | 35 | } |
6e62b1c7 | 36 | |
32f6285e | 37 | getEnpassantCaptures([x, y], shiftX) { |
dac39588 | 38 | let moves = []; |
dac39588 BA |
39 | // En passant: always OK if subturn 1, |
40 | // OK on subturn 2 only if enPassant was played at subturn 1 | |
41 | // (and if there are two e.p. squares available). | |
42 | const Lep = this.epSquares.length; | |
6808d7a1 | 43 | const epSquares = this.epSquares[Lep - 1]; //always at least one element |
dac39588 BA |
44 | let epSqs = []; |
45 | epSquares.forEach(sq => { | |
6808d7a1 | 46 | if (sq) epSqs.push(sq); |
dac39588 | 47 | }); |
6808d7a1 | 48 | if (epSqs.length == 0) return moves; |
32f6285e | 49 | const oppCol = V.GetOppCol(this.getColor(x, y)); |
6808d7a1 BA |
50 | for (let sq of epSqs) { |
51 | if ( | |
52 | this.subTurn == 1 || | |
53 | (epSqs.length == 2 && | |
54 | // Was this en-passant capture already played at subturn 1 ? | |
55 | // (Or maybe the opponent filled the en-passant square with a piece) | |
56 | this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY) | |
57 | ) { | |
58 | if ( | |
59 | sq.x == x + shiftX && | |
60 | Math.abs(sq.y - y) == 1 && | |
dac39588 | 61 | // Add condition "enemy pawn must be present" |
6808d7a1 BA |
62 | this.getPiece(x, sq.y) == V.PAWN && |
63 | this.getColor(x, sq.y) == oppCol | |
64 | ) { | |
65 | let epMove = this.getBasicMove([x, y], [sq.x, sq.y]); | |
dac39588 BA |
66 | epMove.vanish.push({ |
67 | x: x, | |
68 | y: sq.y, | |
6808d7a1 | 69 | p: "p", |
dac39588 BA |
70 | c: oppCol |
71 | }); | |
72 | moves.push(epMove); | |
73 | } | |
74 | } | |
75 | } | |
dac39588 BA |
76 | return moves; |
77 | } | |
6e62b1c7 | 78 | |
c3ff3a0c BA |
79 | atLeastOneMove() { |
80 | const color = this.turn; | |
81 | for (let i = 0; i < V.size.x; i++) { | |
82 | for (let j = 0; j < V.size.y; j++) { | |
83 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { | |
84 | const moves = this.getPotentialMovesFrom([i, j]); | |
85 | if (moves.length > 0) { | |
86 | for (let k = 0; k < moves.length; k++) { | |
87 | const m = moves[k]; | |
88 | // NOTE: not using play() here (=> infinite loop) | |
89 | V.PlayOnBoard(this.board, m); | |
90 | if (m.vanish[0].p == V.KING) | |
91 | this.kingPos[color] = [m.appear[0].x, m.appear[0].y]; | |
92 | const res = !this.underCheck(color); | |
93 | V.UndoOnBoard(this.board, m); | |
94 | if (m.vanish[0].p == V.KING) | |
95 | this.kingPos[color] = [m.start.x, m.start.y]; | |
96 | if (res) return true; | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | } | |
102 | return false; | |
103 | } | |
104 | ||
6808d7a1 | 105 | play(move) { |
dac39588 | 106 | move.flags = JSON.stringify(this.aggregateFlags()); |
964eda04 | 107 | move.turn = [this.turn, this.subTurn]; |
dac39588 BA |
108 | V.PlayOnBoard(this.board, move); |
109 | const epSq = this.getEpSquare(move); | |
c3ff3a0c | 110 | const oppCol = V.GetOppCol(this.turn); |
9842aca2 BA |
111 | if (this.movesCount == 0) { |
112 | // First move in game | |
dac39588 | 113 | this.turn = "b"; |
dac39588 | 114 | this.epSquares.push([epSq]); |
9842aca2 | 115 | this.movesCount = 1; |
dac39588 | 116 | } |
737a5daf BA |
117 | // Does this move give check on subturn 1 or reach stalemate? |
118 | // If yes, skip subturn 2 | |
119 | else if ( | |
120 | this.subTurn == 1 && | |
121 | ( | |
122 | this.underCheck(V.GetOppCol(this.turn)) || | |
123 | !this.atLeastOneMove() | |
124 | ) | |
125 | ) { | |
c3ff3a0c | 126 | this.turn = oppCol; |
dac39588 BA |
127 | this.epSquares.push([epSq]); |
128 | move.checkOnSubturn1 = true; | |
9842aca2 | 129 | this.movesCount++; |
964eda04 BA |
130 | } |
131 | else { | |
6808d7a1 | 132 | if (this.subTurn == 2) { |
c3ff3a0c | 133 | this.turn = oppCol; |
6808d7a1 | 134 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; |
dac39588 | 135 | lastEpsq.push(epSq); |
964eda04 BA |
136 | } |
137 | else { | |
9842aca2 BA |
138 | this.epSquares.push([epSq]); |
139 | this.movesCount++; | |
140 | } | |
dac39588 BA |
141 | this.subTurn = 3 - this.subTurn; |
142 | } | |
3a2a7b5f BA |
143 | this.postPlay(move); |
144 | } | |
145 | ||
146 | postPlay(move) { | |
964eda04 | 147 | const c = move.turn[0]; |
3a2a7b5f BA |
148 | const piece = move.vanish[0].p; |
149 | const firstRank = c == "w" ? V.size.x - 1 : 0; | |
150 | ||
737a5daf | 151 | if (piece == V.KING) { |
c3ff3a0c | 152 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; |
0d5335de | 153 | this.castleFlags[c] = [V.size.y, V.size.y]; |
3a2a7b5f BA |
154 | return; |
155 | } | |
156 | const oppCol = V.GetOppCol(c); | |
157 | const oppFirstRank = V.size.x - 1 - firstRank; | |
158 | if ( | |
159 | move.start.x == firstRank && //our rook moves? | |
160 | this.castleFlags[c].includes(move.start.y) | |
161 | ) { | |
162 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
163 | this.castleFlags[c][flagIdx] = V.size.y; | |
964eda04 | 164 | } |
737a5daf | 165 | if ( |
3a2a7b5f BA |
166 | move.end.x == oppFirstRank && //we took opponent rook? |
167 | this.castleFlags[oppCol].includes(move.end.y) | |
168 | ) { | |
169 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
170 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
171 | } | |
dac39588 | 172 | } |
6e62b1c7 | 173 | |
6808d7a1 | 174 | undo(move) { |
dac39588 BA |
175 | this.disaggregateFlags(JSON.parse(move.flags)); |
176 | V.UndoOnBoard(this.board, move); | |
9842aca2 BA |
177 | if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) { |
178 | // The move may not be full, but is fully undone: | |
dac39588 | 179 | this.epSquares.pop(); |
9842aca2 BA |
180 | // Moves counter was just incremented: |
181 | this.movesCount--; | |
964eda04 BA |
182 | } |
183 | else { | |
9842aca2 | 184 | // Undo the second half of a move |
6808d7a1 | 185 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; |
dac39588 BA |
186 | lastEpsq.pop(); |
187 | } | |
188 | this.turn = move.turn[0]; | |
964eda04 | 189 | this.subTurn = move.turn[1]; |
3a2a7b5f | 190 | super.postUndo(move); |
dac39588 | 191 | } |
6e62b1c7 | 192 | |
6808d7a1 | 193 | static get VALUES() { |
dac39588 | 194 | return { |
6808d7a1 BA |
195 | p: 1, |
196 | r: 5, | |
197 | n: 3, | |
198 | b: 3, | |
199 | q: 7, //slightly less than in orthodox game | |
200 | k: 1000 | |
dac39588 BA |
201 | }; |
202 | } | |
0596f5e7 | 203 | |
dac39588 | 204 | // No alpha-beta here, just adapted min-max at depth 2(+1) |
6808d7a1 | 205 | getComputerMove() { |
dac39588 BA |
206 | const maxeval = V.INFINITY; |
207 | const color = this.turn; | |
208 | const oppCol = V.GetOppCol(this.turn); | |
51882959 | 209 | |
dac39588 BA |
210 | // Search best (half) move for opponent turn |
211 | const getBestMoveEval = () => { | |
dac39588 | 212 | let score = this.getCurrentScore(); |
6808d7a1 BA |
213 | if (score != "*") { |
214 | if (score == "1/2") return 0; | |
dac39588 BA |
215 | return maxeval * (score == "1-0" ? 1 : -1); |
216 | } | |
217 | let moves = this.getAllValidMoves(); | |
6808d7a1 BA |
218 | let res = oppCol == "w" ? -maxeval : maxeval; |
219 | for (let m of moves) { | |
dac39588 BA |
220 | this.play(m); |
221 | score = this.getCurrentScore(); | |
222 | // Now turn is oppCol,2 if m doesn't give check | |
223 | // Otherwise it's color,1. In both cases the next test makes sense | |
6808d7a1 | 224 | if (score != "*") { |
dac39588 | 225 | if (score == "1/2") |
6808d7a1 BA |
226 | res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0); |
227 | else { | |
dac39588 BA |
228 | // Found a mate |
229 | this.undo(m); | |
230 | return maxeval * (score == "1-0" ? 1 : -1); | |
231 | } | |
232 | } | |
233 | const evalPos = this.evalPosition(); | |
6808d7a1 | 234 | res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos); |
dac39588 BA |
235 | this.undo(m); |
236 | } | |
237 | return res; | |
238 | }; | |
6e62b1c7 | 239 | |
bc0b9205 | 240 | const moves11 = this.getAllValidMoves(); |
737a5daf BA |
241 | let doubleMove = null; |
242 | let bestEval = Number.POSITIVE_INFINITY * (color == 'w' ? -1 : 1); | |
dac39588 | 243 | // Rank moves using a min-max at depth 2 |
6808d7a1 | 244 | for (let i = 0; i < moves11.length; i++) { |
dac39588 | 245 | this.play(moves11[i]); |
6808d7a1 | 246 | if (this.turn != color) { |
dac39588 | 247 | // We gave check with last move: search the best opponent move |
737a5daf BA |
248 | const evalM = getBestMoveEval() + 0.05 - Math.random() / 10; |
249 | if ( | |
250 | (color == 'w' && evalM > bestEval) || | |
251 | (color == 'b' && evalM < bestEval) | |
252 | ) { | |
253 | doubleMove = moves11[i]; | |
254 | bestEval = evalM; | |
255 | } | |
bc0b9205 BA |
256 | } |
257 | else { | |
dac39588 | 258 | let moves12 = this.getAllValidMoves(); |
6808d7a1 | 259 | for (let j = 0; j < moves12.length; j++) { |
dac39588 | 260 | this.play(moves12[j]); |
737a5daf BA |
261 | const evalM = getBestMoveEval() + 0.05 - Math.random() / 10 |
262 | if ( | |
263 | (color == 'w' && evalM > bestEval) || | |
264 | (color == 'b' && evalM < bestEval) | |
265 | ) { | |
266 | doubleMove = [moves11[i], moves12[j]]; | |
267 | bestEval = evalM; | |
268 | } | |
dac39588 BA |
269 | this.undo(moves12[j]); |
270 | } | |
271 | } | |
272 | this.undo(moves11[i]); | |
273 | } | |
737a5daf | 274 | return doubleMove; |
dac39588 | 275 | } |
6808d7a1 | 276 | }; |