1 import { ChessRules
} from "@/base_rules";
3 export class Doublemove1Rules
extends ChessRules
{
4 static IsGoodEnpassant(enpassant
) {
5 const squares
= enpassant
.split(",");
6 if (squares
.length
> 2) return false;
7 for (let sq
of squares
) {
9 const ep
= V
.SquareToCoords(sq
);
10 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
16 // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
18 return this.epSquares
[this.epSquares
.length
- 1].map(
19 epsq
=> epsq
=== undefined
21 : V
.CoordsToSquare(epsq
)
25 setOtherVariables(fen
) {
26 const parsedFen
= V
.ParseFen(fen
);
27 this.setFlags(parsedFen
.flags
);
28 this.epSquares
= [parsedFen
.enpassant
.split(",").map(sq
=> {
29 if (sq
!= "-") return V
.SquareToCoords(sq
);
33 this.turn
= parsedFen
.turn
;
37 getEnpassantCaptures([x
, y
], shiftX
) {
39 // En passant: always OK if subturn 1,
40 // OK on subturn 2 only if enPassant was played at subturn 1
41 // (and if there are two e.p. squares available).
42 const Lep
= this.epSquares
.length
;
43 const epSquares
= this.epSquares
[Lep
- 1]; //always at least one element
45 epSquares
.forEach(sq
=> {
46 if (sq
) epSqs
.push(sq
);
48 if (epSqs
.length
== 0) return moves
;
49 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
50 for (let sq
of epSqs
) {
54 // Was this en-passant capture already played at subturn 1 ?
55 // (Or maybe the opponent filled the en-passant square with a piece)
56 this.board
[epSqs
[0].x
][epSqs
[0].y
] != V
.EMPTY
)
60 Math
.abs(sq
.y
- y
) == 1 &&
61 // Add condition "enemy pawn must be present"
62 this.getPiece(x
, sq
.y
) == V
.PAWN
&&
63 this.getColor(x
, sq
.y
) == oppCol
65 let epMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
80 move.flags
= JSON
.stringify(this.aggregateFlags());
81 move.turn
= [this.turn
, this.subTurn
];
82 V
.PlayOnBoard(this.board
, move);
83 const epSq
= this.getEpSquare(move);
84 if (this.movesCount
== 0) {
87 this.epSquares
.push([epSq
]);
90 // Does this move give check on subturn 1 or reach stalemate?
91 // If yes, skip subturn 2
95 this.underCheck(V
.GetOppCol(this.turn
)) ||
96 !this.atLeastOneMove()
99 this.turn
= V
.GetOppCol(this.turn
);
100 this.epSquares
.push([epSq
]);
101 move.checkOnSubturn1
= true;
105 if (this.subTurn
== 2) {
106 this.turn
= V
.GetOppCol(this.turn
);
107 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
111 this.epSquares
.push([epSq
]);
114 this.subTurn
= 3 - this.subTurn
;
120 const c
= move.turn
[0];
121 const piece
= move.vanish
[0].p
;
122 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
124 if (piece
== V
.KING
) {
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
;
140 move.end
.x
== oppFirstRank
&& //we took opponent rook?
141 this.castleFlags
[oppCol
].includes(move.end
.y
)
143 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
144 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
149 this.disaggregateFlags(JSON
.parse(move.flags
));
150 V
.UndoOnBoard(this.board
, move);
151 if (this.movesCount
== 1 || !!move.checkOnSubturn1
|| this.subTurn
== 2) {
152 // The move may not be full, but is fully undone:
153 this.epSquares
.pop();
154 // Moves counter was just incremented:
158 // Undo the second half of a move
159 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
162 this.turn
= move.turn
[0];
163 this.subTurn
= move.turn
[1];
164 super.postUndo(move);
167 static get VALUES() {
173 q: 7, //slightly less than in orthodox game
178 // No alpha-beta here, just adapted min-max at depth 2(+1)
180 const maxeval
= V
.INFINITY
;
181 const color
= this.turn
;
182 const oppCol
= V
.GetOppCol(this.turn
);
184 // Search best (half) move for opponent turn
185 const getBestMoveEval
= () => {
186 let score
= this.getCurrentScore();
188 if (score
== "1/2") return 0;
189 return maxeval
* (score
== "1-0" ? 1 : -1);
191 let moves
= this.getAllValidMoves();
192 let res
= oppCol
== "w" ? -maxeval : maxeval
;
193 for (let m
of moves
) {
195 score
= this.getCurrentScore();
196 // Now turn is oppCol,2 if m doesn't give check
197 // Otherwise it's color,1. In both cases the next test makes sense
200 res
= oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0);
204 return maxeval
* (score
== "1-0" ? 1 : -1);
207 const evalPos
= this.evalPosition();
208 res
= oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
);
214 const moves11
= this.getAllValidMoves();
215 let doubleMove
= null;
216 let bestEval
= Number
.POSITIVE_INFINITY
* (color
== 'w' ? -1 : 1);
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 const evalM
= getBestMoveEval() + 0.05 - Math
.random() / 10;
224 (color
== 'w' && evalM
> bestEval
) ||
225 (color
== 'b' && evalM
< bestEval
)
227 doubleMove
= moves11
[i
];
232 let moves12
= this.getAllValidMoves();
233 for (let j
= 0; j
< moves12
.length
; j
++) {
234 this.play(moves12
[j
]);
235 const evalM
= getBestMoveEval() + 0.05 - Math
.random() / 10
237 (color
== 'w' && evalM
> bestEval
) ||
238 (color
== 'b' && evalM
< bestEval
)
240 doubleMove
= [moves11
[i
], moves12
[j
]];
243 this.undo(moves12
[j
]);
246 this.undo(moves11
[i
]);