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,undefined] ];
50 const squares
= parsedFen
.enpassant
.split(",");
51 for (let sq
of squares
)
52 res
.push(V
.SquareToCoords(sq
));
54 res
.push(undefined); //always 2 slots in epSquares[i]
55 this.epSquares
= [ res
];
57 this.scanKingsRooks(fen
);
58 // Extract subTurn from turn indicator: "w" (first move), or
59 // "w1" or "w2" white subturn 1 or 2, and same for black
60 const fullTurn
= V
.ParseFen(fen
).turn
;
61 this.startAtFirstMove
= (fullTurn
== "w");
62 this.turn
= fullTurn
[0];
63 this.subTurn
= (fullTurn
[1] || 1);
66 getPotentialPawnMoves([x
,y
])
68 const color
= this.turn
;
70 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
71 const shiftX
= (color
== "w" ? -1 : 1);
72 const firstRank
= (color
== 'w' ? sizeX
-1 : 0);
73 const startRank
= (color
== "w" ? sizeX
-2 : 1);
74 const lastRank
= (color
== "w" ? 0 : sizeX
-1);
75 const pawnColor
= this.getColor(x
,y
); //can be different for checkered
77 if (x
+shiftX
>= 0 && x
+shiftX
< sizeX
) //TODO: always true
79 const finalPieces
= x
+ shiftX
== lastRank
80 ? [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
]
83 if (this.board
[x
+shiftX
][y
] == V
.EMPTY
)
85 for (let piece
of finalPieces
)
87 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
],
88 {c:pawnColor
,p:piece
}));
90 // Next condition because pawns on 1st rank can generally jump
91 if ([startRank
,firstRank
].includes(x
)
92 && this.board
[x
+2*shiftX
][y
] == V
.EMPTY
)
95 moves
.push(this.getBasicMove([x
,y
], [x
+2*shiftX
,y
]));
99 for (let shiftY
of [-1,1])
101 if (y
+ shiftY
>= 0 && y
+ shiftY
< sizeY
102 && this.board
[x
+shiftX
][y
+shiftY
] != V
.EMPTY
103 && this.canTake([x
,y
], [x
+shiftX
,y
+shiftY
]))
105 for (let piece
of finalPieces
)
107 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
+shiftY
],
108 {c:pawnColor
,p:piece
}));
114 // En passant: always OK if subturn 1,
115 // OK on subturn 2 only if enPassant was played at subturn 1
116 // (and if there are two e.p. squares available).
117 const Lep
= this.epSquares
.length
;
118 const epSquares
= this.epSquares
[Lep
-1]; //always at least one element
120 epSquares
.forEach(sq
=> {
124 if (epSqs
.length
== 0)
126 for (let sq
of epSqs
)
128 if (this.subTurn
== 1 || (epSqs
.length
== 2 &&
129 // Was this en-passant capture already played at subturn 1 ?
130 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
))
132 if (sq
.x
== x
+shiftX
&& Math
.abs(sq
.y
- y
) == 1)
134 let epMove
= this.getBasicMove([x
,y
], [sq
.x
,sq
.y
]);
139 c: this.getColor(x
,sq
.y
)
153 move.notation
= [this.getNotation(move), this.getLongNotation(move)];
154 // In this special case, we also need the "move color":
155 move.color
= this.turn
;
157 move.flags
= JSON
.stringify(this.aggregateFlags());
158 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
159 const epSq
= this.getEpSquare(move);
160 if (lastEpsq
.length
== 1)
165 let newEpsqs
= [epSq
];
166 if (this.startAtFirstMove
&& this.moves
.length
== 0)
167 newEpsqs
.push(undefined); //at first move, to force length==2 (TODO)
168 this.epSquares
.push(newEpsqs
);
170 V
.PlayOnBoard(this.board
, move);
171 if (this.startAtFirstMove
&& this.moves
.length
== 0)
173 // Does this move give check on subturn 1? If yes, skip subturn 2
174 else if (this.subTurn
==1 && this.underCheck(this.getOppCol(this.turn
)))
176 this.epSquares
[this.epSquares
.length
-1].push(undefined);
177 this.turn
= this.getOppCol(this.turn
);
178 move.checkOnSubturn1
= true;
182 if (this.subTurn
== 2)
183 this.turn
= this.getOppCol(this.turn
);
184 this.subTurn
= 3 - this.subTurn
;
186 this.moves
.push(move);
187 this.updateVariables(move);
189 move.hash
= hex_md5(this.getFen());
194 this.disaggregateFlags(JSON
.parse(move.flags
));
195 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
196 if (lastEpsq
.length
== 2)
198 if (!!move.checkOnSubturn1
||
199 (this.startAtFirstMove
&& this.moves
.length
== 1))
201 this.epSquares
.pop(); //remove real + artificial e.p. squares
207 this.epSquares
.pop();
208 V
.UndoOnBoard(this.board
, move);
209 if (this.startAtFirstMove
&& this.moves
.length
== 1)
211 else if (move.checkOnSubturn1
)
213 this.turn
= this.getOppCol(this.turn
);
218 if (this.subTurn
== 1)
219 this.turn
= this.getOppCol(this.turn
);
220 this.subTurn
= 3 - this.subTurn
;
223 this.unupdateVariables(move);
226 // NOTE: GenRandInitFen() is OK,
227 // since at first move turn indicator is just "w"
229 // No alpha-beta here, just adapted min-max at depth 2(+1)
232 if (this.subTurn
== 2)
233 return null; //TODO: imperfect interface setup
235 const maxeval
= V
.INFINITY
;
236 const color
= this.turn
;
237 const oppCol
= this.getOppCol(this.turn
);
239 // Search best (half) move for opponent turn
240 const getBestMoveEval
= () => {
241 let moves
= this.getAllValidMoves();
242 if (moves
.length
== 0)
244 const score
= this.checkGameEnd();
247 return maxeval
* (score
== "1-0" ? 1 : -1);
249 let res
= (oppCol
== "w" ? -maxeval : maxeval
);
253 this.turn
= color
; //very artificial...
254 if (!this.atLeastOneMove())
256 const score
= this.checkGameEnd();
258 res
= (oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0));
264 return maxeval
* (score
== "1-0" ? 1 : -1);
267 const evalPos
= this.evalPosition();
268 res
= (oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
));
275 let moves11
= this.getAllValidMoves();
276 let doubleMoves
= [];
277 // Rank moves using a min-max at depth 2
278 for (let i
=0; i
<moves11
.length
; i
++)
280 moves11
[i
].eval
= (color
=="w" ? -1 : 1) * maxeval
;
281 this.play(moves11
[i
]);
282 if (this.turn
!= color
)
284 // We gave check with last move: search the best opponent move
285 doubleMoves
.push({moves:[moves11
[i
]], eval:getBestMoveEval()});
289 let moves12
= this.getAllValidMoves();
290 for (let j
=0; j
<moves12
.length
; j
++)
292 this.play(moves12
[j
]);
294 moves:[moves11
[i
],moves12
[j
]],
295 eval:getBestMoveEval()});
296 this.undo(moves12
[j
]);
299 this.undo(moves11
[i
]);
302 doubleMoves
.sort( (a
,b
) => {
303 return (color
=="w" ? 1 : -1) * (b
.eval
- a
.eval
); });
304 let candidates
= [0]; //indices of candidates moves
306 i
<doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
312 const selected
= doubleMoves
[_
.sample(candidates
, 1)].moves
;
313 if (selected
.length
== 1)
318 getPGN(mycolor
, score
, fenStart
, mode
)
321 pgn
+= '[Site "vchess.club"]<br>';
322 const opponent
= mode
=="human" ? "Anonymous" : "Computer";
323 pgn
+= '[Variant "' + variant
+ '"]<br>';
324 pgn
+= '[Date "' + getDate(new Date()) + '"]<br>';
325 pgn
+= '[White "' + (mycolor
=='w'?'Myself':opponent
) + '"]<br>';
326 pgn
+= '[Black "' + (mycolor
=='b'?'Myself':opponent
) + '"]<br>';
327 pgn
+= '[FenStart "' + fenStart
+ '"]<br>';
328 pgn
+= '[Fen "' + this.getFen() + '"]<br>';
329 pgn
+= '[Result "' + score
+ '"]<br><br>';
333 while (i
< this.moves
.length
)
335 pgn
+= (counter
++) + ".";
336 for (let color
of ["w","b"])
339 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
340 move += this.moves
[i
++].notation
[0] + ",";
341 move = move.slice(0,-1); //remove last comma
342 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
347 // "Complete moves" PGN (helping in ambiguous cases)
350 while (i
< this.moves
.length
)
352 pgn
+= (counter
++) + ".";
353 for (let color
of ["w","b"])
356 while (i
< this.moves
.length
&& this.moves
[i
].color
== color
)
357 move += this.moves
[i
++].notation
[1] + ",";
358 move = move.slice(0,-1); //remove last comma
359 pgn
+= move + (i
< this.moves
.length
-1 ? " " : "");
368 const VariantRules
= MarseilleRules
;