Add Checkered1 + fix last move highlights
[vchess.git] / client / src / variants / Marseille.js
CommitLineData
829f6574 1import { ChessRules } from "@/base_rules";
5fde3a01 2import { randInt } from "@/utils/alea";
829f6574 3
32f6285e 4export class MarseilleRules 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);
dac39588
BA
34 // Extract subTurn from turn indicator: "w" (first move), or
35 // "w1" or "w2" white subturn 1 or 2, and same for black
af34341d
BA
36 this.turn = parsedFen.turn;
37 this.subTurn = 1;
dac39588 38 }
6e62b1c7 39
32f6285e 40 getEnpassantCaptures([x, y], shiftX) {
dac39588 41 let moves = [];
dac39588
BA
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;
6808d7a1 46 const epSquares = this.epSquares[Lep - 1]; //always at least one element
dac39588
BA
47 let epSqs = [];
48 epSquares.forEach(sq => {
6808d7a1 49 if (sq) epSqs.push(sq);
dac39588 50 });
6808d7a1 51 if (epSqs.length == 0) return moves;
32f6285e 52 const oppCol = V.GetOppCol(this.getColor(x, y));
6808d7a1
BA
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 &&
dac39588 64 // Add condition "enemy pawn must be present"
6808d7a1
BA
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]);
dac39588
BA
69 epMove.vanish.push({
70 x: x,
71 y: sq.y,
6808d7a1 72 p: "p",
dac39588
BA
73 c: oppCol
74 });
75 moves.push(epMove);
76 }
77 }
78 }
dac39588
BA
79 return moves;
80 }
6e62b1c7 81
6808d7a1 82 play(move) {
dac39588
BA
83 move.flags = JSON.stringify(this.aggregateFlags());
84 move.turn = this.turn + this.subTurn;
85 V.PlayOnBoard(this.board, move);
86 const epSq = this.getEpSquare(move);
9842aca2
BA
87 if (this.movesCount == 0) {
88 // First move in game
dac39588 89 this.turn = "b";
dac39588 90 this.epSquares.push([epSq]);
9842aca2 91 this.movesCount = 1;
dac39588
BA
92 }
93 // Does this move give check on subturn 1? If yes, skip subturn 2
6808d7a1 94 else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) {
dac39588
BA
95 this.turn = V.GetOppCol(this.turn);
96 this.epSquares.push([epSq]);
97 move.checkOnSubturn1 = true;
9842aca2 98 this.movesCount++;
6808d7a1
BA
99 } else {
100 if (this.subTurn == 2) {
dac39588 101 this.turn = V.GetOppCol(this.turn);
6808d7a1 102 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588 103 lastEpsq.push(epSq);
9842aca2
BA
104 } else {
105 this.epSquares.push([epSq]);
106 this.movesCount++;
107 }
dac39588
BA
108 this.subTurn = 3 - this.subTurn;
109 }
3a2a7b5f
BA
110 this.postPlay(move);
111 }
112
113 postPlay(move) {
114 const c = move.turn.charAt(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;
0d5335de 121 this.castleFlags[c] = [V.size.y, V.size.y];
3a2a7b5f
BA
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 } else if (
133 move.end.x == oppFirstRank && //we took opponent rook?
134 this.castleFlags[oppCol].includes(move.end.y)
135 ) {
136 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
137 this.castleFlags[oppCol][flagIdx] = V.size.y;
138 }
dac39588 139 }
6e62b1c7 140
6808d7a1 141 undo(move) {
dac39588
BA
142 this.disaggregateFlags(JSON.parse(move.flags));
143 V.UndoOnBoard(this.board, move);
9842aca2
BA
144 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
145 // The move may not be full, but is fully undone:
dac39588 146 this.epSquares.pop();
9842aca2
BA
147 // Moves counter was just incremented:
148 this.movesCount--;
149 } else {
150 // Undo the second half of a move
6808d7a1 151 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588
BA
152 lastEpsq.pop();
153 }
154 this.turn = move.turn[0];
155 this.subTurn = parseInt(move.turn[1]);
3a2a7b5f 156 super.postUndo(move);
dac39588 157 }
6e62b1c7 158
dac39588
BA
159 // NOTE: GenRandInitFen() is OK,
160 // since at first move turn indicator is just "w"
6e62b1c7 161
6808d7a1 162 static get VALUES() {
dac39588 163 return {
6808d7a1
BA
164 p: 1,
165 r: 5,
166 n: 3,
167 b: 3,
168 q: 7, //slightly less than in orthodox game
169 k: 1000
dac39588
BA
170 };
171 }
0596f5e7 172
dac39588 173 // No alpha-beta here, just adapted min-max at depth 2(+1)
6808d7a1 174 getComputerMove() {
dac39588
BA
175 const maxeval = V.INFINITY;
176 const color = this.turn;
177 const oppCol = V.GetOppCol(this.turn);
51882959 178
dac39588
BA
179 // Search best (half) move for opponent turn
180 const getBestMoveEval = () => {
dac39588 181 let score = this.getCurrentScore();
6808d7a1
BA
182 if (score != "*") {
183 if (score == "1/2") return 0;
dac39588
BA
184 return maxeval * (score == "1-0" ? 1 : -1);
185 }
186 let moves = this.getAllValidMoves();
6808d7a1
BA
187 let res = oppCol == "w" ? -maxeval : maxeval;
188 for (let m of moves) {
dac39588
BA
189 this.play(m);
190 score = this.getCurrentScore();
191 // Now turn is oppCol,2 if m doesn't give check
192 // Otherwise it's color,1. In both cases the next test makes sense
6808d7a1 193 if (score != "*") {
dac39588 194 if (score == "1/2")
6808d7a1
BA
195 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
196 else {
dac39588
BA
197 // Found a mate
198 this.undo(m);
199 return maxeval * (score == "1-0" ? 1 : -1);
200 }
201 }
202 const evalPos = this.evalPosition();
6808d7a1 203 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
dac39588
BA
204 this.undo(m);
205 }
206 return res;
207 };
6e62b1c7 208
dac39588
BA
209 let moves11 = this.getAllValidMoves();
210 let doubleMoves = [];
211 // Rank moves using a min-max at depth 2
6808d7a1 212 for (let i = 0; i < moves11.length; i++) {
dac39588 213 this.play(moves11[i]);
6808d7a1 214 if (this.turn != color) {
dac39588 215 // We gave check with last move: search the best opponent move
6808d7a1
BA
216 doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() });
217 } else {
dac39588 218 let moves12 = this.getAllValidMoves();
6808d7a1 219 for (let j = 0; j < moves12.length; j++) {
dac39588
BA
220 this.play(moves12[j]);
221 doubleMoves.push({
6808d7a1
BA
222 moves: [moves11[i], moves12[j]],
223 eval: getBestMoveEval()
224 });
dac39588
BA
225 this.undo(moves12[j]);
226 }
227 }
228 this.undo(moves11[i]);
229 }
6e62b1c7 230
6808d7a1
BA
231 doubleMoves.sort((a, b) => {
232 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
233 });
dac39588 234 let candidates = [0]; //indices of candidates moves
6808d7a1
BA
235 for (
236 let i = 1;
237 i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
238 i++
239 ) {
dac39588
BA
240 candidates.push(i);
241 }
78bab51e 242
dac39588 243 const selected = doubleMoves[randInt(candidates.length)].moves;
6808d7a1 244 if (selected.length == 1) return selected[0];
dac39588
BA
245 return selected;
246 }
6808d7a1 247};