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
;
239 const color
= this.turn
;
240 const pieces
= Object
.keys(V
.ALICE_CODES
);
241 const kp
= this.kingPos
[color
];
242 const mirrorSide
= pieces
.includes(this.getPiece(kp
[0], kp
[1])) ? 1 : 2;
243 let sideBoard
= this.getSideBoard(mirrorSide
);
244 let saveBoard
= this.board
;
245 this.board
= sideBoard
;
246 let res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
247 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))]
249 this.board
= saveBoard
;
254 super.postPlay(move); //standard king
255 const piece
= move.vanish
[0].p
;
256 const c
= move.vanish
[0].c
;
259 this.kingPos
[c
][0] = move.appear
[0].x
;
260 this.kingPos
[c
][1] = move.appear
[0].y
;
261 this.castleFlags
[c
] = [8, 8];
266 super.postUndo(move);
267 const c
= move.vanish
[0].c
;
268 if (move.vanish
[0].p
== "l")
269 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
273 if (this.atLeastOneMove()) return "*";
274 const pieces
= Object
.keys(V
.ALICE_CODES
);
275 const color
= this.turn
;
276 const kp
= this.kingPos
[color
];
277 const mirrorSide
= pieces
.includes(this.getPiece(kp
[0], kp
[1])) ? 1 : 2;
278 let sideBoard
= this.getSideBoard(mirrorSide
);
279 let saveBoard
= this.board
;
280 this.board
= sideBoard
;
282 if (!this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
284 else res
= color
== "w" ? "0-1" : "1-0";
285 this.board
= saveBoard
;
289 static get VALUES() {
290 return Object
.assign(
303 static get SEARCH_DEPTH() {
308 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
) {
309 if (move.end
.y
< move.start
.y
) return "0-0-0";
313 const finalSquare
= V
.CoordsToSquare(move.end
);
314 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
316 const captureMark
= move.vanish
.length
> move.appear
.length
? "x" : "";
318 if (["p", "s"].includes(piece
) && captureMark
.length
== 1)
319 pawnMark
= V
.CoordToColumn(move.start
.y
); //start column
321 // Piece or pawn movement
322 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
323 if (["s", "p"].includes(piece
) && !["s", "p"].includes(move.appear
[0].p
)) {
325 notation
+= "=" + move.appear
[0].p
.toUpperCase();