1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 // NOTE: alternative implementation, probably cleaner = use only 1 board
5 export const VariantRules
= class AliceRules
extends ChessRules
7 static get ALICE_PIECES()
18 static get ALICE_CODES()
32 return (Object
.keys(this.ALICE_PIECES
).includes(b
[1]) ? "Alice/" : "") + b
;
37 return ChessRules
.PIECES
.concat(Object
.keys(V
.ALICE_PIECES
));
40 setOtherVariables(fen
)
42 super.setOtherVariables(fen
);
43 const rows
= V
.ParseFen(fen
).position
.split("/");
44 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0)
46 // INIT_COL_XXX won't be required if Alice kings are found (means 'king moved')
47 for (let i
=0; i
<rows
.length
; i
++)
49 let k
= 0; //column index on board
50 for (let j
=0; j
<rows
[i
].length
; j
++)
52 switch (rows
[i
].charAt(j
))
55 this.kingPos
['b'] = [i
,k
];
58 this.kingPos
['w'] = [i
,k
];
61 const num
= parseInt(rows
[i
].charAt(j
));
71 // Return the (standard) color+piece notation at a square for a board
72 getSquareOccupation(i
, j
, mirrorSide
)
74 const piece
= this.getPiece(i
,j
);
75 if (mirrorSide
==1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
76 return this.board
[i
][j
];
77 else if (mirrorSide
==2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
78 return this.getColor(i
,j
) + V
.ALICE_PIECES
[piece
];
82 // Build board of the given (mirror)side
83 getSideBoard(mirrorSide
)
85 // Build corresponding board from complete board
86 let sideBoard
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, "");
87 for (let i
=0; i
<V
.size
.x
; i
++)
89 for (let j
=0; j
<V
.size
.y
; j
++)
90 sideBoard
[i
][j
] = this.getSquareOccupation(i
, j
, mirrorSide
);
95 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
96 getPotentialMovesFrom([x
,y
], sideBoard
)
98 const pieces
= Object
.keys(V
.ALICE_CODES
);
99 const codes
= Object
.keys(V
.ALICE_PIECES
);
100 const mirrorSide
= (pieces
.includes(this.getPiece(x
,y
)) ? 1 : 2);
101 const color
= this.getColor(x
,y
);
103 // Search valid moves on sideBoard
104 let saveBoard
= this.board
;
105 this.board
= sideBoard
|| this.getSideBoard(mirrorSide
);
106 let moves
= super.getPotentialMovesFrom([x
,y
])
108 // Filter out king moves which result in under-check position on
109 // current board (before mirror traversing)
110 let aprioriValid
= true;
111 if (m
.appear
[0].p
== V
.KING
)
114 if (this.underCheck(color
))
115 aprioriValid
= false;
120 this.board
= saveBoard
;
122 // Finally filter impossible moves
123 let res
= moves
.filter(m
=> {
124 if (m
.appear
.length
== 2) //castle
126 // appear[i] must be an empty square on the other board
127 for (let psq
of m
.appear
)
129 if (this.getSquareOccupation(psq
.x
,psq
.y
,3-mirrorSide
) != V
.EMPTY
)
133 else if (this.board
[m
.end
.x
][m
.end
.y
] != V
.EMPTY
)
135 // Attempt to capture
136 const piece
= this.getPiece(m
.end
.x
,m
.end
.y
);
137 if ((mirrorSide
==1 && codes
.includes(piece
))
138 || (mirrorSide
==2 && pieces
.includes(piece
)))
143 // If the move is computed on board1, m.appear change for Alice pieces.
146 m
.appear
.forEach(psq
=> { //forEach: castling taken into account
147 psq
.p
= V
.ALICE_CODES
[psq
.p
]; //goto board2
150 else //move on board2: mark vanishing pieces as Alice
152 m
.vanish
.forEach(psq
=> {
153 psq
.p
= V
.ALICE_CODES
[psq
.p
];
156 // Fix en-passant captures
157 if (m
.vanish
[0].p
== V
.PAWN
&& m
.vanish
.length
== 2
158 && this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
)
160 m
.vanish
[1].c
= V
.GetOppCol(this.getColor(x
,y
));
161 // In the special case of en-passant, if
162 // - board1 takes board2 : vanish[1] --> Alice
163 // - board2 takes board1 : vanish[1] --> normal
164 let van
= m
.vanish
[1];
165 if (mirrorSide
==1 && codes
.includes(this.getPiece(van
.x
,van
.y
)))
166 van
.p
= V
.ALICE_CODES
[van
.p
];
167 else if (mirrorSide
==2 && pieces
.includes(this.getPiece(van
.x
,van
.y
)))
168 van
.p
= V
.ALICE_PIECES
[van
.p
];
177 if (moves
.length
== 0)
179 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
180 const color
= this.turn
;
181 return moves
.filter(m
=> {
182 this.playSide(m
, sideBoard
); //no need to track flags
183 const res
= !this.underCheck(color
, sideBoard
);
184 this.undoSide(m
, sideBoard
);
191 const color
= this.turn
;
192 const oppCol
= V
.GetOppCol(color
);
193 var potentialMoves
= [];
194 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
195 for (var i
=0; i
<V
.size
.x
; i
++)
197 for (var j
=0; j
<V
.size
.y
; j
++)
199 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
202 Object
.keys(V
.ALICE_CODES
).includes(this.getPiece(i
,j
))
205 Array
.prototype.push
.apply(potentialMoves
,
206 this.getPotentialMovesFrom([i
,j
], sideBoard
[mirrorSide
-1]));
210 return this.filterValid(potentialMoves
, sideBoard
);
213 // Play on sideboards [TODO: only one sideBoard required]
214 playSide(move, sideBoard
)
216 const pieces
= Object
.keys(V
.ALICE_CODES
);
217 move.vanish
.forEach(psq
=> {
218 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
219 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
221 move.appear
.forEach(psq
=> {
222 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
223 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
224 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
226 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
230 // Undo on sideboards
231 undoSide(move, sideBoard
)
233 const pieces
= Object
.keys(V
.ALICE_CODES
);
234 move.appear
.forEach(psq
=> {
235 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
236 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
238 move.vanish
.forEach(psq
=> {
239 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
240 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
241 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
243 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
247 underCheck(color
, sideBoard
) //sideBoard arg always provided
249 const kp
= this.kingPos
[color
];
250 const mirrorSide
= (sideBoard
[0][kp
[0]][kp
[1]] != V
.EMPTY
? 1 : 2);
251 let saveBoard
= this.board
;
252 this.board
= sideBoard
[mirrorSide
-1];
253 let res
= this.isAttacked(kp
, [V
.GetOppCol(color
)]);
254 this.board
= saveBoard
;
258 getCheckSquares(color
)
260 const pieces
= Object
.keys(V
.ALICE_CODES
);
261 const kp
= this.kingPos
[color
];
262 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
263 let sideBoard
= this.getSideBoard(mirrorSide
);
264 let saveBoard
= this.board
;
265 this.board
= sideBoard
;
266 let res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
267 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
269 this.board
= saveBoard
;
273 updateVariables(move)
275 super.updateVariables(move); //standard king
276 const piece
= move.vanish
[0].p
;
277 const c
= move.vanish
[0].c
;
281 this.kingPos
[c
][0] = move.appear
[0].x
;
282 this.kingPos
[c
][1] = move.appear
[0].y
;
283 this.castleFlags
[c
] = [false,false];
287 unupdateVariables(move)
289 super.unupdateVariables(move);
290 const c
= move.vanish
[0].c
;
291 if (move.vanish
[0].p
== "l")
292 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
297 const pieces
= Object
.keys(V
.ALICE_CODES
);
298 const color
= this.turn
;
299 const kp
= this.kingPos
[color
];
300 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
301 let sideBoard
= this.getSideBoard(mirrorSide
);
302 let saveBoard
= this.board
;
303 this.board
= sideBoard
;
305 if (!this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
308 res
= (color
== "w" ? "0-1" : "1-0");
309 this.board
= saveBoard
;
315 return Object
.assign(
330 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
332 if (move.end
.y
< move.start
.y
)
338 const finalSquare
= V
.CoordsToSquare(move.end
);
339 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
341 const captureMark
= (move.vanish
.length
> move.appear
.length
? "x" : "");
343 if (["p","s"].includes(piece
) && captureMark
.length
== 1)
344 pawnMark
= V
.CoordToColumn(move.start
.y
); //start column
346 // Piece or pawn movement
347 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
348 if (['s','p'].includes(piece
) && !['s','p'].includes(move.appear
[0].p
))
351 notation
+= "=" + move.appear
[0].p
.toUpperCase();