Add variants-related link
[vchess.git] / client / src / variants / Doublemove1.js
CommitLineData
829f6574
BA
1import { ChessRules } from "@/base_rules";
2
b406466b 3export 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
6808d7a1 79 play(move) {
dac39588 80 move.flags = JSON.stringify(this.aggregateFlags());
964eda04 81 move.turn = [this.turn, this.subTurn];
dac39588
BA
82 V.PlayOnBoard(this.board, move);
83 const epSq = this.getEpSquare(move);
9842aca2
BA
84 if (this.movesCount == 0) {
85 // First move in game
dac39588 86 this.turn = "b";
dac39588 87 this.epSquares.push([epSq]);
9842aca2 88 this.movesCount = 1;
dac39588 89 }
737a5daf
BA
90 // Does this move give check on subturn 1 or reach stalemate?
91 // If yes, skip subturn 2
92 else if (
93 this.subTurn == 1 &&
94 (
95 this.underCheck(V.GetOppCol(this.turn)) ||
96 !this.atLeastOneMove()
97 )
98 ) {
dac39588
BA
99 this.turn = V.GetOppCol(this.turn);
100 this.epSquares.push([epSq]);
101 move.checkOnSubturn1 = true;
9842aca2 102 this.movesCount++;
964eda04
BA
103 }
104 else {
6808d7a1 105 if (this.subTurn == 2) {
dac39588 106 this.turn = V.GetOppCol(this.turn);
6808d7a1 107 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588 108 lastEpsq.push(epSq);
964eda04
BA
109 }
110 else {
9842aca2
BA
111 this.epSquares.push([epSq]);
112 this.movesCount++;
113 }
dac39588
BA
114 this.subTurn = 3 - this.subTurn;
115 }
3a2a7b5f
BA
116 this.postPlay(move);
117 }
118
119 postPlay(move) {
964eda04 120 const c = move.turn[0];
3a2a7b5f
BA
121 const piece = move.vanish[0].p;
122 const firstRank = c == "w" ? V.size.x - 1 : 0;
123
737a5daf 124 if (piece == V.KING) {
3a2a7b5f
BA
125 this.kingPos[c][0] = move.appear[0].x;
126 this.kingPos[c][1] = move.appear[0].y;
0d5335de 127 this.castleFlags[c] = [V.size.y, V.size.y];
3a2a7b5f
BA
128 return;
129 }
130 const oppCol = V.GetOppCol(c);
131 const oppFirstRank = V.size.x - 1 - firstRank;
132 if (
133 move.start.x == firstRank && //our rook moves?
134 this.castleFlags[c].includes(move.start.y)
135 ) {
136 const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
137 this.castleFlags[c][flagIdx] = V.size.y;
964eda04 138 }
737a5daf 139 if (
3a2a7b5f
BA
140 move.end.x == oppFirstRank && //we took opponent rook?
141 this.castleFlags[oppCol].includes(move.end.y)
142 ) {
143 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
144 this.castleFlags[oppCol][flagIdx] = V.size.y;
145 }
dac39588 146 }
6e62b1c7 147
6808d7a1 148 undo(move) {
dac39588
BA
149 this.disaggregateFlags(JSON.parse(move.flags));
150 V.UndoOnBoard(this.board, move);
9842aca2
BA
151 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
152 // The move may not be full, but is fully undone:
dac39588 153 this.epSquares.pop();
9842aca2
BA
154 // Moves counter was just incremented:
155 this.movesCount--;
964eda04
BA
156 }
157 else {
9842aca2 158 // Undo the second half of a move
6808d7a1 159 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588
BA
160 lastEpsq.pop();
161 }
162 this.turn = move.turn[0];
964eda04 163 this.subTurn = move.turn[1];
3a2a7b5f 164 super.postUndo(move);
dac39588 165 }
6e62b1c7 166
6808d7a1 167 static get VALUES() {
dac39588 168 return {
6808d7a1
BA
169 p: 1,
170 r: 5,
171 n: 3,
172 b: 3,
173 q: 7, //slightly less than in orthodox game
174 k: 1000
dac39588
BA
175 };
176 }
0596f5e7 177
dac39588 178 // No alpha-beta here, just adapted min-max at depth 2(+1)
6808d7a1 179 getComputerMove() {
dac39588
BA
180 const maxeval = V.INFINITY;
181 const color = this.turn;
182 const oppCol = V.GetOppCol(this.turn);
51882959 183
dac39588
BA
184 // Search best (half) move for opponent turn
185 const getBestMoveEval = () => {
dac39588 186 let score = this.getCurrentScore();
6808d7a1
BA
187 if (score != "*") {
188 if (score == "1/2") return 0;
dac39588
BA
189 return maxeval * (score == "1-0" ? 1 : -1);
190 }
191 let moves = this.getAllValidMoves();
6808d7a1
BA
192 let res = oppCol == "w" ? -maxeval : maxeval;
193 for (let m of moves) {
dac39588
BA
194 this.play(m);
195 score = this.getCurrentScore();
196 // Now turn is oppCol,2 if m doesn't give check
197 // Otherwise it's color,1. In both cases the next test makes sense
6808d7a1 198 if (score != "*") {
dac39588 199 if (score == "1/2")
6808d7a1
BA
200 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
201 else {
dac39588
BA
202 // Found a mate
203 this.undo(m);
204 return maxeval * (score == "1-0" ? 1 : -1);
205 }
206 }
207 const evalPos = this.evalPosition();
6808d7a1 208 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
dac39588
BA
209 this.undo(m);
210 }
211 return res;
212 };
6e62b1c7 213
bc0b9205 214 const moves11 = this.getAllValidMoves();
737a5daf
BA
215 let doubleMove = null;
216 let bestEval = Number.POSITIVE_INFINITY * (color == 'w' ? -1 : 1);
dac39588 217 // Rank moves using a min-max at depth 2
6808d7a1 218 for (let i = 0; i < moves11.length; i++) {
dac39588 219 this.play(moves11[i]);
6808d7a1 220 if (this.turn != color) {
dac39588 221 // We gave check with last move: search the best opponent move
737a5daf
BA
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];
228 bestEval = evalM;
229 }
bc0b9205
BA
230 }
231 else {
dac39588 232 let moves12 = this.getAllValidMoves();
6808d7a1 233 for (let j = 0; j < moves12.length; j++) {
dac39588 234 this.play(moves12[j]);
737a5daf
BA
235 const evalM = getBestMoveEval() + 0.05 - Math.random() / 10
236 if (
237 (color == 'w' && evalM > bestEval) ||
238 (color == 'b' && evalM < bestEval)
239 ) {
240 doubleMove = [moves11[i], moves12[j]];
241 bestEval = evalM;
242 }
dac39588
BA
243 this.undo(moves12[j]);
244 }
245 }
246 this.undo(moves11[i]);
247 }
737a5daf 248 return doubleMove;
dac39588 249 }
6808d7a1 250};