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