1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class TeleportRules
extends ChessRules
{
6 // TODO: only highlight if the move is legal
7 return (this.subTurn
== 2 && this.board
[x
][y
] == V
.EMPTY
);
10 setOtherVariables(fen
) {
11 super.setOtherVariables(fen
);
16 canTake([x1
, y1
], [x2
, y2
]) {
17 return this.subTurn
== 1;
22 m
.vanish
.length
== 2 &&
23 m
.appear
.length
== 1 &&
24 m
.vanish
[0].c
== m
.vanish
[1].c
&&
25 m
.appear
[0].p
== V
.KING
27 // Rook teleportation with the king
28 return this.getPpath(m
.vanish
[1].c
+ m
.vanish
[1].p
);
30 return this.getPpath(m
.appear
[0].c
+ m
.appear
[0].p
);
33 getPotentialMovesFrom([x
, y
]) {
34 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
35 // subTurn == 2: a move is a click, not handled here
40 if (this.subTurn
== 2) return super.filterValid(moves
);
41 const color
= this.turn
;
42 return moves
.filter(m
=> {
46 m
.vanish
.length
== 1 ||
47 m
.appear
.length
== 2 ||
48 m
.vanish
[0].c
!= m
.vanish
[1].c
51 res
= !this.underCheck(color
);
54 // Self-capture: find landing square not resulting in check
55 outerLoop: for (let i
=0; i
<8; i
++) {
56 for (let j
=0; j
<8; j
++) {
58 this.board
[i
][j
] == V
.EMPTY
&&
60 m
.vanish
[1].p
!= V
.PAWN
||
61 i
!= (color
== 'w' ? 0 : 7)
64 const tMove
= new Move({
70 // The dropped piece nature has no importance:
75 start: { x: -1, y: -1 }
78 const moveOk
= !this.underCheck(color
);
94 if (this.subTurn
== 1) return super.getAllValidMoves();
95 // Subturn == 2: only teleportations
97 const L
= this.firstMove
.length
;
98 const color
= this.turn
;
99 for (let i
=0; i
<8; i
++) {
100 for (let j
=0; j
<8; j
++) {
102 this.board
[i
][j
] == V
.EMPTY
&&
104 this.firstMove
[L
-1].vanish
[1].p
!= V
.PAWN
||
105 i
!= (color
== 'w' ? 0 : 7)
108 const tMove
= new Move({
114 p: this.firstMove
[L
-1].vanish
[1].p
118 start: { x: -1, y: -1 }
121 const moveOk
= !this.underCheck(color
);
123 if (moveOk
) moves
.push(tMove
);
131 if (this.kingPos
[color
][0] < 0)
132 // King is being moved:
134 return super.underCheck(color
);
138 if (this.subTurn
== 2)
141 return super.getCurrentScore();
145 if (isNaN(square
[0])) return null;
146 // If subTurn == 2 && square is empty && !underCheck, then teleport
147 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
148 const L
= this.firstMove
.length
;
149 const color
= this.turn
;
151 this.firstMove
[L
-1].vanish
[1].p
== V
.PAWN
&&
152 square
[0] == (color
== 'w' ? 0 : 7)
154 // Pawns cannot be teleported on last rank
157 const tMove
= new Move({
163 p: this.firstMove
[L
-1].vanish
[1].p
167 start: { x: -1, y: -1 }
170 const moveOk
= !this.underCheck(color
);
172 if (moveOk
) return tMove
;
178 move.flags
= JSON
.stringify(this.aggregateFlags());
179 if (move.vanish
.length
> 0) {
180 this.epSquares
.push(this.getEpSquare(move));
181 this.firstMove
.push(move);
183 V
.PlayOnBoard(this.board
, move);
186 move.vanish
.length
== 1 ||
187 move.appear
.length
== 2 ||
188 move.vanish
[0].c
!= move.vanish
[1].c
190 this.turn
= V
.GetOppCol(this.turn
);
194 else this.subTurn
= 2;
199 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
200 // A king is moved: temporarily off board
201 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
202 else if (move.appear
[0].p
== V
.KING
)
203 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
204 this.updateCastleFlags(move);
207 // NOTE: no need to update if castleFlags already off
208 updateCastleFlags(move) {
209 if (move.vanish
.length
== 0) return;
210 const c
= move.vanish
[0].c
;
212 move.vanish
.length
== 2 &&
213 move.appear
.length
== 1 &&
214 move.vanish
[0].c
== move.vanish
[1].c
216 // Self-capture: of the king or a rook?
217 if (move.vanish
[1].p
== V
.KING
)
218 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
219 else if (move.vanish
[1].p
== V
.ROOK
) {
220 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
222 move.end
.x
== firstRank
&&
223 this.castleFlags
[c
].includes(move.end
.y
)
225 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
226 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
232 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
233 const oppCol
= V
.GetOppCol(c
);
234 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
235 if (move.vanish
[0].p
== V
.KING
&& move.appear
.length
> 0)
236 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
238 move.start
.x
== firstRank
&&
239 this.castleFlags
[c
].includes(move.start
.y
)
241 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
242 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
245 move.end
.x
== oppFirstRank
&&
246 this.castleFlags
[oppCol
].includes(move.end
.y
)
248 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
249 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
255 this.disaggregateFlags(JSON
.parse(move.flags
));
256 if (move.vanish
.length
> 0) {
257 this.epSquares
.pop();
258 this.firstMove
.pop();
260 V
.UndoOnBoard(this.board
, move);
261 if (this.subTurn
== 2) this.subTurn
= 1;
263 this.turn
= V
.GetOppCol(this.turn
);
265 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
271 if (move.vanish
.length
== 0) {
272 if (move.appear
[0].p
== V
.KING
)
273 // A king was teleported
274 this.kingPos
[move.appear
[0].c
] = [-1, -1];
276 else if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
277 // A king was (self-)taken
278 this.kingPos
[move.vanish
[1].c
] = [move.end
.x
, move.end
.y
];
279 else super.postUndo(move);
283 let moves
= this.getAllValidMoves();
284 if (moves
.length
== 0) return null;
285 // Custom "search" at depth 1 (for now. TODO?)
286 const maxeval
= V
.INFINITY
;
287 const color
= this.turn
;
288 const initEval
= this.evalPosition();
291 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
293 m
.vanish
.length
== 2 &&
294 m
.appear
.length
== 1 &&
295 m
.vanish
[0].c
== m
.vanish
[1].c
297 const moves2
= this.getAllValidMoves();
299 moves2
.forEach(m2
=> {
301 const score
= this.getCurrentScore();
303 if (["1-0", "0-1"].includes(score
))
304 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
305 else if (score
== "*")
306 // Add small fluctuations to avoid dropping pieces always on the
307 // first square available.
308 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
310 (color
== 'w' && mvEval
> m
.eval
) ||
311 (color
== 'b' && mvEval
< m
.eval
)
313 // TODO: if many second moves have the same eval, only the
314 // first is kept. Could be randomized.
322 const score
= this.getCurrentScore();
323 if (score
!= "1/2") {
324 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
325 else m
.eval
= this.evalPosition();
330 moves
.sort((a
, b
) => {
331 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
333 let candidates
= [0];
334 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
336 const mIdx
= candidates
[randInt(candidates
.length
)];
337 if (!moves
[mIdx
].next
) return moves
[mIdx
];
338 const move2
= moves
[mIdx
].next
;
339 delete moves
[mIdx
]["next"];
340 return [moves
[mIdx
], move2
];
344 if (move.vanish
.length
> 0) return super.getNotation(move);
347 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
348 return piece
+ "@" + V
.CoordsToSquare(move.end
);