Advances on Chakart, almost done. Fix DoubleMove2
[vchess.git] / client / src / variants / Doublemove2.js
1 import { ChessRules } from "@/base_rules";
2 import { randInt } from "@/utils/alea";
3
4 export class Doublemove2Rules 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 // Extract subTurn from turn indicator: "w" (first move), or
35 // "w1" or "w2" white subturn 1 or 2, and same for black
36 this.turn = parsedFen.turn;
37 this.subTurn = 1;
38 }
39
40 getEnpassantCaptures([x, y], shiftX) {
41 let moves = [];
42 // En passant: always OK if subturn 1,
43 // OK on subturn 2 only if enPassant was played at subturn 1
44 // (and if there are two e.p. squares available).
45 const Lep = this.epSquares.length;
46 const epSquares = this.epSquares[Lep - 1]; //always at least one element
47 let epSqs = [];
48 epSquares.forEach(sq => {
49 if (sq) epSqs.push(sq);
50 });
51 if (epSqs.length == 0) return moves;
52 const oppCol = V.GetOppCol(this.getColor(x, y));
53 for (let sq of epSqs) {
54 if (
55 this.subTurn == 1 ||
56 (epSqs.length == 2 &&
57 // Was this en-passant capture already played at subturn 1 ?
58 // (Or maybe the opponent filled the en-passant square with a piece)
59 this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY)
60 ) {
61 if (
62 sq.x == x + shiftX &&
63 Math.abs(sq.y - y) == 1 &&
64 // Add condition "enemy pawn must be present"
65 this.getPiece(x, sq.y) == V.PAWN &&
66 this.getColor(x, sq.y) == oppCol
67 ) {
68 let epMove = this.getBasicMove([x, y], [sq.x, sq.y]);
69 epMove.vanish.push({
70 x: x,
71 y: sq.y,
72 p: "p",
73 c: oppCol
74 });
75 moves.push(epMove);
76 }
77 }
78 }
79 return moves;
80 }
81
82 isAttacked(sq, color, castling) {
83 const singleMoveAttack = super.isAttacked(sq, color);
84 if (singleMoveAttack) return true;
85 if (!!castling) {
86 if (this.subTurn == 1)
87 // Castling at move 1 could be done into check
88 return false;
89 return singleMoveAttack;
90 }
91 // Double-move allowed:
92 const curTurn = this.turn;
93 this.turn = color;
94 const moves1 = super.getAllPotentialMoves();
95 this.turn = curTurn;
96 for (let move of moves1) {
97 this.play(move);
98 const res = super.isAttacked(sq, color);
99 this.undo(move);
100 if (res) return res;
101 }
102 return false;
103 }
104
105 filterValid(moves) {
106 if (this.subTurn == 1) {
107 return moves.filter(m1 => {
108 this.play(m1);
109 // NOTE: no recursion because next call will see subTurn == 2
110 const res = super.atLeastOneMove();
111 this.undo(m1);
112 return res;
113 });
114 }
115 return super.filterValid(moves);
116 }
117
118 play(move) {
119 move.flags = JSON.stringify(this.aggregateFlags());
120 V.PlayOnBoard(this.board, move);
121 const epSq = this.getEpSquare(move);
122 if (this.subTurn == 2) {
123 let lastEpsq = this.epSquares[this.epSquares.length - 1];
124 lastEpsq.push(epSq);
125 this.turn = V.GetOppCol(this.turn);
126 }
127 else {
128 this.epSquares.push([epSq]);
129 this.movesCount++;
130 if (this.movesCount == 1) this.turn = "b";
131 }
132 if (this.movesCount > 1) this.subTurn = 3 - this.subTurn;
133 this.postPlay(move);
134 }
135
136 postPlay(move) {
137 const c = move.vanish[0].c;
138 const piece = move.vanish[0].p;
139 const firstRank = c == "w" ? V.size.x - 1 : 0;
140
141 if (piece == V.KING && move.appear.length > 0) {
142 this.kingPos[c][0] = move.appear[0].x;
143 this.kingPos[c][1] = move.appear[0].y;
144 this.castleFlags[c] = [V.size.y, V.size.y];
145 return;
146 }
147 const oppCol = V.GetOppCol(c);
148 const oppFirstRank = V.size.x - 1 - firstRank;
149 if (
150 move.start.x == firstRank && //our rook moves?
151 this.castleFlags[c].includes(move.start.y)
152 ) {
153 const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
154 this.castleFlags[c][flagIdx] = V.size.y;
155 } else if (
156 move.end.x == oppFirstRank && //we took opponent rook?
157 this.castleFlags[oppCol].includes(move.end.y)
158 ) {
159 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
160 this.castleFlags[oppCol][flagIdx] = V.size.y;
161 }
162 }
163
164 undo(move) {
165 this.disaggregateFlags(JSON.parse(move.flags));
166 V.UndoOnBoard(this.board, move);
167 if (this.subTurn == 2 || this.movesCount == 1) {
168 this.epSquares.pop();
169 this.movesCount--;
170 if (this.movesCount == 0) this.turn = "w";
171 }
172 else {
173 let lastEpsq = this.epSquares[this.epSquares.length - 1];
174 lastEpsq.pop();
175 this.turn = V.GetOppCol(this.turn);
176 }
177 if (this.movesCount > 0) this.subTurn = 3 - this.subTurn;
178 super.postUndo(move);
179 }
180
181 static get VALUES() {
182 return {
183 p: 1,
184 r: 5,
185 n: 3,
186 b: 3,
187 q: 7, //slightly less than in orthodox game
188 k: 1000
189 };
190 }
191
192 // No alpha-beta here, just adapted min-max at depth 1(+1)
193 getComputerMove() {
194 const color = this.turn;
195 const moves11 = this.getAllValidMoves();
196 if (this.movesCount == 0)
197 // First white move at random:
198 return moves11[randInt(moves11.length)];
199
200 let doubleMoves = [];
201 // Rank moves using a min-max at depth 2
202 for (let i = 0; i < moves11.length; i++) {
203 this.play(moves11[i]);
204 const moves12 = this.getAllValidMoves();
205 for (let j = 0; j < moves12.length; j++) {
206 this.play(moves12[j]);
207 doubleMoves.push({
208 moves: [moves11[i], moves12[j]],
209 eval: this.evalPosition()
210 });
211 this.undo(moves12[j]);
212 }
213 this.undo(moves11[i]);
214 }
215
216 doubleMoves.sort((a, b) => {
217 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
218 });
219 let candidates = [0]; //indices of candidates moves
220 for (
221 let i = 1;
222 i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
223 i++
224 ) {
225 candidates.push(i);
226 }
227 return doubleMoves[randInt(candidates.length)].moves;
228 }
229 };