1 import { ChessRules
, PiPo
} from "@/base_rules";
3 export class AtomicRules
extends ChessRules
{
5 getPotentialMovesFrom([x
, y
]) {
6 let moves
= super.getPotentialMovesFrom([x
, y
]);
10 // NOTE: if vanish.length==2 and appear.length==2, this is castle
11 if (m
.vanish
.length
> 1 && m
.appear
.length
<= 1) {
12 // Explosion! (TODO?: drop moves which explode our king here)
23 for (let step
of steps
) {
24 let x
= m
.end
.x
+ step
[0];
25 let y
= m
.end
.y
+ step
[1];
28 this.board
[x
][y
] != V
.EMPTY
&&
29 this.getPiece(x
, y
) != V
.PAWN
33 p: this.getPiece(x
, y
),
34 c: this.getColor(x
, y
),
41 m
.end
= { x: m
.appear
[0].x
, y: m
.appear
[0].y
};
42 m
.appear
.pop(); //Nothin appears in this case
49 getPotentialKingMoves([x
, y
]) {
50 // King cannot capture:
52 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
53 for (let step
of steps
) {
54 const i
= x
+ step
[0];
55 const j
= y
+ step
[1];
56 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
)
57 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
59 return moves
.concat(this.getCastleMoves([x
, y
]));
62 isAttacked(sq
, color
) {
64 this.getPiece(sq
[0], sq
[1]) == V
.KING
&&
65 this.isAttackedByKing(sq
, color
)
67 // A king next to the enemy king is immune to attacks
71 this.isAttackedByPawn(sq
, color
) ||
72 this.isAttackedByRook(sq
, color
) ||
73 this.isAttackedByKnight(sq
, color
) ||
74 this.isAttackedByBishop(sq
, color
) ||
75 this.isAttackedByQueen(sq
, color
)
76 // No "attackedByKing": it cannot take
82 if (move.appear
.length
== 0) {
84 const firstRank
= { w: 7, b: 0 };
85 for (let c
of ["w", "b"]) {
86 // Did we explode king of color c ? (TODO: remove move earlier)
88 Math
.abs(this.kingPos
[c
][0] - move.end
.x
) <= 1 &&
89 Math
.abs(this.kingPos
[c
][1] - move.end
.y
) <= 1
91 this.kingPos
[c
] = [-1, -1];
92 this.castleFlags
[c
] = [8, 8];
94 // Now check if init rook(s) exploded
95 if (Math
.abs(move.end
.x
- firstRank
[c
]) <= 1) {
96 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][0]) <= 1)
97 this.castleFlags
[c
][0] = 8;
98 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][1]) <= 1)
99 this.castleFlags
[c
][1] = 8;
107 super.postUndo(move);
109 const oppCol
= V
.GetOppCol(c
);
110 if ([this.kingPos
[c
][0], this.kingPos
[oppCol
][0]].some(e
=> e
< 0)) {
111 // There is a chance that last move blowed some king away..
112 for (let psq
of move.vanish
) {
114 this.kingPos
[psq
.c
== c
? c : oppCol
] = [psq
.x
, psq
.y
];
120 const oppCol
= V
.GetOppCol(color
);
122 // If our king disappeared, move is not valid
123 if (this.kingPos
[color
][0] < 0) res
= true;
124 // If opponent king disappeared, move is valid
125 else if (this.kingPos
[oppCol
][0] < 0) res
= false;
126 // Otherwise, if we remain under check, move is not valid
127 else res
= this.isAttacked(this.kingPos
[color
], oppCol
);
132 const color
= this.turn
;
135 this.kingPos
[color
][0] >= 0 && //king might have exploded
136 this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
))
138 res
= [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))];
144 const color
= this.turn
;
145 const kp
= this.kingPos
[color
];
148 return color
== "w" ? "0-1" : "1-0";
149 if (this.atLeastOneMove()) return "*";
150 if (!this.isAttacked(kp
, V
.GetOppCol(color
))) return "1/2";
151 return color
== "w" ? "0-1" : "1-0"; //checkmate