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