1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class TeleportRules
extends ChessRules
{
6 // Testing move validity results in an infinite update loop.
7 // TODO: find a way to test validity anyway.
8 return (this.subTurn
== 2 && this.board
[x
][y
] == V
.EMPTY
);
11 setOtherVariables(fen
) {
12 super.setOtherVariables(fen
);
17 canTake([x1
, y1
], [x2
, y2
]) {
18 return this.subTurn
== 1;
23 m
.vanish
.length
== 2 &&
24 m
.appear
.length
== 1 &&
25 m
.vanish
[0].c
== m
.vanish
[1].c
&&
26 m
.appear
[0].p
== V
.KING
28 // Rook teleportation with the king
29 return this.getPpath(m
.vanish
[1].c
+ m
.vanish
[1].p
);
31 return this.getPpath(m
.appear
[0].c
+ m
.appear
[0].p
);
34 getPotentialMovesFrom([x
, y
]) {
35 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
36 // subTurn == 2: a move is a click, not handled here
41 if (this.subTurn
== 2) return super.filterValid(moves
);
42 const color
= this.turn
;
43 return moves
.filter(m
=> {
47 m
.vanish
.length
== 1 ||
48 m
.appear
.length
== 2 ||
49 m
.vanish
[0].c
!= m
.vanish
[1].c
52 res
= !this.underCheck(color
);
55 // Self-capture: find landing square not resulting in check
56 outerLoop: for (let i
=0; i
<8; i
++) {
57 for (let j
=0; j
<8; j
++) {
59 this.board
[i
][j
] == V
.EMPTY
&&
61 m
.vanish
[1].p
!= V
.PAWN
||
62 i
!= (color
== 'w' ? 0 : 7)
65 const tMove
= new Move({
71 // The dropped piece nature has no importance:
76 start: { x: -1, y: -1 }
79 const moveOk
= !this.underCheck(color
);
95 if (this.subTurn
== 1) return super.getAllValidMoves();
96 // Subturn == 2: only teleportations
98 const L
= this.firstMove
.length
;
99 const color
= this.turn
;
100 for (let i
=0; i
<8; i
++) {
101 for (let j
=0; j
<8; j
++) {
103 this.board
[i
][j
] == V
.EMPTY
&&
105 this.firstMove
[L
-1].vanish
[1].p
!= V
.PAWN
||
106 i
!= (color
== 'w' ? 0 : 7)
109 const tMove
= new Move({
115 p: this.firstMove
[L
-1].vanish
[1].p
119 start: { x: -1, y: -1 }
122 const moveOk
= !this.underCheck(color
);
124 if (moveOk
) moves
.push(tMove
);
132 if (this.kingPos
[color
][0] < 0)
133 // King is being moved:
135 return super.underCheck(color
);
139 if (isNaN(square
[0])) return null;
140 // If subTurn == 2 && square is empty && !underCheck, then teleport
141 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
142 const L
= this.firstMove
.length
;
143 const color
= this.turn
;
145 this.firstMove
[L
-1].vanish
[1].p
== V
.PAWN
&&
146 square
[0] == (color
== 'w' ? 0 : 7)
148 // Pawns cannot be teleported on last rank
151 const tMove
= new Move({
157 p: this.firstMove
[L
-1].vanish
[1].p
161 start: { x: -1, y: -1 }
164 const moveOk
= !this.underCheck(color
);
166 if (moveOk
) return tMove
;
172 move.flags
= JSON
.stringify(this.aggregateFlags());
173 if (move.vanish
.length
> 0) {
174 this.epSquares
.push(this.getEpSquare(move));
175 this.firstMove
.push(move);
177 V
.PlayOnBoard(this.board
, move);
180 move.vanish
.length
== 1 ||
181 move.appear
.length
== 2 ||
182 move.vanish
[0].c
!= move.vanish
[1].c
184 this.turn
= V
.GetOppCol(this.turn
);
188 else this.subTurn
= 2;
193 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
194 // A king is moved: temporarily off board
195 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
196 else if (move.appear
[0].p
== V
.KING
)
197 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
198 this.updateCastleFlags(move);
201 // NOTE: no need to update if castleFlags already off
202 updateCastleFlags(move) {
203 if (move.vanish
.length
== 0) return;
204 const c
= move.vanish
[0].c
;
206 move.vanish
.length
== 2 &&
207 move.appear
.length
== 1 &&
208 move.vanish
[0].c
== move.vanish
[1].c
210 // Self-capture: of the king or a rook?
211 if (move.vanish
[1].p
== V
.KING
)
212 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
213 else if (move.vanish
[1].p
== V
.ROOK
) {
214 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
216 move.end
.x
== firstRank
&&
217 this.castleFlags
[c
].includes(move.end
.y
)
219 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
220 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
225 super.updateCastleFlags(move, move.vanish
[0].p
, c
);
229 this.disaggregateFlags(JSON
.parse(move.flags
));
230 if (move.vanish
.length
> 0) {
231 this.epSquares
.pop();
232 this.firstMove
.pop();
234 V
.UndoOnBoard(this.board
, move);
235 if (this.subTurn
== 2) this.subTurn
= 1;
237 this.turn
= V
.GetOppCol(this.turn
);
239 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
245 if (move.vanish
.length
== 0) {
246 if (move.appear
[0].p
== V
.KING
)
247 // A king was teleported
248 this.kingPos
[move.appear
[0].c
] = [-1, -1];
250 else if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
251 // A king was (self-)taken
252 this.kingPos
[move.vanish
[1].c
] = [move.end
.x
, move.end
.y
];
253 else super.postUndo(move);
257 let moves
= this.getAllValidMoves();
258 if (moves
.length
== 0) return null;
259 // Custom "search" at depth 1 (for now. TODO?)
260 const maxeval
= V
.INFINITY
;
261 const color
= this.turn
;
262 const initEval
= this.evalPosition();
265 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
267 m
.vanish
.length
== 2 &&
268 m
.appear
.length
== 1 &&
269 m
.vanish
[0].c
== m
.vanish
[1].c
271 const moves2
= this.getAllValidMoves();
273 moves2
.forEach(m2
=> {
275 const score
= this.getCurrentScore();
277 if (["1-0", "0-1"].includes(score
))
278 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
279 else if (score
== "*")
280 // Add small fluctuations to avoid dropping pieces always on the
281 // first square available.
282 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
284 (color
== 'w' && mvEval
> m
.eval
) ||
285 (color
== 'b' && mvEval
< m
.eval
)
294 const score
= this.getCurrentScore();
295 if (score
!= "1/2") {
296 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
297 else m
.eval
= this.evalPosition();
302 moves
.sort((a
, b
) => {
303 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
305 let candidates
= [0];
306 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
308 const mIdx
= candidates
[randInt(candidates
.length
)];
309 if (!moves
[mIdx
].next
) return moves
[mIdx
];
310 const move2
= moves
[mIdx
].next
;
311 delete moves
[mIdx
]["next"];
312 return [moves
[mIdx
], move2
];
316 if (move.vanish
.length
> 0) return super.getNotation(move);
319 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
320 return piece
+ "@" + V
.CoordsToSquare(move.end
);