1 import { ChessRules
} from "@/base_rules";
2 // NOTE: alternative implementation, probably cleaner = use only 1 board
3 export const VariantRules
= class AliceRules
extends ChessRules
5 static get ALICE_PIECES()
16 static get ALICE_CODES()
30 return (Object
.keys(this.ALICE_PIECES
).includes(b
[1]) ? "Alice/" : "") + b
;
35 return ChessRules
.PIECES
.concat(Object
.keys(V
.ALICE_PIECES
));
38 setOtherVariables(fen
)
40 super.setOtherVariables(fen
);
41 const rows
= V
.ParseFen(fen
).position
.split("/");
42 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0)
44 // INIT_COL_XXX won't be required if Alice kings are found (means 'king moved')
45 for (let i
=0; i
<rows
.length
; i
++)
47 let k
= 0; //column index on board
48 for (let j
=0; j
<rows
[i
].length
; j
++)
50 switch (rows
[i
].charAt(j
))
53 this.kingPos
['b'] = [i
,k
];
56 this.kingPos
['w'] = [i
,k
];
59 const num
= parseInt(rows
[i
].charAt(j
));
69 // Return the (standard) color+piece notation at a square for a board
70 getSquareOccupation(i
, j
, mirrorSide
)
72 const piece
= this.getPiece(i
,j
);
73 if (mirrorSide
==1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
74 return this.board
[i
][j
];
75 else if (mirrorSide
==2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
76 return this.getColor(i
,j
) + V
.ALICE_PIECES
[piece
];
80 // Build board of the given (mirror)side
81 getSideBoard(mirrorSide
)
83 // Build corresponding board from complete board
84 let sideBoard
= doubleArray(V
.size
.x
, V
.size
.y
, "");
85 for (let i
=0; i
<V
.size
.x
; i
++)
87 for (let j
=0; j
<V
.size
.y
; j
++)
88 sideBoard
[i
][j
] = this.getSquareOccupation(i
, j
, mirrorSide
);
93 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
94 getPotentialMovesFrom([x
,y
], sideBoard
)
96 const pieces
= Object
.keys(V
.ALICE_CODES
);
97 const codes
= Object
.keys(V
.ALICE_PIECES
);
98 const mirrorSide
= (pieces
.includes(this.getPiece(x
,y
)) ? 1 : 2);
99 const color
= this.getColor(x
,y
);
101 // Search valid moves on sideBoard
102 let saveBoard
= this.board
;
103 this.board
= sideBoard
|| this.getSideBoard(mirrorSide
);
104 let moves
= super.getPotentialMovesFrom([x
,y
])
106 // Filter out king moves which result in under-check position on
107 // current board (before mirror traversing)
108 let aprioriValid
= true;
109 if (m
.appear
[0].p
== V
.KING
)
112 if (this.underCheck(color
))
113 aprioriValid
= false;
118 this.board
= saveBoard
;
120 // Finally filter impossible moves
121 let res
= moves
.filter(m
=> {
122 if (m
.appear
.length
== 2) //castle
124 // appear[i] must be an empty square on the other board
125 for (let psq
of m
.appear
)
127 if (this.getSquareOccupation(psq
.x
,psq
.y
,3-mirrorSide
) != V
.EMPTY
)
131 else if (this.board
[m
.end
.x
][m
.end
.y
] != V
.EMPTY
)
133 // Attempt to capture
134 const piece
= this.getPiece(m
.end
.x
,m
.end
.y
);
135 if ((mirrorSide
==1 && codes
.includes(piece
))
136 || (mirrorSide
==2 && pieces
.includes(piece
)))
141 // If the move is computed on board1, m.appear change for Alice pieces.
144 m
.appear
.forEach(psq
=> { //forEach: castling taken into account
145 psq
.p
= V
.ALICE_CODES
[psq
.p
]; //goto board2
148 else //move on board2: mark vanishing pieces as Alice
150 m
.vanish
.forEach(psq
=> {
151 psq
.p
= V
.ALICE_CODES
[psq
.p
];
154 // Fix en-passant captures
155 if (m
.vanish
[0].p
== V
.PAWN
&& m
.vanish
.length
== 2
156 && this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
)
158 m
.vanish
[1].c
= V
.GetOppCol(this.getColor(x
,y
));
159 // In the special case of en-passant, if
160 // - board1 takes board2 : vanish[1] --> Alice
161 // - board2 takes board1 : vanish[1] --> normal
162 let van
= m
.vanish
[1];
163 if (mirrorSide
==1 && codes
.includes(this.getPiece(van
.x
,van
.y
)))
164 van
.p
= V
.ALICE_CODES
[van
.p
];
165 else if (mirrorSide
==2 && pieces
.includes(this.getPiece(van
.x
,van
.y
)))
166 van
.p
= V
.ALICE_PIECES
[van
.p
];
175 if (moves
.length
== 0)
177 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
178 const color
= this.turn
;
179 return moves
.filter(m
=> {
180 this.playSide(m
, sideBoard
); //no need to track flags
181 const res
= !this.underCheck(color
, sideBoard
);
182 this.undoSide(m
, sideBoard
);
189 const color
= this.turn
;
190 const oppCol
= V
.GetOppCol(color
);
191 var potentialMoves
= [];
192 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
193 for (var i
=0; i
<V
.size
.x
; i
++)
195 for (var j
=0; j
<V
.size
.y
; j
++)
197 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
200 Object
.keys(V
.ALICE_CODES
).includes(this.getPiece(i
,j
))
203 Array
.prototype.push
.apply(potentialMoves
,
204 this.getPotentialMovesFrom([i
,j
], sideBoard
[mirrorSide
-1]));
208 return this.filterValid(potentialMoves
, sideBoard
);
211 // Play on sideboards [TODO: only one sideBoard required]
212 playSide(move, sideBoard
)
214 const pieces
= Object
.keys(V
.ALICE_CODES
);
215 move.vanish
.forEach(psq
=> {
216 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
217 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
219 move.appear
.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
;
224 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
228 // Undo on sideboards
229 undoSide(move, sideBoard
)
231 const pieces
= Object
.keys(V
.ALICE_CODES
);
232 move.appear
.forEach(psq
=> {
233 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
234 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
236 move.vanish
.forEach(psq
=> {
237 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
238 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
239 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
241 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
245 underCheck(color
, sideBoard
) //sideBoard arg always provided
247 const kp
= this.kingPos
[color
];
248 const mirrorSide
= (sideBoard
[0][kp
[0]][kp
[1]] != V
.EMPTY
? 1 : 2);
249 let saveBoard
= this.board
;
250 this.board
= sideBoard
[mirrorSide
-1];
251 let res
= this.isAttacked(kp
, [V
.GetOppCol(color
)]);
252 this.board
= saveBoard
;
256 getCheckSquares(color
)
258 const pieces
= Object
.keys(V
.ALICE_CODES
);
259 const kp
= this.kingPos
[color
];
260 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
261 let sideBoard
= this.getSideBoard(mirrorSide
);
262 let saveBoard
= this.board
;
263 this.board
= sideBoard
;
264 let res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
265 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
267 this.board
= saveBoard
;
271 updateVariables(move)
273 super.updateVariables(move); //standard king
274 const piece
= move.vanish
[0].p
;
275 const c
= move.vanish
[0].c
;
279 this.kingPos
[c
][0] = move.appear
[0].x
;
280 this.kingPos
[c
][1] = move.appear
[0].y
;
281 this.castleFlags
[c
] = [false,false];
285 unupdateVariables(move)
287 super.unupdateVariables(move);
288 const c
= move.vanish
[0].c
;
289 if (move.vanish
[0].p
== "l")
290 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
295 const pieces
= Object
.keys(V
.ALICE_CODES
);
296 const color
= this.turn
;
297 const kp
= this.kingPos
[color
];
298 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
299 let sideBoard
= this.getSideBoard(mirrorSide
);
300 let saveBoard
= this.board
;
301 this.board
= sideBoard
;
303 if (!this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
306 res
= (color
== "w" ? "0-1" : "1-0");
307 this.board
= saveBoard
;
313 return Object
.assign(
328 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
330 if (move.end
.y
< move.start
.y
)
336 const finalSquare
= V
.CoordsToSquare(move.end
);
337 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
339 const captureMark
= (move.vanish
.length
> move.appear
.length
? "x" : "");
341 if (["p","s"].includes(piece
) && captureMark
.length
== 1)
342 pawnMark
= V
.CoordToColumn(move.start
.y
); //start column
344 // Piece or pawn movement
345 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
346 if (['s','p'].includes(piece
) && !['s','p'].includes(move.appear
[0].p
))
349 notation
+= "=" + move.appear
[0].p
.toUpperCase();