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 return this.turn
+ this.subTurn
;
25 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
28 const L
= this.epSquares
.length
;
29 if (this.epSquares
[L
-1].every(epsq
=> epsq
=== undefined))
30 return "-"; //no en-passant
32 this.epSquares
[L
-1].forEach(epsq
=> {
34 res
+= V
.CoordsToSquare(epsq
) + ",";
36 return res
.slice(0,-1); //remove last comma
39 setOtherVariables(fen
)
41 const parsedFen
= V
.ParseFen(fen
);
42 this.setFlags(parsedFen
.flags
);
43 if (parsedFen
.enpassant
== "-")
44 this.epSquares
= [ [undefined] ];
48 const squares
= parsedFen
.enpassant
.split(",");
49 for (let sq
of squares
)
50 res
.push(V
.SquareToCoords(sq
));
51 this.epSquares
= [ res
];
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
61 getPotentialPawnMoves([x
,y
])
63 const color
= this.turn
;
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
]
75 if (this.board
[x
+shiftX
][y
] == V
.EMPTY
)
77 for (let piece
of finalPieces
)
79 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
],
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
)
87 moves
.push(this.getBasicMove([x
,y
], [x
+2*shiftX
,y
]));
91 for (let shiftY
of [-1,1])
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
]))
97 for (let piece
of finalPieces
)
99 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
+shiftY
],
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
111 epSquares
.forEach(sq
=> {
115 if (epSqs
.length
== 0)
117 const oppCol
= V
.GetOppCol(color
);
118 for (let sq
of epSqs
)
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
))
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
)
129 let epMove
= this.getBasicMove([x
,y
], [sq
.x
,sq
.y
]);
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
153 this.epSquares
.push([epSq
]);
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
)))
158 this.turn
= V
.GetOppCol(this.turn
);
159 this.epSquares
.push([epSq
]);
160 move.checkOnSubturn1
= true;
164 if (this.subTurn
== 2)
166 this.turn
= V
.GetOppCol(this.turn
);
167 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
171 this.epSquares
.push([epSq
]);
172 this.subTurn
= 3 - this.subTurn
;
174 this.updateVariables(move);
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
185 let lastEpsq
= this.epSquares
[this.epSquares
.length
-1];
188 this.turn
= move.turn
[0];
189 this.subTurn
= parseInt(move.turn
[1]);
190 this.unupdateVariables(move);
193 // NOTE: GenRandInitFen() is OK,
194 // since at first move turn indicator is just "w"
203 'q': 7, //slightly less than in orthodox game
208 // No alpha-beta here, just adapted min-max at depth 2(+1)
211 if (this.subTurn
== 2)
212 return null; //TODO: imperfect interface setup
214 const maxeval
= V
.INFINITY
;
215 const color
= this.turn
;
216 const oppCol
= V
.GetOppCol(this.turn
);
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)
224 const score
= this.checkGameEnd();
227 return maxeval
* (score
== "1-0" ? 1 : -1);
229 let res
= (oppCol
== "w" ? -maxeval : maxeval
);
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())
237 const score
= this.checkGameEnd();
239 res
= (oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0));
244 return maxeval
* (score
== "1-0" ? 1 : -1);
247 const evalPos
= this.evalPosition();
248 res
= (oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
));
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
++)
259 this.play(moves11
[i
]);
260 if (this.turn
!= color
)
262 // We gave check with last move: search the best opponent move
263 doubleMoves
.push({moves:[moves11
[i
]], eval:getBestMoveEval()});
267 let moves12
= this.getAllValidMoves();
268 for (let j
=0; j
<moves12
.length
; j
++)
270 this.play(moves12
[j
]);
272 moves:[moves11
[i
],moves12
[j
]],
273 eval:getBestMoveEval()});
274 this.undo(moves12
[j
]);
277 this.undo(moves11
[i
]);
280 doubleMoves
.sort( (a
,b
) => {
281 return (color
=="w" ? 1 : -1) * (b
.eval
- a
.eval
); });
282 let candidates
= [0]; //indices of candidates moves
284 i
<doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
290 const selected
= doubleMoves
[sample(candidates
)].moves
;
291 if (selected
.length
== 1)