Fix Maxima (immobilize kings too)
[vchess.git] / client / src / variants / Teleport2.js
CommitLineData
2c9f08fc
BA
1import { Teleport1Rules } from "@/variants/Teleport1";
2
3export class Teleport2Rules extends Teleport1Rules {
4
5 canTake([x1, y1], [x2, y2]) {
6 // Cannot teleport king:
7 return (this.subTurn == 1 && this.getPiece(x2, y2) != V.KING);
8 }
9
10 underCheck(color) {
11 // Standard method:
12 return this.isAttacked(this.kingPos[color], V.GetOppCol(color));
13 }
14
15 postPlay(move) {
500dbd2c 16 if (move.vanish.length > 0) {
2c9f08fc
BA
17 // Standard method:
18 if (move.appear[0].p == V.KING)
19 this.kingPos[move.appear[0].c] = [move.appear[0].x, move.appear[0].y];
20 this.updateCastleFlags(move);
21 }
22 }
23
24 updateCastleFlags(move) {
25 // Standard method: TODO = find a better way... (not rewriting)
500dbd2c 26 const c = move.vanish[0].c;
2c9f08fc
BA
27 const firstRank = (c == "w" ? V.size.x - 1 : 0);
28 const oppCol = this.turn;
29 const oppFirstRank = V.size.x - 1 - firstRank;
500dbd2c 30 if (move.vanish[0].p == V.KING && move.appear.length > 0)
2c9f08fc
BA
31 this.castleFlags[c] = [V.size.y, V.size.y];
32 else if (
33 move.start.x == firstRank &&
34 this.castleFlags[c].includes(move.start.y)
35 ) {
36 const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
37 this.castleFlags[c][flagIdx] = V.size.y;
38 }
39 if (
40 move.end.x == oppFirstRank &&
41 this.castleFlags[oppCol].includes(move.end.y)
42 ) {
43 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
44 this.castleFlags[oppCol][flagIdx] = V.size.y;
45 }
46 }
47
48 postUndo(move) {
49 if (move.vanish.length > 0) {
50 // Standard method:
51 const c = this.getColor(move.start.x, move.start.y);
52 if (this.getPiece(move.start.x, move.start.y) == V.KING)
53 this.kingPos[c] = [move.start.x, move.start.y];
54 }
55 }
56
57};