1 import { ChessRules
} from "@/base_rules";
3 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 const color
= this.turn
;
82 for (let i
= 0; i
< V
.size
.x
; i
++) {
83 for (let j
= 0; j
< V
.size
.y
; j
++) {
84 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
85 const moves
= this.getPotentialMovesFrom([i
, j
]);
86 if (moves
.length
> 0) {
87 for (let k
= 0; k
< moves
.length
; k
++) {
89 // NOTE: not using play() here (=> infinite loop)
90 V
.PlayOnBoard(this.board
, m
);
91 if (m
.vanish
[0].p
== V
.KING
)
92 this.kingPos
[color
] = [m
.appear
[0].x
, m
.appear
[0].y
];
93 const res
= !this.underCheck(color
);
94 V
.UndoOnBoard(this.board
, m
);
95 if (m
.vanish
[0].p
== V
.KING
)
96 this.kingPos
[color
] = [m
.start
.x
, m
.start
.y
];
107 move.flags
= JSON
.stringify(this.aggregateFlags());
108 move.turn
= [this.turn
, this.subTurn
];
109 V
.PlayOnBoard(this.board
, move);
110 const epSq
= this.getEpSquare(move);
111 const oppCol
= V
.GetOppCol(this.turn
);
112 if (this.movesCount
== 0) {
113 // First move in game
115 this.epSquares
.push([epSq
]);
118 // Does this move give check on subturn 1 or reach stalemate?
119 // If yes, skip subturn 2
123 this.underCheck(V
.GetOppCol(this.turn
)) ||
124 !this.atLeastOneMove()
128 this.epSquares
.push([epSq
]);
129 move.checkOnSubturn1
= true;
133 if (this.subTurn
== 2) {
135 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
139 this.epSquares
.push([epSq
]);
142 this.subTurn
= 3 - this.subTurn
;
148 const c
= move.turn
[0];
149 const piece
= move.vanish
[0].p
;
150 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
152 if (piece
== V
.KING
) {
153 this.kingPos
[c
] = [move.appear
[0].x
, move.appear
[0].y
];
154 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
157 const oppCol
= V
.GetOppCol(c
);
158 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
160 move.start
.x
== firstRank
&& //our rook moves?
161 this.castleFlags
[c
].includes(move.start
.y
)
163 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
164 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
167 move.end
.x
== oppFirstRank
&& //we took opponent rook?
168 this.castleFlags
[oppCol
].includes(move.end
.y
)
170 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
171 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
176 this.disaggregateFlags(JSON
.parse(move.flags
));
177 V
.UndoOnBoard(this.board
, move);
178 if (this.movesCount
== 1 || !!move.checkOnSubturn1
|| this.subTurn
== 2) {
179 // The move may not be full, but is fully undone:
180 this.epSquares
.pop();
181 // Moves counter was just incremented:
185 // Undo the second half of a move
186 let lastEpsq
= this.epSquares
[this.epSquares
.length
- 1];
189 this.turn
= move.turn
[0];
190 this.subTurn
= move.turn
[1];
191 super.postUndo(move);
194 static get VALUES() {
200 q: 7, //slightly less than in orthodox game
205 // No alpha-beta here, just adapted min-max at depth 2(+1)
207 const maxeval
= V
.INFINITY
;
208 const color
= this.turn
;
209 const oppCol
= V
.GetOppCol(this.turn
);
211 // Search best (half) move for opponent turn
212 const getBestMoveEval
= () => {
213 let score
= this.getCurrentScore();
215 if (score
== "1/2") return 0;
216 return maxeval
* (score
== "1-0" ? 1 : -1);
218 let moves
= this.getAllValidMoves();
219 let res
= oppCol
== "w" ? -maxeval : maxeval
;
220 for (let m
of moves
) {
222 score
= this.getCurrentScore();
223 // Now turn is oppCol,2 if m doesn't give check
224 // Otherwise it's color,1. In both cases the next test makes sense
227 res
= oppCol
== "w" ? Math
.max(res
, 0) : Math
.min(res
, 0);
231 return maxeval
* (score
== "1-0" ? 1 : -1);
234 const evalPos
= this.evalPosition();
235 res
= oppCol
== "w" ? Math
.max(res
, evalPos
) : Math
.min(res
, evalPos
);
241 const moves11
= this.getAllValidMoves();
242 let doubleMove
= null;
243 let bestEval
= Number
.POSITIVE_INFINITY
* (color
== 'w' ? -1 : 1);
244 // Rank moves using a min-max at depth 2
245 for (let i
= 0; i
< moves11
.length
; i
++) {
246 this.play(moves11
[i
]);
247 if (this.turn
!= color
) {
248 // We gave check with last move: search the best opponent move
249 const evalM
= getBestMoveEval() + 0.05 - Math
.random() / 10;
251 (color
== 'w' && evalM
> bestEval
) ||
252 (color
== 'b' && evalM
< bestEval
)
254 doubleMove
= moves11
[i
];
259 let moves12
= this.getAllValidMoves();
260 for (let j
= 0; j
< moves12
.length
; j
++) {
261 this.play(moves12
[j
]);
262 const evalM
= getBestMoveEval() + 0.05 - Math
.random() / 10
264 (color
== 'w' && evalM
> bestEval
) ||
265 (color
== 'b' && evalM
< bestEval
)
267 doubleMove
= [moves11
[i
], moves12
[j
]];
270 this.undo(moves12
[j
]);
273 this.undo(moves11
[i
]);