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
);
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 // Do not allow castling to escape from check
64 oppMoves
= super.getSlideNJumpMoves(
66 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
71 for (let om
of oppMoves
) {
72 V
.PlayOnBoard(this.board
, om
);
73 Object
.values(attacked
).forEach(sq
=> {
74 const origSq
= [sq
[0], sq
[1]];
75 if (om
.start
.x
== sq
[0] && om
.start
.y
== sq
[1])
77 sq
= [om
.appear
[0].x
, om
.appear
[0].y
];
78 if (!this.isAttacked(sq
, color
))
79 delete attacked
[origSq
[0]+"_"+origSq
[1]];
81 V
.UndoOnBoard(this.board
, om
);
82 if (Object
.keys(attacked
).length
== 0)
83 // No need to explore more moves
90 // 3) Add mate-captures:
91 Object
.values(attacked
).forEach(sq
=> {
92 m
.vanish
.push(new PiPo({
96 p: this.getPiece(sq
[0], sq
[1])
106 // No "under check" conditions in castling
108 return super.getCastleMoves(sq
, "castleInCheck");
111 // TODO: allow pieces to "commit suicide"? (Currently yes except king)
113 // Remove moves which let the king mate-captured:
114 if (moves
.length
== 0) return [];
115 const color
= this.turn
;
116 const oppCol
= V
.GetOppCol(color
);
117 return moves
.filter(m
=> {
120 if (this.underCheck(color
)) {
122 const attacked
= this.kingPos
[color
];
123 // Try to find a move to escape check
124 // TODO: very inefficient method.
125 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
126 for (let j
=0; j
<V
.size
.y
; j
++) {
127 if (this.getColor(i
,j
) == color
) {
129 // Artficial turn change to "play twice":
131 switch (this.getPiece(i
, j
)) {
133 emoves
= this.getPotentialPawnMoves([i
, j
]);
136 emoves
= this.getPotentialRookMoves([i
, j
]);
139 emoves
= this.getPotentialKnightMoves([i
, j
]);
142 emoves
= this.getPotentialBishopMoves([i
, j
]);
145 emoves
= this.getPotentialQueenMoves([i
, j
]);
148 emoves
= this.getPotentialKingMoves([i
, j
]);
152 for (let em
of emoves
) {
153 V
.PlayOnBoard(this.board
, em
);
155 if (em
.start
.x
== attacked
[0] && em
.start
.y
== attacked
[1])
157 sq
= [em
.appear
[0].x
, em
.appear
[0].y
];
158 if (!this.isAttacked(sq
, oppCol
))
160 V
.UndoOnBoard(this.board
, em
);
162 // No need to explore more moves
175 super.postPlay(move);
176 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
177 for (let i
= 1; i
<move.vanish
.length
; i
++) {
178 const v
= move.vanish
[i
];
179 // Did opponent king disappeared?
181 this.kingPos
[this.turn
] = [-1, -1];
183 else if (v
.p
== V
.ROOK
) {
184 if (v
.y
< this.INIT_COL_KING
[v
.c
])
185 this.castleFlags
[v
.c
][0] = 8;
187 // v.y > this.INIT_COL_KING[v.c]
188 this.castleFlags
[v
.c
][1] = 8;
196 const oppCol
= this.turn
;
197 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
198 // Did opponent king disappeared?
199 const psq
= move.vanish
.find(v
=> v
.p
== V
.KING
&& v
.c
== oppCol
)
201 this.kingPos
[psq
.c
] = [psq
.x
, psq
.y
];
206 const color
= this.turn
;
207 const kp
= this.kingPos
[color
];
210 return color
== "w" ? "0-1" : "1-0";
211 if (this.atLeastOneMove()) return "*";
212 // Kings still there, no moves:
216 static get SEARCH_DEPTH() {
221 let notation
= super.getNotation(move);
222 // Add a capture mark (not describing what is captured...):
223 if (move.vanish
.length
> 1 && move.appear
.length
== 1) {
224 if (!!(notation
.match(/^[a-h]x/)))
225 // Pawn capture: remove initial "b" in bxc4 for example
226 notation
= notation
.substr(1);
227 notation
= notation
.replace("x","") + "X";