Fix Eightpieces, add some simple variants, add a basic variants classification instea...
[vchess.git] / client / src / variants / Doublemove1.js
1 import { ChessRules } from "@/base_rules";
2
3 export class Doublemove1Rules extends ChessRules {
4 static IsGoodEnpassant(enpassant) {
5 const squares = enpassant.split(",");
6 if (squares.length > 2) return false;
7 for (let sq of squares) {
8 if (sq != "-") {
9 const ep = V.SquareToCoords(sq);
10 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
11 }
12 }
13 return true;
14 }
15
16 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
17 getEnpassantFen() {
18 return this.epSquares[this.epSquares.length - 1].map(
19 epsq => epsq === undefined
20 ? "-" //no en-passant
21 : V.CoordsToSquare(epsq)
22 ).join(",");
23 }
24
25 setOtherVariables(fen) {
26 const parsedFen = V.ParseFen(fen);
27 this.setFlags(parsedFen.flags);
28 this.epSquares = [parsedFen.enpassant.split(",").map(sq => {
29 if (sq != "-") return V.SquareToCoords(sq);
30 return undefined;
31 })];
32 this.scanKings(fen);
33 this.turn = parsedFen.turn;
34 this.subTurn = 1;
35 }
36
37 getEnpassantCaptures([x, y], shiftX) {
38 let moves = [];
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;
43 const epSquares = this.epSquares[Lep - 1]; //always at least one element
44 let epSqs = [];
45 epSquares.forEach(sq => {
46 if (sq) epSqs.push(sq);
47 });
48 if (epSqs.length == 0) return moves;
49 const oppCol = V.GetOppCol(this.getColor(x, y));
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 &&
61 // Add condition "enemy pawn must be present"
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]);
66 epMove.vanish.push({
67 x: x,
68 y: sq.y,
69 p: "p",
70 c: oppCol
71 });
72 moves.push(epMove);
73 }
74 }
75 }
76 return moves;
77 }
78
79 play(move) {
80 move.flags = JSON.stringify(this.aggregateFlags());
81 move.turn = [this.turn, this.subTurn];
82 V.PlayOnBoard(this.board, move);
83 const epSq = this.getEpSquare(move);
84 if (this.movesCount == 0) {
85 // First move in game
86 this.turn = "b";
87 this.epSquares.push([epSq]);
88 this.movesCount = 1;
89 }
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 ) {
99 this.turn = V.GetOppCol(this.turn);
100 this.epSquares.push([epSq]);
101 move.checkOnSubturn1 = true;
102 this.movesCount++;
103 }
104 else {
105 if (this.subTurn == 2) {
106 this.turn = V.GetOppCol(this.turn);
107 let lastEpsq = this.epSquares[this.epSquares.length - 1];
108 lastEpsq.push(epSq);
109 }
110 else {
111 this.epSquares.push([epSq]);
112 this.movesCount++;
113 }
114 this.subTurn = 3 - this.subTurn;
115 }
116 this.postPlay(move);
117 }
118
119 postPlay(move) {
120 const c = move.turn[0];
121 const piece = move.vanish[0].p;
122 const firstRank = c == "w" ? V.size.x - 1 : 0;
123
124 if (piece == V.KING) {
125 this.kingPos[c][0] = move.appear[0].x;
126 this.kingPos[c][1] = move.appear[0].y;
127 this.castleFlags[c] = [V.size.y, V.size.y];
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;
138 }
139 if (
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 }
146 }
147
148 undo(move) {
149 this.disaggregateFlags(JSON.parse(move.flags));
150 V.UndoOnBoard(this.board, move);
151 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
152 // The move may not be full, but is fully undone:
153 this.epSquares.pop();
154 // Moves counter was just incremented:
155 this.movesCount--;
156 }
157 else {
158 // Undo the second half of a move
159 let lastEpsq = this.epSquares[this.epSquares.length - 1];
160 lastEpsq.pop();
161 }
162 this.turn = move.turn[0];
163 this.subTurn = move.turn[1];
164 super.postUndo(move);
165 }
166
167 static get VALUES() {
168 return {
169 p: 1,
170 r: 5,
171 n: 3,
172 b: 3,
173 q: 7, //slightly less than in orthodox game
174 k: 1000
175 };
176 }
177
178 // No alpha-beta here, just adapted min-max at depth 2(+1)
179 getComputerMove() {
180 const maxeval = V.INFINITY;
181 const color = this.turn;
182 const oppCol = V.GetOppCol(this.turn);
183
184 // Search best (half) move for opponent turn
185 const getBestMoveEval = () => {
186 let score = this.getCurrentScore();
187 if (score != "*") {
188 if (score == "1/2") return 0;
189 return maxeval * (score == "1-0" ? 1 : -1);
190 }
191 let moves = this.getAllValidMoves();
192 let res = oppCol == "w" ? -maxeval : maxeval;
193 for (let m of moves) {
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
198 if (score != "*") {
199 if (score == "1/2")
200 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
201 else {
202 // Found a mate
203 this.undo(m);
204 return maxeval * (score == "1-0" ? 1 : -1);
205 }
206 }
207 const evalPos = this.evalPosition();
208 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
209 this.undo(m);
210 }
211 return res;
212 };
213
214 const moves11 = this.getAllValidMoves();
215 let doubleMove = null;
216 let bestEval = Number.POSITIVE_INFINITY * (color == 'w' ? -1 : 1);
217 // Rank moves using a min-max at depth 2
218 for (let i = 0; i < moves11.length; i++) {
219 this.play(moves11[i]);
220 if (this.turn != color) {
221 // We gave check with last move: search the best opponent move
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 }
230 }
231 else {
232 let moves12 = this.getAllValidMoves();
233 for (let j = 0; j < moves12.length; j++) {
234 this.play(moves12[j]);
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 }
243 this.undo(moves12[j]);
244 }
245 }
246 this.undo(moves11[i]);
247 }
248 return doubleMove;
249 }
250 };