1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class MarseilleRules
extends ChessRules
{
5 static IsGoodEnpassant(enpassant
) {
6 const squares
= enpassant
.split(",");
7 if (squares
.length
> 2) return false;
8 for (let sq
of squares
) {
10 const ep
= V
.SquareToCoords(sq
);
11 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
17 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
19 return this.epSquares
[this.epSquares
.length
- 1].map(
20 epsq
=> epsq
=== undefined
22 : V
.CoordsToSquare(epsq
)
26 setOtherVariables(fen
) {
27 const parsedFen
= V
.ParseFen(fen
);
28 this.setFlags(parsedFen
.flags
);
29 this.epSquares
= [parsedFen
.enpassant
.split(",").map(sq
=> {
30 if (sq
!= "-") return V
.SquareToCoords(sq
);
34 // Extract subTurn from turn indicator: "w" (first move), or
35 // "w1" or "w2" white subturn 1 or 2, and same for black
36 this.turn
= parsedFen
.turn
;
40 getEnpassantCaptures([x
, y
], shiftX
) {
42 // En passant: always OK if subturn 1,
43 // OK on subturn 2 only if enPassant was played at subturn 1
44 // (and if there are two e.p. squares available).
45 const Lep
= this.epSquares
.length
;
46 const epSquares
= this.epSquares
[Lep
- 1]; //always at least one element
48 epSquares
.forEach(sq
=> {
49 if (sq
) epSqs
.push(sq
);
51 if (epSqs
.length
== 0) return moves
;
52 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
53 for (let sq
of epSqs
) {
57 // Was this en-passant capture already played at subturn 1 ?
58 // (Or maybe the opponent filled the en-passant square with a piece)
59 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
)
63 Math
.abs(sq
.y
- y
) == 1 &&
64 // Add condition "enemy pawn must be present"
65 this.getPiece(x
, sq
.y
) == V
.PAWN
&&
66 this.getColor(x
, sq
.y
) == oppCol
68 let epMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
83 move.flags
= JSON
.stringify(this.aggregateFlags());
84 move.turn
= this.turn
+ this.subTurn
;
85 V
.PlayOnBoard(this.board
, move);
86 const epSq
= this.getEpSquare(move);
87 if (this.movesCount
== 0) {
90 this.epSquares
.push([epSq
]);
93 // Does this move give check on subturn 1? If yes, skip subturn 2
94 else if (this.subTurn
== 1 && this.underCheck(V
.GetOppCol(this.turn
))) {
95 this.turn
= V
.GetOppCol(this.turn
);
96 this.epSquares
.push([epSq
]);
97 move.checkOnSubturn1
= true;
100 if (this.subTurn
== 2) {
101 this.turn
= V
.GetOppCol(this.turn
);
102 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
105 this.epSquares
.push([epSq
]);
108 this.subTurn
= 3 - this.subTurn
;
114 const c
= move.turn
.charAt(0);
115 const piece
= move.vanish
[0].p
;
116 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
118 if (piece
== V
.KING
&& move.appear
.length
> 0) {
119 this.kingPos
[c
][0] = move.appear
[0].x
;
120 this.kingPos
[c
][1] = move.appear
[0].y
;
121 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
124 const oppCol
= V
.GetOppCol(c
);
125 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
127 move.start
.x
== firstRank
&& //our rook moves?
128 this.castleFlags
[c
].includes(move.start
.y
)
130 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
131 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
133 move.end
.x
== oppFirstRank
&& //we took opponent rook?
134 this.castleFlags
[oppCol
].includes(move.end
.y
)
136 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
137 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
142 this.disaggregateFlags(JSON
.parse(move.flags
));
143 V
.UndoOnBoard(this.board
, move);
144 if (this.movesCount
== 1 || !!move.checkOnSubturn1
|| this.subTurn
== 2) {
145 // The move may not be full, but is fully undone:
146 this.epSquares
.pop();
147 // Moves counter was just incremented:
150 // Undo the second half of a move
151 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
154 this.turn
= move.turn
[0];
155 this.subTurn
= parseInt(move.turn
[1]);
156 super.postUndo(move);
159 // NOTE: GenRandInitFen() is OK,
160 // since at first move turn indicator is just "w"
162 static get VALUES() {
168 q: 7, //slightly less than in orthodox game
173 // No alpha-beta here, just adapted min-max at depth 2(+1)
175 const maxeval
= V
.INFINITY
;
176 const color
= this.turn
;
177 const oppCol
= V
.GetOppCol(this.turn
);
179 // Search best (half) move for opponent turn
180 const getBestMoveEval
= () => {
181 let score
= this.getCurrentScore();
183 if (score
== "1/2") return 0;
184 return maxeval
* (score
== "1-0" ? 1 : -1);
186 let moves
= this.getAllValidMoves();
187 let res
= oppCol
== "w" ? -maxeval : maxeval
;
188 for (let m
of moves
) {
190 score
= this.getCurrentScore();
191 // Now turn is oppCol,2 if m doesn't give check
192 // Otherwise it's color,1. In both cases the next test makes sense
195 res
= oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0);
199 return maxeval
* (score
== "1-0" ? 1 : -1);
202 const evalPos
= this.evalPosition();
203 res
= oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
);
209 let moves11
= this.getAllValidMoves();
210 let doubleMoves
= [];
211 // Rank moves using a min-max at depth 2
212 for (let i
= 0; i
< moves11
.length
; i
++) {
213 this.play(moves11
[i
]);
214 if (this.turn
!= color
) {
215 // We gave check with last move: search the best opponent move
216 doubleMoves
.push({ moves: [moves11
[i
]], eval: getBestMoveEval() });
218 let moves12
= this.getAllValidMoves();
219 for (let j
= 0; j
< moves12
.length
; j
++) {
220 this.play(moves12
[j
]);
222 moves: [moves11
[i
], moves12
[j
]],
223 eval: getBestMoveEval()
225 this.undo(moves12
[j
]);
228 this.undo(moves11
[i
]);
231 doubleMoves
.sort((a
, b
) => {
232 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
234 let candidates
= [0]; //indices of candidates moves
237 i
< doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
243 const selected
= doubleMoves
[randInt(candidates
.length
)].moves
;
244 if (selected
.length
== 1) return selected
[0];