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 finalPieces
= x
+ shiftX
== lastRank
74 ? [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
]
78 if (this.board
[x
+shiftX
][y
] == V
.EMPTY
)
80 for (let piece
of finalPieces
)
82 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
],
85 // Next condition because pawns on 1st rank can generally jump
86 if ([startRank
,firstRank
].includes(x
)
87 && this.board
[x
+2*shiftX
][y
] == V
.EMPTY
)
90 moves
.push(this.getBasicMove([x
,y
], [x
+2*shiftX
,y
]));
94 for (let shiftY
of [-1,1])
96 if (y
+ shiftY
>= 0 && y
+ shiftY
< sizeY
97 && this.board
[x
+shiftX
][y
+shiftY
] != V
.EMPTY
98 && this.canTake([x
,y
], [x
+shiftX
,y
+shiftY
]))
100 for (let piece
of finalPieces
)
102 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
+shiftY
],
108 // En passant: always OK if subturn 1,
109 // OK on subturn 2 only if enPassant was played at subturn 1
110 // (and if there are two e.p. squares available).
111 const Lep
= this.epSquares
.length
;
112 const epSquares
= this.epSquares
[Lep
-1]; //always at least one element
114 epSquares
.forEach(sq
=> {
118 if (epSqs
.length
== 0)
120 const oppCol
= this.getOppCol(color
);
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
129 // Add condition "enemy pawn must be present"
130 && this.getPiece(x
,sq
.y
) == V
.PAWN
&& this.getColor(x
,sq
.y
) == oppCol
)
132 let epMove
= this.getBasicMove([x
,y
], [sq
.x
,sq
.y
]);
151 move.notation
= [this.getNotation(move), this.getLongNotation(move)];
152 // In this special case, we also need the "move color":
153 move.color
= this.turn
;
155 move.flags
= JSON
.stringify(this.aggregateFlags());
156 V
.PlayOnBoard(this.board
, move);
157 const epSq
= this.getEpSquare(move);
158 if (this.startAtFirstMove
&& this.moves
.length
== 0)
161 this.epSquares
.push([epSq
]);
163 // Does this move give check on subturn 1? If yes, skip subturn 2
164 else if (this.subTurn
==1 && this.underCheck(this.getOppCol(this.turn
)))
166 this.turn
= this.getOppCol(this.turn
);
167 this.epSquares
.push([epSq
]);
168 move.checkOnSubturn1
= true;
172 if (this.subTurn
== 2)
174 this.turn
= this.getOppCol(this.turn
);
175 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
179 this.epSquares
.push([epSq
]);
180 this.subTurn
= 3 - this.subTurn
;
182 this.moves
.push(move);
183 this.updateVariables(move);
185 move.hash
= hex_md5(this.getFen());
190 this.disaggregateFlags(JSON
.parse(move.flags
));
191 V
.UndoOnBoard(this.board
, move);
192 if (this.startAtFirstMove
&& this.moves
.length
== 1)
195 this.epSquares
.pop();
197 else if (move.checkOnSubturn1
)
199 this.turn
= this.getOppCol(this.turn
);
201 this.epSquares
.pop();
205 if (this.subTurn
== 1)
207 this.turn
= this.getOppCol(this.turn
);
208 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
212 this.epSquares
.pop();
213 this.subTurn
= 3 - this.subTurn
;
216 this.unupdateVariables(move);
219 // NOTE: GenRandInitFen() is OK,
220 // since at first move turn indicator is just "w"
229 'q': 7, //slightly less than in orthodox game
234 // No alpha-beta here, just adapted min-max at depth 2(+1)
237 if (this.subTurn
== 2)
238 return null; //TODO: imperfect interface setup
240 const maxeval
= V
.INFINITY
;
241 const color
= this.turn
;
242 const oppCol
= this.getOppCol(this.turn
);
244 // Search best (half) move for opponent turn
245 const getBestMoveEval
= () => {
246 const turnBefore
= this.turn
+ this.subTurn
;
247 let moves
= this.getAllValidMoves();
248 if (moves
.length
== 0)
250 const score
= this.checkGameEnd();
253 return maxeval
* (score
== "1-0" ? 1 : -1);
255 let res
= (oppCol
== "w" ? -maxeval : maxeval
);
259 // Now turn is oppCol,2 if m doesn't give check
260 // Otherwise it's color,1. In both cases the next test makes sense
261 if (!this.atLeastOneMove())
263 const score
= this.checkGameEnd();
265 res
= (oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0));
270 return maxeval
* (score
== "1-0" ? 1 : -1);
273 const evalPos
= this.evalPosition();
274 res
= (oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
));
280 let moves11
= this.getAllValidMoves();
281 let doubleMoves
= [];
282 // Rank moves using a min-max at depth 2
283 for (let i
=0; i
<moves11
.length
; i
++)
285 this.play(moves11
[i
]);
286 if (this.turn
!= color
)
288 // We gave check with last move: search the best opponent move
289 doubleMoves
.push({moves:[moves11
[i
]], eval:getBestMoveEval()});
293 let moves12
= this.getAllValidMoves();
294 for (let j
=0; j
<moves12
.length
; j
++)
296 this.play(moves12
[j
]);
298 moves:[moves11
[i
],moves12
[j
]],
299 eval:getBestMoveEval()});
300 this.undo(moves12
[j
]);
303 this.undo(moves11
[i
]);
306 doubleMoves
.sort( (a
,b
) => {
307 return (color
=="w" ? 1 : -1) * (b
.eval
- a
.eval
); });
308 let candidates
= [0]; //indices of candidates moves
310 i
<doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
316 const selected
= doubleMoves
[_
.sample(candidates
, 1)].moves
;
317 if (selected
.length
== 1)
322 getPGN(mycolor
, score
, fenStart
, mode
)
325 pgn
+= '[Site "vchess.club"]\n';
326 const opponent
= mode
=="human" ? "Anonymous" : "Computer";
327 pgn
+= '[Variant "' + variant
+ '"]\n';
328 pgn
+= '[Date "' + getDate(new Date()) + '"]\n';
329 const whiteName
= ["human","computer"].includes(mode
)
330 ? (mycolor
=='w'?'Myself':opponent
)
332 const blackName
= ["human","computer"].includes(mode
)
333 ? (mycolor
=='b'?'Myself':opponent
)
335 pgn
+= '[White "' + whiteName
+ '"]\n';
336 pgn
+= '[Black "' + blackName
+ '"]\n';
337 pgn
+= '[FenStart "' + fenStart
+ '"]\n';
338 pgn
+= '[Fen "' + this.getFen() + '"]\n';
339 pgn
+= '[Result "' + score
+ '"]\n\n';
343 while (i
< this.moves
.length
)
345 pgn
+= (counter
++) + ".";
346 for (let color
of ["w","b"])
349 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
350 move += this.moves
[i
++].notation
[0] + ",";
351 move = move.slice(0,-1); //remove last comma
352 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
357 // "Complete moves" PGN (helping in ambiguous cases)
360 while (i
< this.moves
.length
)
362 pgn
+= (counter
++) + ".";
363 for (let color
of ["w","b"])
366 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
367 move += this.moves
[i
++].notation
[1] + ",";
368 move = move.slice(0,-1); //remove last comma
369 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
377 const VariantRules
= MarseilleRules
;