1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class Allmate1Rules
extends ChessRules
{
4 static get HasEnpassant() {
13 static GenRandInitFen(randomness
) {
14 return ChessRules
.GenRandInitFen(randomness
).slice(0, -2);
17 getPotentialMovesFrom([x
, y
]) {
18 let moves
= super.getPotentialMovesFrom([x
, y
]);
19 // Remove standard captures (without removing castling):
20 moves
= moves
.filter(m
=> {
21 return m
.vanish
.length
== 1 || m
.appear
.length
== 2;
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
);
32 // While something has been captured:
33 // remove it, and keep looking for captures
34 outerLoop: while (true) {
36 // 1) What is attacked?
38 for (let i
=0; i
<V
.size
.x
; i
++) {
39 for (let j
=0; j
<V
.size
.y
; j
++) {
40 if (this.getColor(i
,j
) == oppCol
&& this.isAttacked([i
,j
], color
))
41 attacked
[i
+"_"+j
] = [i
,j
];
44 if (Object
.keys(attacked
).length
== 0) break outerLoop
;
46 // 2) Among attacked pieces, which cannot escape capture?
47 // Avoid "oppMoves = this.getAllValidMoves();" => infinite recursion
48 for (let i
=0; i
<V
.size
.x
; i
++) {
49 for (let j
=0; j
<V
.size
.y
; j
++) {
50 if (this.getColor(i
,j
) == oppCol
) {
52 switch (this.getPiece(i
, j
)) {
54 oppMoves
= this.getPotentialPawnMoves([i
, j
]);
57 oppMoves
= this.getPotentialRookMoves([i
, j
]);
60 oppMoves
= this.getPotentialKnightMoves([i
, j
]);
63 oppMoves
= this.getPotentialBishopMoves([i
, j
]);
66 oppMoves
= this.getPotentialQueenMoves([i
, j
]);
69 // Do not allow castling to escape from check
70 oppMoves
= super.getSlideNJumpMoves(
72 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
77 for (let om
of oppMoves
) {
78 V
.PlayOnBoard(this.board
, om
);
79 Object
.values(attacked
).forEach(sq
=> {
80 const origSq
= [sq
[0], sq
[1]];
81 if (om
.start
.x
== sq
[0] && om
.start
.y
== sq
[1])
83 sq
= [om
.appear
[0].x
, om
.appear
[0].y
];
84 if (!this.isAttacked(sq
, color
))
85 delete attacked
[origSq
[0]+"_"+origSq
[1]];
87 V
.UndoOnBoard(this.board
, om
);
88 if (Object
.keys(attacked
).length
== 0)
89 // No need to explore more moves
96 // 3) Add mate-captures and remove pieces from board:
97 Object
.keys(attacked
).forEach(k
=> {
100 p: this.getPiece(attacked
[k
][0], attacked
[k
][1])
102 this.board
[attacked
[k
][0]][attacked
[k
][1]] = V
.EMPTY
;
106 // Put removed pieces back on board:
107 allAttacks
.forEach(v
=> {
108 this.board
[v
.sq
[0]][v
.sq
[1]] = oppCol
+ v
.p
;
111 allAttacks
.forEach(v
=> {
126 // No "under check" conditions in castling
128 return super.getCastleMoves(sq
, "castleInCheck");
131 // TODO: allow pieces to "commit suicide"? (Currently yes except king)
133 // Remove moves which let the king mate-captured:
134 if (moves
.length
== 0) return [];
135 const color
= this.turn
;
136 const oppCol
= V
.GetOppCol(color
);
137 return moves
.filter(m
=> {
140 if (this.underCheck(color
)) {
142 const attacked
= this.kingPos
[color
];
143 // Try to find a move to escape check
144 // TODO: very inefficient method.
145 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
146 for (let j
=0; j
<V
.size
.y
; j
++) {
147 if (this.getColor(i
,j
) == color
) {
149 // Artficial turn change to "play twice":
151 switch (this.getPiece(i
, j
)) {
153 emoves
= this.getPotentialPawnMoves([i
, j
]);
156 emoves
= this.getPotentialRookMoves([i
, j
]);
159 emoves
= this.getPotentialKnightMoves([i
, j
]);
162 emoves
= this.getPotentialBishopMoves([i
, j
]);
165 emoves
= this.getPotentialQueenMoves([i
, j
]);
168 emoves
= this.getPotentialKingMoves([i
, j
]);
172 for (let em
of emoves
) {
173 V
.PlayOnBoard(this.board
, em
);
175 if (em
.start
.x
== attacked
[0] && em
.start
.y
== attacked
[1])
177 sq
= [em
.appear
[0].x
, em
.appear
[0].y
];
178 if (!this.isAttacked(sq
, oppCol
))
180 V
.UndoOnBoard(this.board
, em
);
182 // No need to explore more moves
195 super.postPlay(move);
196 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
197 for (let i
= 1; i
<move.vanish
.length
; i
++) {
198 const v
= move.vanish
[i
];
199 // Did opponent king disappeared?
201 this.kingPos
[this.turn
] = [-1, -1];
203 else if (v
.p
== V
.ROOK
) {
204 if (v
.y
< this.INIT_COL_KING
[v
.c
])
205 this.castleFlags
[v
.c
][0] = 8;
207 // v.y > this.INIT_COL_KING[v.c]
208 this.castleFlags
[v
.c
][1] = 8;
216 const oppCol
= this.turn
;
217 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
218 // Did opponent king disappeared?
219 const psq
= move.vanish
.find(v
=> v
.p
== V
.KING
&& v
.c
== oppCol
)
221 this.kingPos
[psq
.c
] = [psq
.x
, psq
.y
];
226 const color
= this.turn
;
227 const kp
= this.kingPos
[color
];
230 return color
== "w" ? "0-1" : "1-0";
231 if (this.atLeastOneMove()) return "*";
232 // Kings still there, no moves:
236 static get SEARCH_DEPTH() {
241 let notation
= super.getNotation(move);
242 // Add a capture mark (not describing what is captured...):
243 if (move.vanish
.length
> 1 && move.appear
.length
== 1) {
244 if (!!(notation
.match(/^[a-h]x/)))
245 // Pawn capture: remove initial "b" in bxc4 for example
246 notation
= notation
.substr(1);
247 notation
= notation
.replace("x","") + "X";