1 class MarseilleRules
extends ChessRules
3 static IsGoodEnpassant(enpassant
)
7 const squares
= enpassant
.split(",");
8 if (squares
.length
> 2)
10 for (let sq
of squares
)
12 const ep
= V
.SquareToCoords(sq
);
13 if (isNaN(ep
.x
) || !V
.OnBoard(ep
))
22 if (this.startAtFirstMove
&& this.moves
.length
==0)
24 return this.turn
+ this.subTurn
;
27 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
30 const L
= this.epSquares
.length
;
31 if (this.epSquares
[L
-1].every(epsq
=> epsq
=== undefined))
32 return "-"; //no en-passant
34 this.epSquares
[L
-1].forEach(epsq
=> {
36 res
+= V
.CoordsToSquare(epsq
) + ",";
38 return res
.slice(0,-1); //remove last comma
41 setOtherVariables(fen
)
43 const parsedFen
= V
.ParseFen(fen
);
44 this.setFlags(parsedFen
.flags
);
45 if (parsedFen
.enpassant
== "-")
46 this.epSquares
= [ [undefined] ];
50 const squares
= parsedFen
.enpassant
.split(",");
51 for (let sq
of squares
)
52 res
.push(V
.SquareToCoords(sq
));
53 this.epSquares
= [ res
];
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
;
59 this.startAtFirstMove
= (fullTurn
== "w");
60 this.turn
= fullTurn
[0];
61 this.subTurn
= (fullTurn
[1] || 1);
64 getPotentialPawnMoves([x
,y
])
66 const color
= this.turn
;
68 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
69 const shiftX
= (color
== "w" ? -1 : 1);
70 const firstRank
= (color
== 'w' ? sizeX
-1 : 0);
71 const startRank
= (color
== "w" ? sizeX
-2 : 1);
72 const lastRank
= (color
== "w" ? 0 : sizeX
-1);
73 const pawnColor
= this.getColor(x
,y
); //can be different for checkered
74 const finalPieces
= x
+ shiftX
== lastRank
75 ? [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
]
79 if (this.board
[x
+shiftX
][y
] == V
.EMPTY
)
81 for (let piece
of finalPieces
)
83 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
],
84 {c:pawnColor
,p:piece
}));
86 // Next condition because pawns on 1st rank can generally jump
87 if ([startRank
,firstRank
].includes(x
)
88 && this.board
[x
+2*shiftX
][y
] == V
.EMPTY
)
91 moves
.push(this.getBasicMove([x
,y
], [x
+2*shiftX
,y
]));
95 for (let shiftY
of [-1,1])
97 if (y
+ shiftY
>= 0 && y
+ shiftY
< sizeY
98 && this.board
[x
+shiftX
][y
+shiftY
] != V
.EMPTY
99 && this.canTake([x
,y
], [x
+shiftX
,y
+shiftY
]))
101 for (let piece
of finalPieces
)
103 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
+shiftY
],
104 {c:pawnColor
,p:piece
}));
109 // En passant: always OK if subturn 1,
110 // OK on subturn 2 only if enPassant was played at subturn 1
111 // (and if there are two e.p. squares available).
112 const Lep
= this.epSquares
.length
;
113 const epSquares
= this.epSquares
[Lep
-1]; //always at least one element
115 epSquares
.forEach(sq
=> {
119 if (epSqs
.length
== 0)
121 for (let sq
of epSqs
)
123 if (this.subTurn
== 1 || (epSqs
.length
== 2 &&
124 // Was this en-passant capture already played at subturn 1 ?
125 // (Or maybe the opponent filled the en-passant square with a piece)
126 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
))
128 if (sq
.x
== x
+shiftX
&& Math
.abs(sq
.y
- y
) == 1)
130 let epMove
= this.getBasicMove([x
,y
], [sq
.x
,sq
.y
]);
135 c: this.getColor(x
,sq
.y
)
149 move.notation
= [this.getNotation(move), this.getLongNotation(move)];
150 // In this special case, we also need the "move color":
151 move.color
= this.turn
;
153 move.flags
= JSON
.stringify(this.aggregateFlags());
154 V
.PlayOnBoard(this.board
, move);
155 const epSq
= this.getEpSquare(move);
156 if (this.startAtFirstMove
&& this.moves
.length
== 0)
159 this.epSquares
.push([epSq
]);
161 // Does this move give check on subturn 1? If yes, skip subturn 2
162 else if (this.subTurn
==1 && this.underCheck(this.getOppCol(this.turn
)))
164 this.turn
= this.getOppCol(this.turn
);
165 this.epSquares
.push([epSq
]);
166 move.checkOnSubturn1
= true;
170 if (this.subTurn
== 2)
172 this.turn
= this.getOppCol(this.turn
);
173 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
177 this.epSquares
.push([epSq
]);
178 this.subTurn
= 3 - this.subTurn
;
180 this.moves
.push(move);
181 this.updateVariables(move);
183 move.hash
= hex_md5(this.getFen());
188 this.disaggregateFlags(JSON
.parse(move.flags
));
189 V
.UndoOnBoard(this.board
, move);
190 if (this.startAtFirstMove
&& this.moves
.length
== 1)
193 this.epSquares
.pop();
195 else if (move.checkOnSubturn1
)
197 this.turn
= this.getOppCol(this.turn
);
199 this.epSquares
.pop();
203 if (this.subTurn
== 1)
205 this.turn
= this.getOppCol(this.turn
);
206 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
210 this.epSquares
.pop();
211 this.subTurn
= 3 - this.subTurn
;
214 this.unupdateVariables(move);
217 // NOTE: GenRandInitFen() is OK,
218 // since at first move turn indicator is just "w"
220 // No alpha-beta here, just adapted min-max at depth 2(+1)
223 if (this.subTurn
== 2)
224 return null; //TODO: imperfect interface setup
226 const maxeval
= V
.INFINITY
;
227 const color
= this.turn
;
228 const oppCol
= this.getOppCol(this.turn
);
230 // Search best (half) move for opponent turn
231 const getBestMoveEval
= () => {
232 const turnBefore
= this.turn
+ this.subTurn
;
233 let moves
= this.getAllValidMoves();
234 if (moves
.length
== 0)
236 const score
= this.checkGameEnd();
239 return maxeval
* (score
== "1-0" ? 1 : -1);
241 let res
= (oppCol
== "w" ? -maxeval : maxeval
);
245 // Now turn is oppCol,2 if m doesn't give check
246 // Otherwise it's color,1. In both cases the next test makes sense
247 if (!this.atLeastOneMove())
249 const score
= this.checkGameEnd();
251 res
= (oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0));
256 return maxeval
* (score
== "1-0" ? 1 : -1);
259 const evalPos
= this.evalPosition();
260 res
= (oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
));
266 let moves11
= this.getAllValidMoves();
267 let doubleMoves
= [];
268 // Rank moves using a min-max at depth 2
269 for (let i
=0; i
<moves11
.length
; i
++)
271 this.play(moves11
[i
]);
272 if (this.turn
!= color
)
274 // We gave check with last move: search the best opponent move
275 doubleMoves
.push({moves:[moves11
[i
]], eval:getBestMoveEval()});
279 let moves12
= this.getAllValidMoves();
280 for (let j
=0; j
<moves12
.length
; j
++)
282 this.play(moves12
[j
]);
284 moves:[moves11
[i
],moves12
[j
]],
285 eval:getBestMoveEval()});
286 this.undo(moves12
[j
]);
289 this.undo(moves11
[i
]);
292 doubleMoves
.sort( (a
,b
) => {
293 return (color
=="w" ? 1 : -1) * (b
.eval
- a
.eval
); });
294 let candidates
= [0]; //indices of candidates moves
296 i
<doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
302 const selected
= doubleMoves
[_
.sample(candidates
, 1)].moves
;
303 if (selected
.length
== 1)
308 getPGN(mycolor
, score
, fenStart
, mode
)
311 pgn
+= '[Site "vchess.club"]<br>';
312 const opponent
= mode
=="human" ? "Anonymous" : "Computer";
313 pgn
+= '[Variant "' + variant
+ '"]<br>';
314 pgn
+= '[Date "' + getDate(new Date()) + '"]<br>';
315 pgn
+= '[White "' + (mycolor
=='w'?'Myself':opponent
) + '"]<br>';
316 pgn
+= '[Black "' + (mycolor
=='b'?'Myself':opponent
) + '"]<br>';
317 pgn
+= '[FenStart "' + fenStart
+ '"]<br>';
318 pgn
+= '[Fen "' + this.getFen() + '"]<br>';
319 pgn
+= '[Result "' + score
+ '"]<br><br>';
323 while (i
< this.moves
.length
)
325 pgn
+= (counter
++) + ".";
326 for (let color
of ["w","b"])
329 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
330 move += this.moves
[i
++].notation
[0] + ",";
331 move = move.slice(0,-1); //remove last comma
332 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
337 // "Complete moves" PGN (helping in ambiguous cases)
340 while (i
< this.moves
.length
)
342 pgn
+= (counter
++) + ".";
343 for (let color
of ["w","b"])
346 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
347 move += this.moves
[i
++].notation
[1] + ",";
348 move = move.slice(0,-1); //remove last comma
349 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
357 const VariantRules
= MarseilleRules
;