940d9ba9d5683d15e2c55f9a1f495264994f2554
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;
18 return this.turn
+ this.subTurn
;
21 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
23 return this.epSquares
[this.epSquares
.length
- 1].map(
24 epsq
=> epsq
=== undefined
26 : V
.CoordsToSquare(epsq
)
30 setOtherVariables(fen
) {
31 const parsedFen
= V
.ParseFen(fen
);
32 this.setFlags(parsedFen
.flags
);
33 this.epSquares
= [parsedFen
.enpassant
.split(",").map(sq
=> {
34 if (sq
!= "-") return V
.SquareToCoords(sq
);
38 // Extract subTurn from turn indicator: "w" (first move), or
39 // "w1" or "w2" white subturn 1 or 2, and same for black
40 const fullTurn
= V
.ParseFen(fen
).turn
;
41 this.turn
= fullTurn
[0];
42 // At move 1, the subTurn doesn't need to be specified:
43 this.subTurn
= fullTurn
[1] || 1;
46 getEnpassantCaptures([x
, y
], shiftX
) {
48 // En passant: always OK if subturn 1,
49 // OK on subturn 2 only if enPassant was played at subturn 1
50 // (and if there are two e.p. squares available).
51 const Lep
= this.epSquares
.length
;
52 const epSquares
= this.epSquares
[Lep
- 1]; //always at least one element
54 epSquares
.forEach(sq
=> {
55 if (sq
) epSqs
.push(sq
);
57 if (epSqs
.length
== 0) return moves
;
58 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
59 for (let sq
of epSqs
) {
63 // Was this en-passant capture already played at subturn 1 ?
64 // (Or maybe the opponent filled the en-passant square with a piece)
65 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
)
69 Math
.abs(sq
.y
- y
) == 1 &&
70 // Add condition "enemy pawn must be present"
71 this.getPiece(x
, sq
.y
) == V
.PAWN
&&
72 this.getColor(x
, sq
.y
) == oppCol
74 let epMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
89 move.flags
= JSON
.stringify(this.aggregateFlags());
90 move.turn
= this.turn
+ this.subTurn
;
91 V
.PlayOnBoard(this.board
, move);
92 const epSq
= this.getEpSquare(move);
93 if (this.movesCount
== 0) {
96 this.epSquares
.push([epSq
]);
99 // Does this move give check on subturn 1? If yes, skip subturn 2
100 else if (this.subTurn
== 1 && this.underCheck(V
.GetOppCol(this.turn
))) {
101 this.turn
= V
.GetOppCol(this.turn
);
102 this.epSquares
.push([epSq
]);
103 move.checkOnSubturn1
= true;
106 if (this.subTurn
== 2) {
107 this.turn
= V
.GetOppCol(this.turn
);
108 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
111 this.epSquares
.push([epSq
]);
114 this.subTurn
= 3 - this.subTurn
;
120 const c
= move.turn
.charAt(0);
121 const piece
= move.vanish
[0].p
;
122 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
124 if (piece
== V
.KING
&& move.appear
.length
> 0) {
125 this.kingPos
[c
][0] = move.appear
[0].x
;
126 this.kingPos
[c
][1] = move.appear
[0].y
;
127 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
130 const oppCol
= V
.GetOppCol(c
);
131 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
133 move.start
.x
== firstRank
&& //our rook moves?
134 this.castleFlags
[c
].includes(move.start
.y
)
136 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
137 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
139 move.end
.x
== oppFirstRank
&& //we took opponent rook?
140 this.castleFlags
[oppCol
].includes(move.end
.y
)
142 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
143 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
148 this.disaggregateFlags(JSON
.parse(move.flags
));
149 V
.UndoOnBoard(this.board
, move);
150 if (this.movesCount
== 1 || !!move.checkOnSubturn1
|| this.subTurn
== 2) {
151 // The move may not be full, but is fully undone:
152 this.epSquares
.pop();
153 // Moves counter was just incremented:
156 // Undo the second half of a move
157 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
160 this.turn
= move.turn
[0];
161 this.subTurn
= parseInt(move.turn
[1]);
162 super.postUndo(move);
165 // NOTE: GenRandInitFen() is OK,
166 // since at first move turn indicator is just "w"
168 static get VALUES() {
174 q: 7, //slightly less than in orthodox game
179 // No alpha-beta here, just adapted min-max at depth 2(+1)
181 const maxeval
= V
.INFINITY
;
182 const color
= this.turn
;
183 const oppCol
= V
.GetOppCol(this.turn
);
185 // Search best (half) move for opponent turn
186 const getBestMoveEval
= () => {
187 let score
= this.getCurrentScore();
189 if (score
== "1/2") return 0;
190 return maxeval
* (score
== "1-0" ? 1 : -1);
192 let moves
= this.getAllValidMoves();
193 let res
= oppCol
== "w" ? -maxeval : maxeval
;
194 for (let m
of moves
) {
196 score
= this.getCurrentScore();
197 // Now turn is oppCol,2 if m doesn't give check
198 // Otherwise it's color,1. In both cases the next test makes sense
201 res
= oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0);
205 return maxeval
* (score
== "1-0" ? 1 : -1);
208 const evalPos
= this.evalPosition();
209 res
= oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
);
215 let moves11
= this.getAllValidMoves();
216 let doubleMoves
= [];
217 // Rank moves using a min-max at depth 2
218 for (let i
= 0; i
< moves11
.length
; i
++) {
219 this.play(moves11
[i
]);
220 if (this.turn
!= color
) {
221 // We gave check with last move: search the best opponent move
222 doubleMoves
.push({ moves: [moves11
[i
]], eval: getBestMoveEval() });
224 let moves12
= this.getAllValidMoves();
225 for (let j
= 0; j
< moves12
.length
; j
++) {
226 this.play(moves12
[j
]);
228 moves: [moves11
[i
], moves12
[j
]],
229 eval: getBestMoveEval()
231 this.undo(moves12
[j
]);
234 this.undo(moves11
[i
]);
237 doubleMoves
.sort((a
, b
) => {
238 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
240 let candidates
= [0]; //indices of candidates moves
243 i
< doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
249 const selected
= doubleMoves
[randInt(candidates
.length
)].moves
;
250 if (selected
.length
== 1) return selected
[0];