4801d367792d84c35f1098c2422b9c9d4400dd12
1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class Doublemove2Rules
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 // Goal is king capture => no checks
96 const color
= this.turn
;
97 if (this.kingPos
[color
][0] < 0) return (color
== 'w' ? "0-1" : "1-0");
102 move.flags
= JSON
.stringify(this.aggregateFlags());
103 V
.PlayOnBoard(this.board
, move);
104 const epSq
= this.getEpSquare(move);
105 if (this.subTurn
== 2) {
106 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
108 this.turn
= V
.GetOppCol(this.turn
);
111 this.epSquares
.push([epSq
]);
114 this.movesCount
== 1 ||
115 // King is captured at subTurn 1?
116 (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
121 if (this.movesCount
> 1) this.subTurn
= 3 - this.subTurn
;
126 const c
= move.vanish
[0].c
;
127 const piece
= move.vanish
[0].p
;
128 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
130 if (piece
== V
.KING
&& move.appear
.length
> 0) {
131 this.kingPos
[c
] = [move.appear
[0].x
, move.appear
[0].y
];
132 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
135 const oppCol
= V
.GetOppCol(c
);
136 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
) {
137 // Opponent's king is captured, game over
138 this.kingPos
[oppCol
] = [-1, -1];
139 move.captureKing
= true; //for undo
141 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
143 move.start
.x
== firstRank
&& //our rook moves?
144 this.castleFlags
[c
].includes(move.start
.y
)
146 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
147 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
150 move.end
.x
== oppFirstRank
&& //we took opponent rook?
151 this.castleFlags
[oppCol
].includes(move.end
.y
)
153 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
154 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
159 this.disaggregateFlags(JSON
.parse(move.flags
));
160 V
.UndoOnBoard(this.board
, move);
161 if (this.subTurn
== 2 || this.movesCount
== 1 || !!move.captureKing
) {
162 this.epSquares
.pop();
164 if (this.movesCount
== 0) this.turn
= "w";
167 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
169 this.turn
= V
.GetOppCol(this.turn
);
171 if (this.movesCount
> 0) this.subTurn
= 3 - this.subTurn
;
176 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
177 // Opponent's king was captured
178 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
179 super.postUndo(move);
182 // No alpha-beta here, just adapted min-max at depth 2(+1)
184 const maxeval
= V
.INFINITY
;
185 const color
= this.turn
;
186 const oppCol
= V
.GetOppCol(this.turn
);
188 // Search best (half) move for opponent turn
189 const getBestMoveEval
= () => {
190 let score
= this.getCurrentScore();
191 if (score
!= "*") 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();
200 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 const moves11
= this.getAllValidMoves();
210 if (this.movesCount
== 0)
211 // First white move at random:
212 return moves11
[randInt(moves11
.length
)];
213 let doubleMoves
= [];
214 // Rank moves using a min-max at depth 2
215 for (let i
= 0; i
< moves11
.length
; i
++) {
216 this.play(moves11
[i
]);
217 const moves12
= this.getAllValidMoves();
218 for (let j
= 0; j
< moves12
.length
; j
++) {
219 this.play(moves12
[j
]);
221 moves: [moves11
[i
], moves12
[j
]],
222 // Small fluctuations to uniformize play a little
223 eval: getBestMoveEval() + 0.05 - Math
.random() / 10
225 this.undo(moves12
[j
]);
227 this.undo(moves11
[i
]);
230 doubleMoves
.sort((a
, b
) => {
231 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
233 let candidates
= [0]; //indices of candidates moves
236 i
< doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
241 return doubleMoves
[randInt(candidates
.length
)].moves
;