1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class TeleportRules
extends ChessRules
{
6 if (this.subTurn
== 1 || this.board
[x
][y
] != V
.EMPTY
)
8 // Only highlight if the move is legal
9 const color
= this.turn
;
10 const tMove
= new Move({
16 // The dropped piece nature has no importance:
21 start: { x: -1, y: -1 }
24 const moveOk
= !this.underCheck(color
);
29 setOtherVariables(fen
) {
30 super.setOtherVariables(fen
);
35 canTake([x1
, y1
], [x2
, y2
]) {
36 return this.subTurn
== 1;
41 m
.vanish
.length
== 2 &&
42 m
.appear
.length
== 1 &&
43 m
.vanish
[0].c
== m
.vanish
[1].c
&&
44 m
.appear
[0].p
== V
.KING
46 // Rook teleportation with the king
47 return this.getPpath(m
.vanish
[1].c
+ m
.vanish
[1].p
);
49 return this.getPpath(m
.appear
[0].c
+ m
.appear
[0].p
);
52 getPotentialMovesFrom([x
, y
]) {
53 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
54 // subTurn == 2: a move is a click, not handled here
59 if (this.subTurn
== 2) return super.filterValid(moves
);
60 const color
= this.turn
;
61 return moves
.filter(m
=> {
65 m
.vanish
.length
== 1 ||
66 m
.appear
.length
== 2 ||
67 m
.vanish
[0].c
!= m
.vanish
[1].c
70 res
= !this.underCheck(color
);
73 // Self-capture: find landing square not resulting in check
74 outerLoop: for (let i
=0; i
<8; i
++) {
75 for (let j
=0; j
<8; j
++) {
77 this.board
[i
][j
] == V
.EMPTY
&&
79 m
.vanish
[1].p
!= V
.PAWN
||
80 i
!= (color
== 'w' ? 0 : 7)
83 const tMove
= new Move({
89 // The dropped piece nature has no importance:
94 start: { x: -1, y: -1 }
97 const moveOk
= !this.underCheck(color
);
113 if (this.subTurn
== 1) return super.getAllValidMoves();
114 // Subturn == 2: only teleportations
116 const L
= this.firstMove
.length
;
117 const color
= this.turn
;
118 for (let i
=0; i
<8; i
++) {
119 for (let j
=0; j
<8; j
++) {
121 this.board
[i
][j
] == V
.EMPTY
&&
123 this.firstMove
[L
-1].vanish
[1].p
!= V
.PAWN
||
124 i
!= (color
== 'w' ? 0 : 7)
127 const tMove
= new Move({
133 p: this.firstMove
[L
-1].vanish
[1].p
137 start: { x: -1, y: -1 }
140 const moveOk
= !this.underCheck(color
);
142 if (moveOk
) moves
.push(tMove
);
150 if (this.kingPos
[color
][0] < 0)
151 // King is being moved:
153 return super.underCheck(color
);
157 if (this.subTurn
== 2)
160 return super.getCurrentScore();
164 if (isNaN(square
[0])) return null;
165 // If subTurn == 2 && square is empty && !underCheck, then teleport
166 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
167 const L
= this.firstMove
.length
;
168 const color
= this.turn
;
170 this.firstMove
[L
-1].vanish
[1].p
== V
.PAWN
&&
171 square
[0] == (color
== 'w' ? 0 : 7)
173 // Pawns cannot be teleported on last rank
176 const tMove
= new Move({
182 p: this.firstMove
[L
-1].vanish
[1].p
186 start: { x: -1, y: -1 }
189 const moveOk
= !this.underCheck(color
);
191 if (moveOk
) return tMove
;
197 move.flags
= JSON
.stringify(this.aggregateFlags());
198 if (move.vanish
.length
> 0) {
199 this.epSquares
.push(this.getEpSquare(move));
200 this.firstMove
.push(move);
202 V
.PlayOnBoard(this.board
, move);
205 move.vanish
.length
== 1 ||
206 move.appear
.length
== 2 ||
207 move.vanish
[0].c
!= move.vanish
[1].c
209 this.turn
= V
.GetOppCol(this.turn
);
213 else this.subTurn
= 2;
218 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
219 // A king is moved: temporarily off board
220 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
221 else if (move.appear
[0].p
== V
.KING
)
222 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
223 this.updateCastleFlags(move);
226 // NOTE: no need to update if castleFlags already off
227 updateCastleFlags(move) {
228 if (move.vanish
.length
== 0) return;
229 const c
= move.vanish
[0].c
;
231 move.vanish
.length
== 2 &&
232 move.appear
.length
== 1 &&
233 move.vanish
[0].c
== move.vanish
[1].c
235 // Self-capture: of the king or a rook?
236 if (move.vanish
[1].p
== V
.KING
)
237 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
238 else if (move.vanish
[1].p
== V
.ROOK
) {
239 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
241 move.end
.x
== firstRank
&&
242 this.castleFlags
[c
].includes(move.end
.y
)
244 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
245 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
251 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
252 const oppCol
= V
.GetOppCol(c
);
253 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
254 if (move.vanish
[0].p
== V
.KING
&& move.appear
.length
> 0)
255 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
257 move.start
.x
== firstRank
&&
258 this.castleFlags
[c
].includes(move.start
.y
)
260 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
261 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
264 move.end
.x
== oppFirstRank
&&
265 this.castleFlags
[oppCol
].includes(move.end
.y
)
267 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
268 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
274 this.disaggregateFlags(JSON
.parse(move.flags
));
275 if (move.vanish
.length
> 0) {
276 this.epSquares
.pop();
277 this.firstMove
.pop();
279 V
.UndoOnBoard(this.board
, move);
280 if (this.subTurn
== 2) this.subTurn
= 1;
282 this.turn
= V
.GetOppCol(this.turn
);
284 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
290 if (move.vanish
.length
== 0) {
291 if (move.appear
[0].p
== V
.KING
)
292 // A king was teleported
293 this.kingPos
[move.appear
[0].c
] = [-1, -1];
295 else if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
296 // A king was (self-)taken
297 this.kingPos
[move.vanish
[1].c
] = [move.end
.x
, move.end
.y
];
298 else super.postUndo(move);
302 let moves
= this.getAllValidMoves();
303 if (moves
.length
== 0) return null;
304 // Custom "search" at depth 1 (for now. TODO?)
305 const maxeval
= V
.INFINITY
;
306 const color
= this.turn
;
307 const initEval
= this.evalPosition();
310 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
312 m
.vanish
.length
== 2 &&
313 m
.appear
.length
== 1 &&
314 m
.vanish
[0].c
== m
.vanish
[1].c
316 const moves2
= this.getAllValidMoves();
318 moves2
.forEach(m2
=> {
320 const score
= this.getCurrentScore();
322 if (["1-0", "0-1"].includes(score
))
323 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
324 else if (score
== "*")
325 // Add small fluctuations to avoid dropping pieces always on the
326 // first square available.
327 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
329 (color
== 'w' && mvEval
> m
.eval
) ||
330 (color
== 'b' && mvEval
< m
.eval
)
332 // TODO: if many second moves have the same eval, only the
333 // first is kept. Could be randomized.
341 const score
= this.getCurrentScore();
342 if (score
!= "1/2") {
343 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
344 else m
.eval
= this.evalPosition();
349 moves
.sort((a
, b
) => {
350 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
352 let candidates
= [0];
353 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
355 const mIdx
= candidates
[randInt(candidates
.length
)];
356 if (!moves
[mIdx
].next
) return moves
[mIdx
];
357 const move2
= moves
[mIdx
].next
;
358 delete moves
[mIdx
]["next"];
359 return [moves
[mIdx
], move2
];
363 if (move.vanish
.length
> 0) return super.getNotation(move);
366 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
367 return piece
+ "@" + V
.CoordsToSquare(move.end
);