1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export const VariantRules
= class AllmateRules
extends ChessRules
{
4 static get HasEnpassant() {
9 return false; //Captures handled differently
17 static GenRandInitFen() {
18 return ChessRules
.GenRandInitFen().replace(/ -$/, "");
21 getPotentialMovesFrom([x
, y
]) {
22 let moves
= super.getPotentialMovesFrom([x
, y
]);
24 // Augment moves with "mate-captures":
25 // TODO: this is coded in a highly inefficient way...
26 const color
= this.turn
;
27 const oppCol
= V
.GetOppCol(this.turn
);
31 // 1) What is attacked?
33 for (let i
=0; i
<V
.size
.x
; i
++) {
34 for (let j
=0; j
<V
.size
.y
; j
++) {
35 if (this.getColor(i
,j
) == oppCol
&& this.isAttacked([i
,j
], [color
]))
36 attacked
[i
+"_"+j
] = [i
,j
];
40 // 2) Among attacked pieces, which cannot escape capture?
41 // Avoid "oppMoves = this.getAllValidMoves();" => infinite recursion
42 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
43 for (let j
=0; j
<V
.size
.y
; j
++) {
44 if (this.getColor(i
,j
) == oppCol
) {
46 switch (this.getPiece(i
, j
)) {
48 oppMoves
= this.getPotentialPawnMoves([i
, j
]);
51 oppMoves
= this.getPotentialRookMoves([i
, j
]);
54 oppMoves
= this.getPotentialKnightMoves([i
, j
]);
57 oppMoves
= this.getPotentialBishopMoves([i
, j
]);
60 oppMoves
= this.getPotentialQueenMoves([i
, j
]);
63 oppMoves
= this.getPotentialKingMoves([i
, j
]);
66 for (let om
of oppMoves
) {
67 V
.PlayOnBoard(this.board
, om
);
68 Object
.values(attacked
).forEach(sq
=> {
69 const origSq
= [sq
[0], sq
[1]];
70 if (om
.start
.x
== sq
[0] && om
.start
.y
== sq
[1])
72 sq
= [om
.appear
[0].x
, om
.appear
[0].y
];
73 if (!this.isAttacked(sq
, [color
]))
74 delete attacked
[origSq
[0]+"_"+origSq
[1]];
76 V
.UndoOnBoard(this.board
, om
);
77 if (Object
.keys(attacked
).length
== 0)
78 // No need to explore more moves
85 // 3) Add mate-captures:
86 Object
.values(attacked
).forEach(sq
=> {
87 m
.vanish
.push(new PiPo({
91 p: this.getPiece(sq
[0], sq
[1])
101 // No "under check" conditions in castling
102 getCastleMoves([x
, y
]) {
103 const c
= this.getColor(x
, y
);
104 if (x
!= (c
== "w" ? V
.size
.x
- 1 : 0) || y
!= this.INIT_COL_KING
[c
])
105 return []; //x isn't first rank, or king has moved (shortcut)
108 const oppCol
= V
.GetOppCol(c
);
112 const finalSquares
= [
114 [V
.size
.y
- 2, V
.size
.y
- 3]
119 castleSide
++ //large, then small
121 if (!this.castleFlags
[c
][castleSide
]) continue;
122 // If this code is reached, rooks and king are on initial position
124 // Nothing on the path of the king ? (and no checks)
125 const finDist
= finalSquares
[castleSide
][0] - y
;
126 let step
= finDist
/ Math
.max(1, Math
.abs(finDist
));
127 for (let i
= y
; i
!= finalSquares
[castleSide
][0]; i
+= step
) {
129 this.board
[x
][i
] != V
.EMPTY
&&
130 // NOTE: next check is enough, because of chessboard constraints
131 (this.getColor(x
, i
) != c
||
132 ![V
.KING
, V
.ROOK
].includes(this.getPiece(x
, i
)))
134 continue castlingCheck
;
138 // Nothing on the path to the rook?
139 step
= castleSide
== 0 ? -1 : 1;
140 for (i
= y
+ step
; i
!= this.INIT_COL_ROOK
[c
][castleSide
]; i
+= step
) {
141 if (this.board
[x
][i
] != V
.EMPTY
) continue castlingCheck
;
143 const rookPos
= this.INIT_COL_ROOK
[c
][castleSide
];
145 // Nothing on final squares, except maybe king and castling rook?
146 for (i
= 0; i
< 2; i
++) {
148 this.board
[x
][finalSquares
[castleSide
][i
]] != V
.EMPTY
&&
149 this.getPiece(x
, finalSquares
[castleSide
][i
]) != V
.KING
&&
150 finalSquares
[castleSide
][i
] != rookPos
152 continue castlingCheck
;
156 // If this code is reached, castle is valid
160 new PiPo({ x: x
, y: finalSquares
[castleSide
][0], p: V
.KING
, c: c
}),
161 new PiPo({ x: x
, y: finalSquares
[castleSide
][1], p: V
.ROOK
, c: c
})
164 new PiPo({ x: x
, y: y
, p: V
.KING
, c: c
}),
165 new PiPo({ x: x
, y: rookPos
, p: V
.ROOK
, c: c
})
168 Math
.abs(y
- rookPos
) <= 2
169 ? { x: x
, y: rookPos
}
170 : { x: x
, y: y
+ 2 * (castleSide
== 0 ? -1 : 1) }
182 updateVariables(move) {
183 super.updateVariables(move);
184 if (move.vanish
.length
== 2 && move.appear
.length
== 1) {
185 // Did opponent king disappeared?
186 if (move.vanish
[1].p
== V
.KING
)
187 this.kingPos
[this.turn
] = [-1, -1];
191 unupdateVariables(move) {
192 super.unupdateVariables(move);
193 if (move.vanish
.length
== 2 && move.appear
.length
== 1) {
194 // Did opponent king disappeared?
195 if (move.vanish
[1].p
== V
.KING
)
196 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
,move.vanish
[1].y
];
201 const color
= this.turn
;
202 const kp
= this.kingPos
[color
];
205 return color
== "w" ? "0-1" : "1-0";
206 if (this.atLeastOneMove())
208 // Kings still there, no moves:
212 static get SEARCH_DEPTH() {
217 let notation
= super.getNotation(move);
218 // Add a capture mark (not describing what is captured...):
219 if (move.vanish
.length
> 1 && move.appear
[0].p
!= V
.KING
)
220 notation
= notation
.replace("x","") + "X";