1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 // NOTE: alternative implementation, probably cleaner = use only 1 board
5 // TODO? atLeastOneMove() would be more efficient if rewritten here
6 // (less sideBoard computations)
7 export class AliceRules
extends ChessRules
{
8 static get ALICE_PIECES() {
18 static get ALICE_CODES() {
30 return ChessRules
.PIECES
.concat(Object
.keys(V
.ALICE_PIECES
));
34 return (Object
.keys(V
.ALICE_PIECES
).includes(b
[1]) ? "Alice/" : "") + b
;
37 setOtherVariables(fen
) {
38 super.setOtherVariables(fen
);
39 const rows
= V
.ParseFen(fen
).position
.split("/");
40 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0) {
41 // INIT_COL_XXX won't be required if Alice kings are found
42 // (it means 'king moved')
43 for (let i
= 0; i
< rows
.length
; i
++) {
44 let k
= 0; //column index on board
45 for (let j
= 0; j
< rows
[i
].length
; j
++) {
46 switch (rows
[i
].charAt(j
)) {
48 this.kingPos
["b"] = [i
, k
];
51 this.kingPos
["w"] = [i
, k
];
54 const num
= parseInt(rows
[i
].charAt(j
));
55 if (!isNaN(num
)) k
+= num
- 1;
64 // Return the (standard) color+piece notation at a square for a board
65 getSquareOccupation(i
, j
, mirrorSide
) {
66 const piece
= this.getPiece(i
, j
);
67 if (mirrorSide
== 1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
68 return this.board
[i
][j
];
69 if (mirrorSide
== 2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
70 return this.getColor(i
, j
) + V
.ALICE_PIECES
[piece
];
74 // Build board of the given (mirror)side
75 getSideBoard(mirrorSide
) {
76 // Build corresponding board from complete board
77 let sideBoard
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, "");
78 for (let i
= 0; i
< V
.size
.x
; i
++) {
79 for (let j
= 0; j
< V
.size
.y
; j
++)
80 sideBoard
[i
][j
] = this.getSquareOccupation(i
, j
, mirrorSide
);
85 // NOTE: castle & enPassant
86 // https://www.chessvariants.com/other.dir/alice.html
87 getPotentialMovesFrom([x
, y
], sideBoard
) {
88 const pieces
= Object
.keys(V
.ALICE_CODES
);
89 const codes
= Object
.keys(V
.ALICE_PIECES
);
90 const mirrorSide
= pieces
.includes(this.getPiece(x
, y
)) ? 1 : 2;
91 if (!sideBoard
) sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
92 const color
= this.getColor(x
, y
);
94 // Search valid moves on sideBoard
95 const saveBoard
= this.board
;
96 this.board
= sideBoard
[mirrorSide
- 1];
97 const moves
= super.getPotentialMovesFrom([x
, y
]).filter(m
=> {
98 // Filter out king moves which result in under-check position on
99 // current board (before mirror traversing)
100 let aprioriValid
= true;
101 if (m
.appear
[0].p
== V
.KING
) {
103 if (this.underCheck(color
, sideBoard
)) aprioriValid
= false;
108 this.board
= saveBoard
;
110 // Finally filter impossible moves
111 const res
= moves
.filter(m
=> {
112 if (m
.appear
.length
== 2) {
113 // Castle: appear[i] must be an empty square on the other board
114 for (let psq
of m
.appear
) {
116 this.getSquareOccupation(psq
.x
, psq
.y
, 3 - mirrorSide
) != V
.EMPTY
121 } else if (this.board
[m
.end
.x
][m
.end
.y
] != V
.EMPTY
) {
122 // Attempt to capture
123 const piece
= this.getPiece(m
.end
.x
, m
.end
.y
);
125 (mirrorSide
== 1 && codes
.includes(piece
)) ||
126 (mirrorSide
== 2 && pieces
.includes(piece
))
131 // If the move is computed on board1, m.appear change for Alice pieces.
132 if (mirrorSide
== 1) {
133 m
.appear
.forEach(psq
=> {
134 // forEach: castling taken into account
135 psq
.p
= V
.ALICE_CODES
[psq
.p
]; //goto board2
139 // Move on board2: mark vanishing pieces as Alice
140 m
.vanish
.forEach(psq
=> {
141 psq
.p
= V
.ALICE_CODES
[psq
.p
];
144 // Fix en-passant captures
146 m
.vanish
[0].p
== V
.PAWN
&&
147 m
.vanish
.length
== 2 &&
148 this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
150 m
.vanish
[1].c
= V
.GetOppCol(this.getColor(x
, y
));
151 // In the special case of en-passant, if
152 // - board1 takes board2 : vanish[1] --> Alice
153 // - board2 takes board1 : vanish[1] --> normal
154 let van
= m
.vanish
[1];
155 if (mirrorSide
== 1 && codes
.includes(this.getPiece(van
.x
, van
.y
)))
156 van
.p
= V
.ALICE_CODES
[van
.p
];
159 pieces
.includes(this.getPiece(van
.x
, van
.y
))
161 van
.p
= V
.ALICE_PIECES
[van
.p
];
168 filterValid(moves
, sideBoard
) {
169 if (moves
.length
== 0) return [];
170 if (!sideBoard
) sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
171 const color
= this.turn
;
172 return moves
.filter(m
=> {
173 this.playSide(m
, sideBoard
); //no need to track flags
174 const res
= !this.underCheck(color
, sideBoard
);
175 this.undoSide(m
, sideBoard
);
181 const color
= this.turn
;
182 let potentialMoves
= [];
183 const sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
184 for (var i
= 0; i
< V
.size
.x
; i
++) {
185 for (var j
= 0; j
< V
.size
.y
; j
++) {
186 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
187 Array
.prototype.push
.apply(
189 this.getPotentialMovesFrom([i
, j
], sideBoard
)
194 return this.filterValid(potentialMoves
, sideBoard
);
197 // Play on sideboards [TODO: only one sideBoard required]
198 playSide(move, sideBoard
) {
199 const pieces
= Object
.keys(V
.ALICE_CODES
);
200 move.vanish
.forEach(psq
=> {
201 const mirrorSide
= pieces
.includes(psq
.p
) ? 1 : 2;
202 sideBoard
[mirrorSide
- 1][psq
.x
][psq
.y
] = V
.EMPTY
;
204 move.appear
.forEach(psq
=> {
205 const mirrorSide
= pieces
.includes(psq
.p
) ? 1 : 2;
206 const piece
= mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
];
207 sideBoard
[mirrorSide
- 1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
208 if (piece
== V
.KING
) this.kingPos
[psq
.c
] = [psq
.x
, psq
.y
];
212 // Undo on sideboards
213 undoSide(move, sideBoard
) {
214 const pieces
= Object
.keys(V
.ALICE_CODES
);
215 move.appear
.forEach(psq
=> {
216 const mirrorSide
= pieces
.includes(psq
.p
) ? 1 : 2;
217 sideBoard
[mirrorSide
- 1][psq
.x
][psq
.y
] = V
.EMPTY
;
219 move.vanish
.forEach(psq
=> {
220 const mirrorSide
= pieces
.includes(psq
.p
) ? 1 : 2;
221 const piece
= mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
];
222 sideBoard
[mirrorSide
- 1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
223 if (piece
== V
.KING
) this.kingPos
[psq
.c
] = [psq
.x
, psq
.y
];
227 // sideBoard: arg containing both boards (see getAllValidMoves())
228 underCheck(color
, sideBoard
) {
229 const kp
= this.kingPos
[color
];
230 const mirrorSide
= sideBoard
[0][kp
[0]][kp
[1]] != V
.EMPTY
? 1 : 2;
231 let saveBoard
= this.board
;
232 this.board
= sideBoard
[mirrorSide
- 1];
233 let res
= this.isAttacked(kp
, [V
.GetOppCol(color
)]);
234 this.board
= saveBoard
;
238 getCheckSquares(color
) {
239 const pieces
= Object
.keys(V
.ALICE_CODES
);
240 const kp
= this.kingPos
[color
];
241 const mirrorSide
= pieces
.includes(this.getPiece(kp
[0], kp
[1])) ? 1 : 2;
242 let sideBoard
= this.getSideBoard(mirrorSide
);
243 let saveBoard
= this.board
;
244 this.board
= sideBoard
;
245 let res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
246 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))]
248 this.board
= saveBoard
;
253 super.postPlay(move); //standard king
254 const piece
= move.vanish
[0].p
;
255 const c
= move.vanish
[0].c
;
258 this.kingPos
[c
][0] = move.appear
[0].x
;
259 this.kingPos
[c
][1] = move.appear
[0].y
;
260 this.castleFlags
[c
] = [8, 8];
265 super.postUndo(move);
266 const c
= move.vanish
[0].c
;
267 if (move.vanish
[0].p
== "l")
268 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
272 if (this.atLeastOneMove()) return "*";
273 const pieces
= Object
.keys(V
.ALICE_CODES
);
274 const color
= this.turn
;
275 const kp
= this.kingPos
[color
];
276 const mirrorSide
= pieces
.includes(this.getPiece(kp
[0], kp
[1])) ? 1 : 2;
277 let sideBoard
= this.getSideBoard(mirrorSide
);
278 let saveBoard
= this.board
;
279 this.board
= sideBoard
;
281 if (!this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
283 else res
= color
== "w" ? "0-1" : "1-0";
284 this.board
= saveBoard
;
288 static get VALUES() {
289 return Object
.assign(
302 static get SEARCH_DEPTH() {
307 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
) {
308 if (move.end
.y
< move.start
.y
) return "0-0-0";
312 const finalSquare
= V
.CoordsToSquare(move.end
);
313 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
315 const captureMark
= move.vanish
.length
> move.appear
.length
? "x" : "";
317 if (["p", "s"].includes(piece
) && captureMark
.length
== 1)
318 pawnMark
= V
.CoordToColumn(move.start
.y
); //start column
320 // Piece or pawn movement
321 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
322 if (["s", "p"].includes(piece
) && !["s", "p"].includes(move.appear
[0].p
)) {
324 notation
+= "=" + move.appear
[0].p
.toUpperCase();