1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class Doublemove1Rules
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 this.turn
= parsedFen
.turn
;
38 getEnpassantCaptures([x
, y
], shiftX
) {
40 // En passant: always OK if subturn 1,
41 // OK on subturn 2 only if enPassant was played at subturn 1
42 // (and if there are two e.p. squares available).
43 const Lep
= this.epSquares
.length
;
44 const epSquares
= this.epSquares
[Lep
- 1]; //always at least one element
46 epSquares
.forEach(sq
=> {
47 if (sq
) epSqs
.push(sq
);
49 if (epSqs
.length
== 0) return moves
;
50 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
51 for (let sq
of epSqs
) {
55 // Was this en-passant capture already played at subturn 1 ?
56 // (Or maybe the opponent filled the en-passant square with a piece)
57 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
)
61 Math
.abs(sq
.y
- y
) == 1 &&
62 // Add condition "enemy pawn must be present"
63 this.getPiece(x
, sq
.y
) == V
.PAWN
&&
64 this.getColor(x
, sq
.y
) == oppCol
66 let epMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
81 move.flags
= JSON
.stringify(this.aggregateFlags());
82 move.turn
= this.turn
+ this.subTurn
;
83 V
.PlayOnBoard(this.board
, move);
84 const epSq
= this.getEpSquare(move);
85 if (this.movesCount
== 0) {
88 this.epSquares
.push([epSq
]);
91 // Does this move give check on subturn 1? If yes, skip subturn 2
92 else if (this.subTurn
== 1 && this.underCheck(V
.GetOppCol(this.turn
))) {
93 this.turn
= V
.GetOppCol(this.turn
);
94 this.epSquares
.push([epSq
]);
95 move.checkOnSubturn1
= true;
98 if (this.subTurn
== 2) {
99 this.turn
= V
.GetOppCol(this.turn
);
100 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
103 this.epSquares
.push([epSq
]);
106 this.subTurn
= 3 - this.subTurn
;
112 const c
= move.turn
.charAt(0);
113 const piece
= move.vanish
[0].p
;
114 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
116 if (piece
== V
.KING
&& move.appear
.length
> 0) {
117 this.kingPos
[c
][0] = move.appear
[0].x
;
118 this.kingPos
[c
][1] = move.appear
[0].y
;
119 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
122 const oppCol
= V
.GetOppCol(c
);
123 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
125 move.start
.x
== firstRank
&& //our rook moves?
126 this.castleFlags
[c
].includes(move.start
.y
)
128 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
129 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
131 move.end
.x
== oppFirstRank
&& //we took opponent rook?
132 this.castleFlags
[oppCol
].includes(move.end
.y
)
134 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
135 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
140 this.disaggregateFlags(JSON
.parse(move.flags
));
141 V
.UndoOnBoard(this.board
, move);
142 if (this.movesCount
== 1 || !!move.checkOnSubturn1
|| this.subTurn
== 2) {
143 // The move may not be full, but is fully undone:
144 this.epSquares
.pop();
145 // Moves counter was just incremented:
148 // Undo the second half of a move
149 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
152 this.turn
= move.turn
[0];
153 this.subTurn
= parseInt(move.turn
[1]);
154 super.postUndo(move);
157 static get VALUES() {
163 q: 7, //slightly less than in orthodox game
168 // No alpha-beta here, just adapted min-max at depth 2(+1)
170 const maxeval
= V
.INFINITY
;
171 const color
= this.turn
;
172 const oppCol
= V
.GetOppCol(this.turn
);
174 // Search best (half) move for opponent turn
175 const getBestMoveEval
= () => {
176 let score
= this.getCurrentScore();
178 if (score
== "1/2") return 0;
179 return maxeval
* (score
== "1-0" ? 1 : -1);
181 let moves
= this.getAllValidMoves();
182 let res
= oppCol
== "w" ? -maxeval : maxeval
;
183 for (let m
of moves
) {
185 score
= this.getCurrentScore();
186 // Now turn is oppCol,2 if m doesn't give check
187 // Otherwise it's color,1. In both cases the next test makes sense
190 res
= oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0);
194 return maxeval
* (score
== "1-0" ? 1 : -1);
197 const evalPos
= this.evalPosition();
198 res
= oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
);
204 let moves11
= this.getAllValidMoves();
205 let doubleMoves
= [];
206 // Rank moves using a min-max at depth 2
207 for (let i
= 0; i
< moves11
.length
; i
++) {
208 this.play(moves11
[i
]);
209 if (this.turn
!= color
) {
210 // We gave check with last move: search the best opponent move
211 doubleMoves
.push({ moves: [moves11
[i
]], eval: getBestMoveEval() });
213 let moves12
= this.getAllValidMoves();
214 for (let j
= 0; j
< moves12
.length
; j
++) {
215 this.play(moves12
[j
]);
217 moves: [moves11
[i
], moves12
[j
]],
218 eval: getBestMoveEval()
220 this.undo(moves12
[j
]);
223 this.undo(moves11
[i
]);
226 doubleMoves
.sort((a
, b
) => {
227 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
229 let candidates
= [0]; //indices of candidates moves
232 i
< doubleMoves
.length
&& doubleMoves
[i
].eval
== doubleMoves
[0].eval
;
238 const selected
= doubleMoves
[randInt(candidates
.length
)].moves
;
239 if (selected
.length
== 1) return selected
[0];