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 (this.subTurn
== 2)
142 return super.getCurrentScore();
146 if (isNaN(square
[0])) return null;
147 // If subTurn == 2 && square is empty && !underCheck, then teleport
148 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
149 const L
= this.firstMove
.length
;
150 const color
= this.turn
;
152 this.firstMove
[L
-1].vanish
[1].p
== V
.PAWN
&&
153 square
[0] == (color
== 'w' ? 0 : 7)
155 // Pawns cannot be teleported on last rank
158 const tMove
= new Move({
164 p: this.firstMove
[L
-1].vanish
[1].p
168 start: { x: -1, y: -1 }
171 const moveOk
= !this.underCheck(color
);
173 if (moveOk
) return tMove
;
179 move.flags
= JSON
.stringify(this.aggregateFlags());
180 if (move.vanish
.length
> 0) {
181 this.epSquares
.push(this.getEpSquare(move));
182 this.firstMove
.push(move);
184 V
.PlayOnBoard(this.board
, move);
187 move.vanish
.length
== 1 ||
188 move.appear
.length
== 2 ||
189 move.vanish
[0].c
!= move.vanish
[1].c
191 this.turn
= V
.GetOppCol(this.turn
);
195 else this.subTurn
= 2;
200 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
201 // A king is moved: temporarily off board
202 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
203 else if (move.appear
[0].p
== V
.KING
)
204 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
205 this.updateCastleFlags(move);
208 // NOTE: no need to update if castleFlags already off
209 updateCastleFlags(move) {
210 if (move.vanish
.length
== 0) return;
211 const c
= move.vanish
[0].c
;
213 move.vanish
.length
== 2 &&
214 move.appear
.length
== 1 &&
215 move.vanish
[0].c
== move.vanish
[1].c
217 // Self-capture: of the king or a rook?
218 if (move.vanish
[1].p
== V
.KING
)
219 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
220 else if (move.vanish
[1].p
== V
.ROOK
) {
221 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
223 move.end
.x
== firstRank
&&
224 this.castleFlags
[c
].includes(move.end
.y
)
226 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
227 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
233 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
234 const oppCol
= V
.GetOppCol(c
);
235 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
236 if (move.vanish
[0].p
== V
.KING
&& move.appear
.length
> 0)
237 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
239 move.start
.x
== firstRank
&&
240 this.castleFlags
[c
].includes(move.start
.y
)
242 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
243 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
246 move.end
.x
== oppFirstRank
&&
247 this.castleFlags
[oppCol
].includes(move.end
.y
)
249 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
250 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
256 this.disaggregateFlags(JSON
.parse(move.flags
));
257 if (move.vanish
.length
> 0) {
258 this.epSquares
.pop();
259 this.firstMove
.pop();
261 V
.UndoOnBoard(this.board
, move);
262 if (this.subTurn
== 2) this.subTurn
= 1;
264 this.turn
= V
.GetOppCol(this.turn
);
266 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
272 if (move.vanish
.length
== 0) {
273 if (move.appear
[0].p
== V
.KING
)
274 // A king was teleported
275 this.kingPos
[move.appear
[0].c
] = [-1, -1];
277 else if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
278 // A king was (self-)taken
279 this.kingPos
[move.vanish
[1].c
] = [move.end
.x
, move.end
.y
];
280 else super.postUndo(move);
284 let moves
= this.getAllValidMoves();
285 if (moves
.length
== 0) return null;
286 // Custom "search" at depth 1 (for now. TODO?)
287 const maxeval
= V
.INFINITY
;
288 const color
= this.turn
;
289 const initEval
= this.evalPosition();
292 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
294 m
.vanish
.length
== 2 &&
295 m
.appear
.length
== 1 &&
296 m
.vanish
[0].c
== m
.vanish
[1].c
298 const moves2
= this.getAllValidMoves();
300 moves2
.forEach(m2
=> {
302 const score
= this.getCurrentScore();
304 if (["1-0", "0-1"].includes(score
))
305 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
306 else if (score
== "*")
307 // Add small fluctuations to avoid dropping pieces always on the
308 // first square available.
309 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
311 (color
== 'w' && mvEval
> m
.eval
) ||
312 (color
== 'b' && mvEval
< m
.eval
)
314 // TODO: if many second moves have the same eval, only the
315 // first is kept. Could be randomized.
323 const score
= this.getCurrentScore();
324 if (score
!= "1/2") {
325 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
326 else m
.eval
= this.evalPosition();
331 moves
.sort((a
, b
) => {
332 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
334 let candidates
= [0];
335 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
337 const mIdx
= candidates
[randInt(candidates
.length
)];
338 if (!moves
[mIdx
].next
) return moves
[mIdx
];
339 const move2
= moves
[mIdx
].next
;
340 delete moves
[mIdx
]["next"];
341 return [moves
[mIdx
], move2
];
345 if (move.vanish
.length
> 0) return super.getNotation(move);
348 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
349 return piece
+ "@" + V
.CoordsToSquare(move.end
);