52c7fd6e56e5fa08dc9a307284b0f691fe5655ba
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class TeleportRules
extends ChessRules
{
7 // Testing move validity results in an infinite update loop.
8 // TODO: find a way to test validity anyway.
9 return (this.subTurn
== 2 && this.board
[x
][y
] == V
.EMPTY
);
12 setOtherVariables(fen
) {
13 super.setOtherVariables(fen
);
18 canTake([x1
, y1
], [x2
, y2
]) {
19 return this.subTurn
== 1;
24 m
.vanish
.length
== 2 &&
25 m
.appear
.length
== 1 &&
26 m
.vanish
[0].c
== m
.vanish
[1].c
&&
27 m
.appear
[0].p
== V
.KING
29 // Rook teleportation with the king
30 return this.getPpath(m
.vanish
[1].c
+ m
.vanish
[1].p
);
32 return this.getPpath(m
.appear
[0].c
+ m
.appear
[0].p
);
35 getPotentialMovesFrom([x
, y
]) {
36 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
37 // subTurn == 2: a move is a click, not handled here
42 if (this.subTurn
== 2) return super.filterValid(moves
);
43 const color
= this.turn
;
44 return moves
.filter(m
=> {
48 m
.vanish
.length
== 1 ||
49 m
.appear
.length
== 2 ||
50 m
.vanish
[0].c
!= m
.vanish
[1].c
53 res
= !this.underCheck(color
);
56 // Self-capture: find landing square not resulting in check
57 outerLoop: for (let i
=0; i
<8; i
++) {
58 for (let j
=0; j
<8; j
++) {
60 this.board
[i
][j
] == V
.EMPTY
&&
62 m
.vanish
[1].p
!= V
.PAWN
||
63 i
!= (color
== 'w' ? 0 : 7)
66 const tMove
= new Move({
72 // The dropped piece nature has no importance:
77 start: { x: -1, y: -1 }
80 const moveOk
= !this.underCheck(color
);
96 if (this.subTurn
== 1) return super.getAllValidMoves();
97 // Subturn == 2: only teleportations
99 const L
= this.firstMove
.length
;
100 const color
= this.turn
;
101 for (let i
=0; i
<8; i
++) {
102 for (let j
=0; j
<8; j
++) {
104 this.board
[i
][j
] == V
.EMPTY
&&
106 this.firstMove
[L
-1].vanish
[1].p
!= V
.PAWN
||
107 i
!= (color
== 'w' ? 0 : 7)
110 const tMove
= new Move({
116 p: this.firstMove
[L
-1].vanish
[1].p
120 start: { x: -1, y: -1 }
123 const moveOk
= !this.underCheck(color
);
125 if (moveOk
) moves
.push(tMove
);
133 if (this.kingPos
[color
][0] < 0)
134 // King is being moved:
136 return super.underCheck(color
);
140 if (isNaN(square
[0])) return null;
141 // If subTurn == 2 && square is empty && !underCheck, then teleport
142 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
143 const L
= this.firstMove
.length
;
144 const color
= this.turn
;
146 this.firstMove
[L
-1].vanish
[1].p
== V
.PAWN
&&
147 square
[0] == (color
== 'w' ? 0 : 7)
149 // Pawns cannot be teleported on last rank
152 const tMove
= new Move({
158 p: this.firstMove
[L
-1].vanish
[1].p
162 start: { x: -1, y: -1 }
165 const moveOk
= !this.underCheck(color
);
167 if (moveOk
) return tMove
;
173 move.flags
= JSON
.stringify(this.aggregateFlags());
174 if (move.vanish
.length
> 0) {
175 this.epSquares
.push(this.getEpSquare(move));
176 this.firstMove
.push(move);
178 V
.PlayOnBoard(this.board
, move);
181 move.vanish
.length
== 1 ||
182 move.appear
.length
== 2 ||
183 move.vanish
[0].c
!= move.vanish
[1].c
185 this.turn
= V
.GetOppCol(this.turn
);
189 else this.subTurn
= 2;
194 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
195 // A king is moved: temporarily off board
196 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
197 else if (move.appear
[0].p
== V
.KING
)
198 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
199 this.updateCastleFlags(move);
202 // NOTE: no need to update if castleFlags already off
203 updateCastleFlags(move) {
204 if (move.vanish
.length
== 0) return;
205 const c
= move.vanish
[0].c
;
207 move.vanish
.length
== 2 &&
208 move.appear
.length
== 1 &&
209 move.vanish
[0].c
== move.vanish
[1].c
211 // Self-capture: of the king or a rook?
212 if (move.vanish
[1].p
== V
.KING
)
213 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
214 else if (move.vanish
[1].p
== V
.ROOK
) {
215 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
217 move.end
.x
== firstRank
&&
218 this.castleFlags
[c
].includes(move.end
.y
)
220 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
221 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
226 super.updateCastleFlags(move, move.vanish
[0].p
, c
);
230 this.disaggregateFlags(JSON
.parse(move.flags
));
231 if (move.vanish
.length
> 0) {
232 this.epSquares
.pop();
233 this.firstMove
.pop();
235 V
.UndoOnBoard(this.board
, move);
236 if (this.subTurn
== 2) this.subTurn
= 1;
238 this.turn
= V
.GetOppCol(this.turn
);
240 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
246 if (move.vanish
.length
== 0) {
247 if (move.appear
[0].p
== V
.KING
)
248 // A king was teleported
249 this.kingPos
[move.appear
[0].c
] = [-1, -1];
251 else if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
252 // A king was (self-)taken
253 this.kingPos
[move.vanish
[1].c
] = [move.end
.x
, move.end
.y
];
254 else super.postUndo(move);
258 let moves
= this.getAllValidMoves();
259 if (moves
.length
== 0) return null;
260 // Custom "search" at depth 1 (for now. TODO?)
261 const maxeval
= V
.INFINITY
;
262 const color
= this.turn
;
263 const initEval
= this.evalPosition();
266 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
268 m
.vanish
.length
== 2 &&
269 m
.appear
.length
== 1 &&
270 m
.vanish
[0].c
== m
.vanish
[1].c
272 const moves2
= this.getAllValidMoves();
274 moves2
.forEach(m2
=> {
276 const score
= this.getCurrentScore();
278 if (["1-0", "0-1"].includes(score
))
279 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
280 else if (score
== "*")
281 // Add small fluctuations to avoid dropping pieces always on the
282 // first square available.
283 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
285 (color
== 'w' && mvEval
> m
.eval
) ||
286 (color
== 'b' && mvEval
< m
.eval
)
295 const score
= this.getCurrentScore();
296 if (score
!= "1/2") {
297 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
298 else m
.eval
= this.evalPosition();
303 moves
.sort((a
, b
) => {
304 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
306 let candidates
= [0];
307 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
309 const mIdx
= candidates
[randInt(candidates
.length
)];
310 if (!moves
[mIdx
].next
) return moves
[mIdx
];
311 const move2
= moves
[mIdx
].next
;
312 delete moves
[mIdx
]["next"];
313 return [moves
[mIdx
], move2
];
317 if (move.vanish
.length
> 0) return super.getNotation(move);
320 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
321 return piece
+ "@" + V
.CoordsToSquare(move.end
);