Some other fixes
[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
6808d7a1 17 getTurnFen() {
dac39588
BA
18 return this.turn + this.subTurn;
19 }
6e62b1c7 20
dac39588 21 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
6808d7a1 22 getEnpassantFen() {
e71161fb
BA
23 return this.epSquares[this.epSquares.length - 1].map(
24 epsq => epsq === undefined
25 ? "-" //no en-passant
26 : V.CoordsToSquare(epsq)
27 ).join(",");
dac39588 28 }
6e62b1c7 29
6808d7a1 30 setOtherVariables(fen) {
dac39588
BA
31 const parsedFen = V.ParseFen(fen);
32 this.setFlags(parsedFen.flags);
e71161fb
BA
33 this.epSquares = [parsedFen.enpassant.split(",").map(sq => {
34 if (sq != "-") return V.SquareToCoords(sq);
35 return undefined;
36 })];
3a2a7b5f 37 this.scanKings(fen);
dac39588
BA
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];
9842aca2
BA
42 // At move 1, the subTurn doesn't need to be specified:
43 this.subTurn = fullTurn[1] || 1;
dac39588 44 }
6e62b1c7 45
32f6285e 46 getEnpassantCaptures([x, y], shiftX) {
dac39588 47 let moves = [];
dac39588
BA
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;
6808d7a1 52 const epSquares = this.epSquares[Lep - 1]; //always at least one element
dac39588
BA
53 let epSqs = [];
54 epSquares.forEach(sq => {
6808d7a1 55 if (sq) epSqs.push(sq);
dac39588 56 });
6808d7a1 57 if (epSqs.length == 0) return moves;
32f6285e 58 const oppCol = V.GetOppCol(this.getColor(x, y));
6808d7a1
BA
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 &&
dac39588 70 // Add condition "enemy pawn must be present"
6808d7a1
BA
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]);
dac39588
BA
75 epMove.vanish.push({
76 x: x,
77 y: sq.y,
6808d7a1 78 p: "p",
dac39588
BA
79 c: oppCol
80 });
81 moves.push(epMove);
82 }
83 }
84 }
dac39588
BA
85 return moves;
86 }
6e62b1c7 87
6808d7a1 88 play(move) {
dac39588
BA
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);
9842aca2
BA
93 if (this.movesCount == 0) {
94 // First move in game
dac39588 95 this.turn = "b";
dac39588 96 this.epSquares.push([epSq]);
9842aca2 97 this.movesCount = 1;
dac39588
BA
98 }
99 // Does this move give check on subturn 1? If yes, skip subturn 2
6808d7a1 100 else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) {
dac39588
BA
101 this.turn = V.GetOppCol(this.turn);
102 this.epSquares.push([epSq]);
103 move.checkOnSubturn1 = true;
9842aca2 104 this.movesCount++;
6808d7a1
BA
105 } else {
106 if (this.subTurn == 2) {
dac39588 107 this.turn = V.GetOppCol(this.turn);
6808d7a1 108 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588 109 lastEpsq.push(epSq);
9842aca2
BA
110 } else {
111 this.epSquares.push([epSq]);
112 this.movesCount++;
113 }
dac39588
BA
114 this.subTurn = 3 - this.subTurn;
115 }
3a2a7b5f
BA
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 }
dac39588 145 }
6e62b1c7 146
6808d7a1 147 undo(move) {
dac39588
BA
148 this.disaggregateFlags(JSON.parse(move.flags));
149 V.UndoOnBoard(this.board, move);
9842aca2
BA
150 if (this.movesCount == 1 || !!move.checkOnSubturn1 || this.subTurn == 2) {
151 // The move may not be full, but is fully undone:
dac39588 152 this.epSquares.pop();
9842aca2
BA
153 // Moves counter was just incremented:
154 this.movesCount--;
155 } else {
156 // Undo the second half of a move
6808d7a1 157 let lastEpsq = this.epSquares[this.epSquares.length - 1];
dac39588
BA
158 lastEpsq.pop();
159 }
160 this.turn = move.turn[0];
161 this.subTurn = parseInt(move.turn[1]);
3a2a7b5f 162 super.postUndo(move);
dac39588 163 }
6e62b1c7 164
dac39588
BA
165 // NOTE: GenRandInitFen() is OK,
166 // since at first move turn indicator is just "w"
6e62b1c7 167
6808d7a1 168 static get VALUES() {
dac39588 169 return {
6808d7a1
BA
170 p: 1,
171 r: 5,
172 n: 3,
173 b: 3,
174 q: 7, //slightly less than in orthodox game
175 k: 1000
dac39588
BA
176 };
177 }
0596f5e7 178
dac39588 179 // No alpha-beta here, just adapted min-max at depth 2(+1)
6808d7a1 180 getComputerMove() {
dac39588
BA
181 const maxeval = V.INFINITY;
182 const color = this.turn;
183 const oppCol = V.GetOppCol(this.turn);
51882959 184
dac39588
BA
185 // Search best (half) move for opponent turn
186 const getBestMoveEval = () => {
dac39588 187 let score = this.getCurrentScore();
6808d7a1
BA
188 if (score != "*") {
189 if (score == "1/2") return 0;
dac39588
BA
190 return maxeval * (score == "1-0" ? 1 : -1);
191 }
192 let moves = this.getAllValidMoves();
6808d7a1
BA
193 let res = oppCol == "w" ? -maxeval : maxeval;
194 for (let m of moves) {
dac39588
BA
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
6808d7a1 199 if (score != "*") {
dac39588 200 if (score == "1/2")
6808d7a1
BA
201 res = oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0);
202 else {
dac39588
BA
203 // Found a mate
204 this.undo(m);
205 return maxeval * (score == "1-0" ? 1 : -1);
206 }
207 }
208 const evalPos = this.evalPosition();
6808d7a1 209 res = oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos);
dac39588
BA
210 this.undo(m);
211 }
212 return res;
213 };
6e62b1c7 214
dac39588
BA
215 let moves11 = this.getAllValidMoves();
216 let doubleMoves = [];
217 // Rank moves using a min-max at depth 2
6808d7a1 218 for (let i = 0; i < moves11.length; i++) {
dac39588 219 this.play(moves11[i]);
6808d7a1 220 if (this.turn != color) {
dac39588 221 // We gave check with last move: search the best opponent move
6808d7a1
BA
222 doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() });
223 } else {
dac39588 224 let moves12 = this.getAllValidMoves();
6808d7a1 225 for (let j = 0; j < moves12.length; j++) {
dac39588
BA
226 this.play(moves12[j]);
227 doubleMoves.push({
6808d7a1
BA
228 moves: [moves11[i], moves12[j]],
229 eval: getBestMoveEval()
230 });
dac39588
BA
231 this.undo(moves12[j]);
232 }
233 }
234 this.undo(moves11[i]);
235 }
6e62b1c7 236
6808d7a1
BA
237 doubleMoves.sort((a, b) => {
238 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
239 });
dac39588 240 let candidates = [0]; //indices of candidates moves
6808d7a1
BA
241 for (
242 let i = 1;
243 i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
244 i++
245 ) {
dac39588
BA
246 candidates.push(i);
247 }
78bab51e 248
dac39588 249 const selected = doubleMoves[randInt(candidates.length)].moves;
6808d7a1 250 if (selected.length == 1) return selected[0];
dac39588
BA
251 return selected;
252 }
6808d7a1 253};