Failed attempt for marseilleChess with a template: back to render function
[vchess.git] / client / src / variants / Marseille.js
CommitLineData
829f6574
BA
1import { ChessRules } from "@/base_rules";
2
3export const VariantRules = class MarseilleRules extends ChessRules
4f7723a1 4{
6e62b1c7
BA
5 static IsGoodEnpassant(enpassant)
6 {
7 if (enpassant != "-")
8 {
9 const squares = enpassant.split(",");
10 if (squares.length > 2)
11 return false;
12 for (let sq of squares)
13 {
14 const ep = V.SquareToCoords(sq);
15 if (isNaN(ep.x) || !V.OnBoard(ep))
16 return false;
17 }
18 }
19 return true;
20 }
21
22 getTurnFen()
23 {
6e62b1c7
BA
24 return this.turn + this.subTurn;
25 }
26
27 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
28 getEnpassantFen()
29 {
30 const L = this.epSquares.length;
31 if (this.epSquares[L-1].every(epsq => epsq === undefined))
32 return "-"; //no en-passant
33 let res = "";
34 this.epSquares[L-1].forEach(epsq => {
35 if (!!epsq)
36 res += V.CoordsToSquare(epsq) + ",";
37 });
38 return res.slice(0,-1); //remove last comma
39 }
40
41 setOtherVariables(fen)
42 {
43 const parsedFen = V.ParseFen(fen);
44 this.setFlags(parsedFen.flags);
45 if (parsedFen.enpassant == "-")
51882959 46 this.epSquares = [ [undefined] ];
6e62b1c7
BA
47 else
48 {
49 let res = [];
50 const squares = parsedFen.enpassant.split(",");
51 for (let sq of squares)
52 res.push(V.SquareToCoords(sq));
6e62b1c7
BA
53 this.epSquares = [ res ];
54 }
55 this.scanKingsRooks(fen);
56 // Extract subTurn from turn indicator: "w" (first move), or
57 // "w1" or "w2" white subturn 1 or 2, and same for black
58 const fullTurn = V.ParseFen(fen).turn;
6e62b1c7 59 this.turn = fullTurn[0];
7d9e99bc 60 this.subTurn = (fullTurn[1] || 0); //"w0" = special code for first move in game
6e62b1c7
BA
61 }
62
63 getPotentialPawnMoves([x,y])
64 {
65 const color = this.turn;
66 let moves = [];
67 const [sizeX,sizeY] = [V.size.x,V.size.y];
68 const shiftX = (color == "w" ? -1 : 1);
69 const firstRank = (color == 'w' ? sizeX-1 : 0);
70 const startRank = (color == "w" ? sizeX-2 : 1);
71 const lastRank = (color == "w" ? 0 : sizeX-1);
69f3d801
BA
72 const finalPieces = x + shiftX == lastRank
73 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
74 : [V.PAWN];
6e62b1c7 75
69f3d801
BA
76 // One square forward
77 if (this.board[x+shiftX][y] == V.EMPTY)
6e62b1c7 78 {
69f3d801 79 for (let piece of finalPieces)
6e62b1c7 80 {
69f3d801 81 moves.push(this.getBasicMove([x,y], [x+shiftX,y],
edcd679a 82 {c:color,p:piece}));
6e62b1c7 83 }
69f3d801
BA
84 // Next condition because pawns on 1st rank can generally jump
85 if ([startRank,firstRank].includes(x)
86 && this.board[x+2*shiftX][y] == V.EMPTY)
6e62b1c7 87 {
69f3d801
BA
88 // Two squares jump
89 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
90 }
91 }
92 // Captures
93 for (let shiftY of [-1,1])
94 {
95 if (y + shiftY >= 0 && y + shiftY < sizeY
96 && this.board[x+shiftX][y+shiftY] != V.EMPTY
97 && this.canTake([x,y], [x+shiftX,y+shiftY]))
98 {
99 for (let piece of finalPieces)
6e62b1c7 100 {
69f3d801 101 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
edcd679a 102 {c:color,p:piece}));
6e62b1c7
BA
103 }
104 }
105 }
106
107 // En passant: always OK if subturn 1,
108 // OK on subturn 2 only if enPassant was played at subturn 1
109 // (and if there are two e.p. squares available).
110 const Lep = this.epSquares.length;
111 const epSquares = this.epSquares[Lep-1]; //always at least one element
112 let epSqs = [];
113 epSquares.forEach(sq => {
114 if (!!sq)
115 epSqs.push(sq);
116 });
117 if (epSqs.length == 0)
118 return moves;
26b8e4f7 119 const oppCol = V.GetOppCol(color);
6e62b1c7
BA
120 for (let sq of epSqs)
121 {
122 if (this.subTurn == 1 || (epSqs.length == 2 &&
123 // Was this en-passant capture already played at subturn 1 ?
69f3d801 124 // (Or maybe the opponent filled the en-passant square with a piece)
6e62b1c7
BA
125 this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY))
126 {
edcd679a
BA
127 if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1
128 // Add condition "enemy pawn must be present"
129 && this.getPiece(x,sq.y) == V.PAWN && this.getColor(x,sq.y) == oppCol)
6e62b1c7
BA
130 {
131 let epMove = this.getBasicMove([x,y], [sq.x,sq.y]);
132 epMove.vanish.push({
133 x: x,
134 y: sq.y,
135 p: 'p',
edcd679a 136 c: oppCol
6e62b1c7
BA
137 });
138 moves.push(epMove);
139 }
140 }
141 }
142
143 return moves;
144 }
145
7d9e99bc 146 play(move)
6e62b1c7 147 {
6e62b1c7 148 move.flags = JSON.stringify(this.aggregateFlags());
829f6574 149 move.turn = this.turn + this.subTurn;
6e62b1c7 150 V.PlayOnBoard(this.board, move);
51882959 151 const epSq = this.getEpSquare(move);
7d9e99bc 152 if (this.subTurn == 0) //first move in game
51882959 153 {
6e62b1c7 154 this.turn = "b";
829f6574 155 this.subTurn = 1;
51882959
BA
156 this.epSquares.push([epSq]);
157 }
6e62b1c7 158 // Does this move give check on subturn 1? If yes, skip subturn 2
26b8e4f7 159 else if (this.subTurn==1 && this.underCheck(V.GetOppCol(this.turn)))
6e62b1c7 160 {
26b8e4f7 161 this.turn = V.GetOppCol(this.turn);
51882959 162 this.epSquares.push([epSq]);
6e62b1c7
BA
163 move.checkOnSubturn1 = true;
164 }
165 else
166 {
167 if (this.subTurn == 2)
51882959 168 {
26b8e4f7 169 this.turn = V.GetOppCol(this.turn);
51882959
BA
170 let lastEpsq = this.epSquares[this.epSquares.length-1];
171 lastEpsq.push(epSq);
172 }
173 else
174 this.epSquares.push([epSq]);
6e62b1c7
BA
175 this.subTurn = 3 - this.subTurn;
176 }
6e62b1c7 177 this.updateVariables(move);
6e62b1c7
BA
178 }
179
180 undo(move)
181 {
182 this.disaggregateFlags(JSON.parse(move.flags));
6e62b1c7 183 V.UndoOnBoard(this.board, move);
7d9e99bc 184 if (move.turn[1] == '0' || move.checkOnSubturn1 || this.subTurn == 2)
51882959 185 this.epSquares.pop();
7d9e99bc 186 else //this.subTurn == 1
6e62b1c7 187 {
7d9e99bc
BA
188 let lastEpsq = this.epSquares[this.epSquares.length-1];
189 lastEpsq.pop();
6e62b1c7 190 }
7d9e99bc
BA
191 this.turn = move.turn[0];
192 this.subTurn = parseInt(move.turn[1]);
6e62b1c7 193 this.unupdateVariables(move);
6e62b1c7
BA
194 }
195
196 // NOTE: GenRandInitFen() is OK,
197 // since at first move turn indicator is just "w"
198
0596f5e7
BA
199 static get VALUES()
200 {
201 return {
202 'p': 1,
203 'r': 5,
204 'n': 3,
205 'b': 3,
206 'q': 7, //slightly less than in orthodox game
207 'k': 1000
208 };
209 }
210
0c3fe8a6 211 // TODO: this is wrong: revise following base_rules.getComputerMove()
6e62b1c7
BA
212 // No alpha-beta here, just adapted min-max at depth 2(+1)
213 getComputerMove()
214 {
78bab51e
BA
215 if (this.subTurn == 2)
216 return null; //TODO: imperfect interface setup
217
6e62b1c7
BA
218 const maxeval = V.INFINITY;
219 const color = this.turn;
26b8e4f7 220 const oppCol = V.GetOppCol(this.turn);
51882959 221
6e62b1c7
BA
222 // Search best (half) move for opponent turn
223 const getBestMoveEval = () => {
51882959 224 const turnBefore = this.turn + this.subTurn;
6e62b1c7
BA
225 let moves = this.getAllValidMoves();
226 if (moves.length == 0)
227 {
0c3fe8a6 228 const score = this.getCurrentScore();
6e62b1c7
BA
229 if (score == "1/2")
230 return 0;
231 return maxeval * (score == "1-0" ? 1 : -1);
232 }
233 let res = (oppCol == "w" ? -maxeval : maxeval);
234 for (let m of moves)
235 {
236 this.play(m);
51882959
BA
237 // Now turn is oppCol,2 if m doesn't give check
238 // Otherwise it's color,1. In both cases the next test makes sense
6e62b1c7
BA
239 if (!this.atLeastOneMove())
240 {
0c3fe8a6 241 const score = this.getCurrentScore();
6e62b1c7
BA
242 if (score == "1/2")
243 res = (oppCol == "w" ? Math.max(res, 0) : Math.min(res, 0));
244 else
245 {
246 // Found a mate
6e62b1c7
BA
247 this.undo(m);
248 return maxeval * (score == "1-0" ? 1 : -1);
249 }
250 }
251 const evalPos = this.evalPosition();
252 res = (oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos));
6e62b1c7
BA
253 this.undo(m);
254 }
255 return res;
256 };
257
258 let moves11 = this.getAllValidMoves();
259 let doubleMoves = [];
260 // Rank moves using a min-max at depth 2
261 for (let i=0; i<moves11.length; i++)
262 {
6e62b1c7
BA
263 this.play(moves11[i]);
264 if (this.turn != color)
265 {
266 // We gave check with last move: search the best opponent move
267 doubleMoves.push({moves:[moves11[i]], eval:getBestMoveEval()});
268 }
269 else
270 {
271 let moves12 = this.getAllValidMoves();
272 for (let j=0; j<moves12.length; j++)
273 {
274 this.play(moves12[j]);
275 doubleMoves.push({
276 moves:[moves11[i],moves12[j]],
277 eval:getBestMoveEval()});
278 this.undo(moves12[j]);
279 }
280 }
281 this.undo(moves11[i]);
282 }
283
284 doubleMoves.sort( (a,b) => {
285 return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
286 let candidates = [0]; //indices of candidates moves
287 for (let i=1;
288 i<doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval;
289 i++)
290 {
291 candidates.push(i);
292 }
78bab51e 293
8d61fc4a 294 const selected = doubleMoves[sample(candidates)].moves;
6e62b1c7
BA
295 if (selected.length == 1)
296 return selected[0];
297 return selected;
298 }
4f7723a1 299}