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 | |
b406466b BA |
82 | isAttacked(sq, color, castling) { |
83 | const singleMoveAttack = super.isAttacked(sq, color); | |
84 | if (singleMoveAttack) return true; | |
85 | if (!!castling) { | |
86 | if (this.subTurn == 1) | |
87 | // Castling at move 1 could be done into check | |
88 | return false; | |
89 | return singleMoveAttack; | |
90 | } | |
91 | // Double-move allowed: | |
92 | const curTurn = this.turn; | |
93 | this.turn = color; | |
94 | const moves1 = super.getAllPotentialMoves(); | |
95 | this.turn = curTurn; | |
96 | for (let move of moves1) { | |
97 | this.play(move); | |
98 | const res = super.isAttacked(sq, color); | |
99 | this.undo(move); | |
100 | if (res) return res; | |
101 | } | |
102 | return false; | |
103 | } | |
104 | ||
105 | filterValid(moves) { | |
106 | if (this.subTurn == 1) { | |
107 | return moves.filter(m1 => { | |
108 | this.play(m1); | |
109 | // NOTE: no recursion because next call will see subTurn == 2 | |
110 | const res = super.atLeastOneMove(); | |
111 | this.undo(m1); | |
112 | return res; | |
113 | }); | |
114 | } | |
115 | return super.filterValid(moves); | |
116 | } | |
117 | ||
6808d7a1 | 118 | play(move) { |
dac39588 | 119 | move.flags = JSON.stringify(this.aggregateFlags()); |
dac39588 BA |
120 | V.PlayOnBoard(this.board, move); |
121 | const epSq = this.getEpSquare(move); | |
b406466b BA |
122 | if (this.subTurn == 2) { |
123 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; | |
124 | lastEpsq.push(epSq); | |
dac39588 | 125 | this.turn = V.GetOppCol(this.turn); |
b406466b BA |
126 | } |
127 | else { | |
dac39588 | 128 | this.epSquares.push([epSq]); |
9842aca2 | 129 | this.movesCount++; |
b406466b | 130 | if (this.movesCount == 1) this.turn = "b"; |
dac39588 | 131 | } |
b406466b | 132 | if (this.movesCount > 1) this.subTurn = 3 - this.subTurn; |
3a2a7b5f BA |
133 | this.postPlay(move); |
134 | } | |
135 | ||
136 | postPlay(move) { | |
b406466b | 137 | const c = move.vanish[0].c; |
3a2a7b5f BA |
138 | const piece = move.vanish[0].p; |
139 | const firstRank = c == "w" ? V.size.x - 1 : 0; | |
140 | ||
141 | if (piece == V.KING && move.appear.length > 0) { | |
142 | this.kingPos[c][0] = move.appear[0].x; | |
143 | this.kingPos[c][1] = move.appear[0].y; | |
0d5335de | 144 | this.castleFlags[c] = [V.size.y, V.size.y]; |
3a2a7b5f BA |
145 | return; |
146 | } | |
147 | const oppCol = V.GetOppCol(c); | |
148 | const oppFirstRank = V.size.x - 1 - firstRank; | |
149 | if ( | |
150 | move.start.x == firstRank && //our rook moves? | |
151 | this.castleFlags[c].includes(move.start.y) | |
152 | ) { | |
153 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
154 | this.castleFlags[c][flagIdx] = V.size.y; | |
155 | } else if ( | |
156 | move.end.x == oppFirstRank && //we took opponent rook? | |
157 | this.castleFlags[oppCol].includes(move.end.y) | |
158 | ) { | |
159 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
160 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
161 | } | |
dac39588 | 162 | } |
6e62b1c7 | 163 | |
6808d7a1 | 164 | undo(move) { |
dac39588 BA |
165 | this.disaggregateFlags(JSON.parse(move.flags)); |
166 | V.UndoOnBoard(this.board, move); | |
b406466b | 167 | if (this.subTurn == 2 || this.movesCount == 1) { |
dac39588 | 168 | this.epSquares.pop(); |
9842aca2 | 169 | this.movesCount--; |
b406466b BA |
170 | if (this.movesCount == 0) this.turn = "w"; |
171 | } | |
172 | else { | |
6808d7a1 | 173 | let lastEpsq = this.epSquares[this.epSquares.length - 1]; |
dac39588 | 174 | lastEpsq.pop(); |
b406466b | 175 | this.turn = V.GetOppCol(this.turn); |
dac39588 | 176 | } |
b406466b | 177 | if (this.movesCount > 0) this.subTurn = 3 - this.subTurn; |
3a2a7b5f | 178 | super.postUndo(move); |
dac39588 | 179 | } |
6e62b1c7 | 180 | |
6808d7a1 | 181 | static get VALUES() { |
dac39588 | 182 | return { |
6808d7a1 BA |
183 | p: 1, |
184 | r: 5, | |
185 | n: 3, | |
186 | b: 3, | |
187 | q: 7, //slightly less than in orthodox game | |
188 | k: 1000 | |
dac39588 BA |
189 | }; |
190 | } | |
0596f5e7 | 191 | |
dac39588 | 192 | // No alpha-beta here, just adapted min-max at depth 2(+1) |
6808d7a1 | 193 | getComputerMove() { |
dac39588 BA |
194 | const maxeval = V.INFINITY; |
195 | const color = this.turn; | |
196 | const oppCol = V.GetOppCol(this.turn); | |
51882959 | 197 | |
dac39588 BA |
198 | // Search best (half) move for opponent turn |
199 | const getBestMoveEval = () => { | |
dac39588 | 200 | let score = this.getCurrentScore(); |
6808d7a1 BA |
201 | if (score != "*") { |
202 | if (score == "1/2") return 0; | |
dac39588 BA |
203 | return maxeval * (score == "1-0" ? 1 : -1); |
204 | } | |
205 | let moves = this.getAllValidMoves(); | |
6808d7a1 BA |
206 | let res = oppCol == "w" ? -maxeval : maxeval; |
207 | for (let m of moves) { | |
dac39588 BA |
208 | this.play(m); |
209 | score = this.getCurrentScore(); | |
b406466b | 210 | // Now turn is oppCol,2 |
6808d7a1 | 211 | if (score != "*") { |
dac39588 | 212 | if (score == "1/2") |
6808d7a1 BA |
213 | res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0); |
214 | else { | |
b406466b | 215 | // King captured |
dac39588 BA |
216 | this.undo(m); |
217 | return maxeval * (score == "1-0" ? 1 : -1); | |
218 | } | |
219 | } | |
220 | const evalPos = this.evalPosition(); | |
6808d7a1 | 221 | res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos); |
dac39588 BA |
222 | this.undo(m); |
223 | } | |
224 | return res; | |
225 | }; | |
6e62b1c7 | 226 | |
dac39588 BA |
227 | let moves11 = this.getAllValidMoves(); |
228 | let doubleMoves = []; | |
b406466b | 229 | // Rank moves using a min-max at depth 2(+1) |
6808d7a1 | 230 | for (let i = 0; i < moves11.length; i++) { |
dac39588 | 231 | this.play(moves11[i]); |
b406466b BA |
232 | let moves12 = this.getAllValidMoves(); |
233 | for (let j = 0; j < moves12.length; j++) { | |
234 | this.play(moves12[j]); | |
235 | doubleMoves.push({ | |
236 | moves: [moves11[i], moves12[j]], | |
237 | eval: getBestMoveEval() | |
238 | }); | |
239 | this.undo(moves12[j]); | |
dac39588 BA |
240 | } |
241 | this.undo(moves11[i]); | |
242 | } | |
6e62b1c7 | 243 | |
6808d7a1 BA |
244 | doubleMoves.sort((a, b) => { |
245 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); | |
246 | }); | |
dac39588 | 247 | let candidates = [0]; //indices of candidates moves |
6808d7a1 BA |
248 | for ( |
249 | let i = 1; | |
250 | i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval; | |
251 | i++ | |
252 | ) { | |
dac39588 BA |
253 | candidates.push(i); |
254 | } | |
b406466b | 255 | return doubleMoves[randInt(candidates.length)].moves; |
dac39588 | 256 | } |
6808d7a1 | 257 | }; |