Fixing attempt: clients now track tmpIds to know who is online/onfocus. Bug fixed...
[vchess.git] / client / src / variants / Marseille.js
1 import { ChessRules } from "@/base_rules";
2 import { randInt } from "@/utils/alea";
3
4 export class MarseilleRules 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 getTurnFen() {
18 return this.turn + this.subTurn;
19 }
20
21 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
22 getEnpassantFen() {
23 return this.epSquares[this.epSquares.length - 1].map(
24 epsq => epsq === undefined
25 ? "-" //no en-passant
26 : V.CoordsToSquare(epsq)
27 ).join(",");
28 }
29
30 setOtherVariables(fen) {
31 const parsedFen = V.ParseFen(fen);
32 this.setFlags(parsedFen.flags);
33 this.epSquares = [parsedFen.enpassant.split(",").map(sq => {
34 if (sq != "-") return V.SquareToCoords(sq);
35 return undefined;
36 })];
37 this.scanKings(fen);
38 // Extract subTurn from turn indicator: "w" (first move), or
39 // "w1" or "w2" white subturn 1 or 2, and same for black
40 const fullTurn = V.ParseFen(fen).turn;
41 this.turn = fullTurn[0];
42 // At move 1, the subTurn doesn't need to be specified:
43 this.subTurn = fullTurn[1] || 1;
44 }
45
46 getEnpassantCaptures([x, y], shiftX) {
47 let moves = [];
48 // En passant: always OK if subturn 1,
49 // OK on subturn 2 only if enPassant was played at subturn 1
50 // (and if there are two e.p. squares available).
51 const Lep = this.epSquares.length;
52 const epSquares = this.epSquares[Lep - 1]; //always at least one element
53 let epSqs = [];
54 epSquares.forEach(sq => {
55 if (sq) epSqs.push(sq);
56 });
57 if (epSqs.length == 0) return moves;
58 const oppCol = V.GetOppCol(this.getColor(x, y));
59 for (let sq of epSqs) {
60 if (
61 this.subTurn == 1 ||
62 (epSqs.length == 2 &&
63 // Was this en-passant capture already played at subturn 1 ?
64 // (Or maybe the opponent filled the en-passant square with a piece)
65 this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY)
66 ) {
67 if (
68 sq.x == x + shiftX &&
69 Math.abs(sq.y - y) == 1 &&
70 // Add condition "enemy pawn must be present"
71 this.getPiece(x, sq.y) == V.PAWN &&
72 this.getColor(x, sq.y) == oppCol
73 ) {
74 let epMove = this.getBasicMove([x, y], [sq.x, sq.y]);
75 epMove.vanish.push({
76 x: x,
77 y: sq.y,
78 p: "p",
79 c: oppCol
80 });
81 moves.push(epMove);
82 }
83 }
84 }
85 return moves;
86 }
87
88 play(move) {
89 move.flags = JSON.stringify(this.aggregateFlags());
90 move.turn = this.turn + this.subTurn;
91 V.PlayOnBoard(this.board, move);
92 const epSq = this.getEpSquare(move);
93 if (this.movesCount == 0) {
94 // First move in game
95 this.turn = "b";
96 this.epSquares.push([epSq]);
97 this.movesCount = 1;
98 }
99 // Does this move give check on subturn 1? If yes, skip subturn 2
100 else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) {
101 this.turn = V.GetOppCol(this.turn);
102 this.epSquares.push([epSq]);
103 move.checkOnSubturn1 = true;
104 this.movesCount++;
105 } else {
106 if (this.subTurn == 2) {
107 this.turn = V.GetOppCol(this.turn);
108 let lastEpsq = this.epSquares[this.epSquares.length - 1];
109 lastEpsq.push(epSq);
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.charAt(0);
121 const piece = move.vanish[0].p;
122 const firstRank = c == "w" ? V.size.x - 1 : 0;
123
124 if (piece == V.KING && move.appear.length > 0) {
125 this.kingPos[c][0] = move.appear[0].x;
126 this.kingPos[c][1] = move.appear[0].y;
127 if (V.HasCastle) 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 } else if (
139 move.end.x == oppFirstRank && //we took opponent rook?
140 this.castleFlags[oppCol].includes(move.end.y)
141 ) {
142 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
143 this.castleFlags[oppCol][flagIdx] = V.size.y;
144 }
145 }
146
147 undo(move) {
148 this.disaggregateFlags(JSON.parse(move.flags));
149 V.UndoOnBoard(this.board, move);
150 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
151 // The move may not be full, but is fully undone:
152 this.epSquares.pop();
153 // Moves counter was just incremented:
154 this.movesCount--;
155 } else {
156 // Undo the second half of a move
157 let lastEpsq = this.epSquares[this.epSquares.length - 1];
158 lastEpsq.pop();
159 }
160 this.turn = move.turn[0];
161 this.subTurn = parseInt(move.turn[1]);
162 super.postUndo(move);
163 }
164
165 // NOTE: GenRandInitFen() is OK,
166 // since at first move turn indicator is just "w"
167
168 static get VALUES() {
169 return {
170 p: 1,
171 r: 5,
172 n: 3,
173 b: 3,
174 q: 7, //slightly less than in orthodox game
175 k: 1000
176 };
177 }
178
179 // No alpha-beta here, just adapted min-max at depth 2(+1)
180 getComputerMove() {
181 const maxeval = V.INFINITY;
182 const color = this.turn;
183 const oppCol = V.GetOppCol(this.turn);
184
185 // Search best (half) move for opponent turn
186 const getBestMoveEval = () => {
187 let score = this.getCurrentScore();
188 if (score != "*") {
189 if (score == "1/2") return 0;
190 return maxeval * (score == "1-0" ? 1 : -1);
191 }
192 let moves = this.getAllValidMoves();
193 let res = oppCol == "w" ? -maxeval : maxeval;
194 for (let m of moves) {
195 this.play(m);
196 score = this.getCurrentScore();
197 // Now turn is oppCol,2 if m doesn't give check
198 // Otherwise it's color,1. In both cases the next test makes sense
199 if (score != "*") {
200 if (score == "1/2")
201 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
202 else {
203 // Found a mate
204 this.undo(m);
205 return maxeval * (score == "1-0" ? 1 : -1);
206 }
207 }
208 const evalPos = this.evalPosition();
209 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
210 this.undo(m);
211 }
212 return res;
213 };
214
215 let moves11 = this.getAllValidMoves();
216 let doubleMoves = [];
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 doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() });
223 } else {
224 let moves12 = this.getAllValidMoves();
225 for (let j = 0; j < moves12.length; j++) {
226 this.play(moves12[j]);
227 doubleMoves.push({
228 moves: [moves11[i], moves12[j]],
229 eval: getBestMoveEval()
230 });
231 this.undo(moves12[j]);
232 }
233 }
234 this.undo(moves11[i]);
235 }
236
237 doubleMoves.sort((a, b) => {
238 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
239 });
240 let candidates = [0]; //indices of candidates moves
241 for (
242 let i = 1;
243 i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
244 i++
245 ) {
246 candidates.push(i);
247 }
248
249 const selected = doubleMoves[randInt(candidates.length)].moves;
250 if (selected.length == 1) return selected[0];
251 return selected;
252 }
253 };