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