Add Swap, Switching, pawns variants
[vchess.git] / client / src / variants / Doublemove1.js
... / ...
CommitLineData
1import { ChessRules } from "@/base_rules";
2import { randInt } from "@/utils/alea";
3
4export class Doublemove1Rules extends ChessRules {
5 static IsGoodEnpassant(enpassant) {
6 const squares = enpassant.split(",");
7 if (squares.length > 2) return false;
8 for (let sq of squares) {
9 if (sq != "-") {
10 const ep = V.SquareToCoords(sq);
11 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
12 }
13 }
14 return true;
15 }
16
17 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
18 getEnpassantFen() {
19 return this.epSquares[this.epSquares.length - 1].map(
20 epsq => epsq === undefined
21 ? "-" //no en-passant
22 : V.CoordsToSquare(epsq)
23 ).join(",");
24 }
25
26 setOtherVariables(fen) {
27 const parsedFen = V.ParseFen(fen);
28 this.setFlags(parsedFen.flags);
29 this.epSquares = [parsedFen.enpassant.split(",").map(sq => {
30 if (sq != "-") return V.SquareToCoords(sq);
31 return undefined;
32 })];
33 this.scanKings(fen);
34 this.turn = parsedFen.turn;
35 this.subTurn = 1;
36 }
37
38 getEnpassantCaptures([x, y], shiftX) {
39 let moves = [];
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;
44 const epSquares = this.epSquares[Lep - 1]; //always at least one element
45 let epSqs = [];
46 epSquares.forEach(sq => {
47 if (sq) epSqs.push(sq);
48 });
49 if (epSqs.length == 0) return moves;
50 const oppCol = V.GetOppCol(this.getColor(x, y));
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 &&
62 // Add condition "enemy pawn must be present"
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]);
67 epMove.vanish.push({
68 x: x,
69 y: sq.y,
70 p: "p",
71 c: oppCol
72 });
73 moves.push(epMove);
74 }
75 }
76 }
77 return moves;
78 }
79
80 play(move) {
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);
85 if (this.movesCount == 0) {
86 // First move in game
87 this.turn = "b";
88 this.epSquares.push([epSq]);
89 this.movesCount = 1;
90 }
91 // Does this move give check on subturn 1? If yes, skip subturn 2
92 else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) {
93 this.turn = V.GetOppCol(this.turn);
94 this.epSquares.push([epSq]);
95 move.checkOnSubturn1 = true;
96 this.movesCount++;
97 }
98 else {
99 if (this.subTurn == 2) {
100 this.turn = V.GetOppCol(this.turn);
101 let lastEpsq = this.epSquares[this.epSquares.length - 1];
102 lastEpsq.push(epSq);
103 }
104 else {
105 this.epSquares.push([epSq]);
106 this.movesCount++;
107 }
108 this.subTurn = 3 - this.subTurn;
109 }
110 this.postPlay(move);
111 }
112
113 postPlay(move) {
114 const c = move.turn[0];
115 const piece = move.vanish[0].p;
116 const firstRank = c == "w" ? V.size.x - 1 : 0;
117
118 if (piece == V.KING && move.appear.length > 0) {
119 this.kingPos[c][0] = move.appear[0].x;
120 this.kingPos[c][1] = move.appear[0].y;
121 this.castleFlags[c] = [V.size.y, V.size.y];
122 return;
123 }
124 const oppCol = V.GetOppCol(c);
125 const oppFirstRank = V.size.x - 1 - firstRank;
126 if (
127 move.start.x == firstRank && //our rook moves?
128 this.castleFlags[c].includes(move.start.y)
129 ) {
130 const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
131 this.castleFlags[c][flagIdx] = V.size.y;
132 }
133 else if (
134 move.end.x == oppFirstRank && //we took opponent rook?
135 this.castleFlags[oppCol].includes(move.end.y)
136 ) {
137 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
138 this.castleFlags[oppCol][flagIdx] = V.size.y;
139 }
140 }
141
142 undo(move) {
143 this.disaggregateFlags(JSON.parse(move.flags));
144 V.UndoOnBoard(this.board, move);
145 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
146 // The move may not be full, but is fully undone:
147 this.epSquares.pop();
148 // Moves counter was just incremented:
149 this.movesCount--;
150 }
151 else {
152 // Undo the second half of a move
153 let lastEpsq = this.epSquares[this.epSquares.length - 1];
154 lastEpsq.pop();
155 }
156 this.turn = move.turn[0];
157 this.subTurn = move.turn[1];
158 super.postUndo(move);
159 }
160
161 static get VALUES() {
162 return {
163 p: 1,
164 r: 5,
165 n: 3,
166 b: 3,
167 q: 7, //slightly less than in orthodox game
168 k: 1000
169 };
170 }
171
172 // No alpha-beta here, just adapted min-max at depth 2(+1)
173 getComputerMove() {
174 const maxeval = V.INFINITY;
175 const color = this.turn;
176 const oppCol = V.GetOppCol(this.turn);
177
178 // Search best (half) move for opponent turn
179 const getBestMoveEval = () => {
180 let score = this.getCurrentScore();
181 if (score != "*") {
182 if (score == "1/2") return 0;
183 return maxeval * (score == "1-0" ? 1 : -1);
184 }
185 let moves = this.getAllValidMoves();
186 let res = oppCol == "w" ? -maxeval : maxeval;
187 for (let m of moves) {
188 this.play(m);
189 score = this.getCurrentScore();
190 // Now turn is oppCol,2 if m doesn't give check
191 // Otherwise it's color,1. In both cases the next test makes sense
192 if (score != "*") {
193 if (score == "1/2")
194 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
195 else {
196 // Found a mate
197 this.undo(m);
198 return maxeval * (score == "1-0" ? 1 : -1);
199 }
200 }
201 const evalPos = this.evalPosition();
202 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
203 this.undo(m);
204 }
205 return res;
206 };
207
208 const moves11 = this.getAllValidMoves();
209 let doubleMoves = [];
210 // Rank moves using a min-max at depth 2
211 for (let i = 0; i < moves11.length; i++) {
212 this.play(moves11[i]);
213 if (this.turn != color) {
214 // We gave check with last move: search the best opponent move
215 doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() });
216 }
217 else {
218 let moves12 = this.getAllValidMoves();
219 for (let j = 0; j < moves12.length; j++) {
220 this.play(moves12[j]);
221 doubleMoves.push({
222 moves: [moves11[i], moves12[j]],
223 eval: getBestMoveEval() + 0.05 - Math.random() / 10
224 });
225 this.undo(moves12[j]);
226 }
227 }
228 this.undo(moves11[i]);
229 }
230
231 // TODO: array + sort + candidates logic not required when adding small
232 // fluctuations to the eval function (could also be generalized).
233 doubleMoves.sort((a, b) => {
234 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
235 });
236 let candidates = [0]; //indices of candidates moves
237 for (
238 let i = 1;
239 i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
240 i++
241 ) {
242 candidates.push(i);
243 }
244 const selected = doubleMoves[randInt(candidates.length)].moves;
245 if (selected.length == 1) return selected[0];
246 return selected;
247 }
248};