1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class Allmate2Rules
extends ChessRules
{
5 static get HasEnpassant() {
14 static GenRandInitFen(randomness
) {
15 return ChessRules
.GenRandInitFen(randomness
).slice(0, -2);
18 getPotentialMovesFrom([x
, y
]) {
19 let moves
= super.getPotentialMovesFrom([x
, y
]);
20 // Remove standard captures (without removing castling):
21 moves
= moves
.filter(m
=> {
22 return m
.vanish
.length
== 1 || m
.appear
.length
== 2;
25 // Augment moves with "mate-captures":
26 // TODO: this is coded in a highly inefficient way...
27 const color
= this.turn
;
28 const oppCol
= V
.GetOppCol(this.turn
);
32 // 1) What is attacked?
34 for (let i
=0; i
<V
.size
.x
; i
++) {
35 for (let j
=0; j
<V
.size
.y
; j
++) {
36 if (this.getColor(i
,j
) == oppCol
&& this.isAttacked([i
,j
], color
))
37 attacked
[i
+"_"+j
] = [i
,j
];
41 // 2) Among attacked pieces, which cannot escape capture?
42 // --> without (normal-)capturing: difference with Allmate1 variant
43 // Avoid "oppMoves = this.getAllValidMoves();" => infinite recursion
44 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
45 for (let j
=0; j
<V
.size
.y
; j
++) {
46 if (this.getColor(i
,j
) == oppCol
) {
48 switch (this.getPiece(i
, j
)) {
50 oppMoves
= this.getPotentialPawnMoves([i
, j
]);
53 oppMoves
= this.getPotentialRookMoves([i
, j
]);
56 oppMoves
= this.getPotentialKnightMoves([i
, j
]);
59 oppMoves
= this.getPotentialBishopMoves([i
, j
]);
62 oppMoves
= this.getPotentialQueenMoves([i
, j
]);
65 // Do not allow castling to escape from check
66 oppMoves
= super.getSlideNJumpMoves(
68 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
73 for (let om
of oppMoves
) {
74 if (om
.vanish
.length
== 2)
75 // Skip captures: forbidden in this mode
77 V
.PlayOnBoard(this.board
, om
);
78 Object
.values(attacked
).forEach(sq
=> {
79 const origSq
= [sq
[0], sq
[1]];
80 if (om
.start
.x
== sq
[0] && om
.start
.y
== sq
[1])
82 sq
= [om
.appear
[0].x
, om
.appear
[0].y
];
83 if (!this.isAttacked(sq
, color
))
84 delete attacked
[origSq
[0]+"_"+origSq
[1]];
86 V
.UndoOnBoard(this.board
, om
);
87 if (Object
.keys(attacked
).length
== 0)
88 // No need to explore more moves
96 // 3) Add mate-captures:
97 Object
.values(attacked
).forEach(sq
=> {
98 m
.vanish
.push(new PiPo({
102 p: this.getPiece(sq
[0], sq
[1])
110 // No "under check" conditions in castling
112 return super.getCastleMoves(sq
, null, "castleInCheck");
115 // TODO: allow pieces to "commit suicide"? (Currently yes except king)
117 // Remove moves which let the king mate-captured:
118 if (moves
.length
== 0) return [];
119 const color
= this.turn
;
120 const oppCol
= V
.GetOppCol(color
);
121 return moves
.filter(m
=> {
124 if (this.underCheck(color
)) {
126 const attacked
= this.kingPos
[color
];
127 // Try to find a move to escape check
128 // TODO: very inefficient method.
129 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
130 for (let j
=0; j
<V
.size
.y
; j
++) {
131 if (this.getColor(i
,j
) == color
) {
133 // Artficial turn change to "play twice":
135 switch (this.getPiece(i
, j
)) {
137 emoves
= this.getPotentialPawnMoves([i
, j
]);
140 emoves
= this.getPotentialRookMoves([i
, j
]);
143 emoves
= this.getPotentialKnightMoves([i
, j
]);
146 emoves
= this.getPotentialBishopMoves([i
, j
]);
149 emoves
= this.getPotentialQueenMoves([i
, j
]);
152 emoves
= this.getPotentialKingMoves([i
, j
]);
156 for (let em
of emoves
) {
157 V
.PlayOnBoard(this.board
, em
);
159 if (em
.start
.x
== attacked
[0] && em
.start
.y
== attacked
[1])
161 sq
= [em
.appear
[0].x
, em
.appear
[0].y
];
162 if (!this.isAttacked(sq
, oppCol
))
164 V
.UndoOnBoard(this.board
, em
);
166 // No need to explore more moves
179 super.postPlay(move);
180 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
181 for (let i
= 1; i
<move.vanish
.length
; i
++) {
182 const v
= move.vanish
[i
];
183 // Did opponent king disappeared?
185 this.kingPos
[this.turn
] = [-1, -1];
187 else if (v
.p
== V
.ROOK
) {
188 if (v
.y
< this.kingPos
[v
.c
][1])
189 this.castleFlags
[v
.c
][0] = 8;
191 // v.y > this.kingPos[v.c][1]
192 this.castleFlags
[v
.c
][1] = 8;
200 const oppCol
= this.turn
;
201 if (move.vanish
.length
>= 2 && move.appear
.length
== 1) {
202 // Did opponent king disappeared?
203 const psq
= move.vanish
.find(v
=> v
.p
== V
.KING
&& v
.c
== oppCol
)
205 this.kingPos
[psq
.c
] = [psq
.x
, psq
.y
];
210 const color
= this.turn
;
211 const kp
= this.kingPos
[color
];
214 return color
== "w" ? "0-1" : "1-0";
215 if (this.atLeastOneMove()) return "*";
216 // Kings still there, no moves:
220 static get SEARCH_DEPTH() {
225 let notation
= super.getNotation(move);
226 // Add a capture mark (not describing what is captured...):
227 if (move.vanish
.length
> 1 && move.appear
.length
== 1) {
228 if (!!(notation
.match(/^[a-h]x/)))
229 // Pawn capture: remove initial "b" in bxc4 for example
230 notation
= notation
.substr(1);
231 notation
= notation
.replace("x","") + "X";