04b4a3466169df2698bb236796ea41cdd636afdd
1 // NOTE: alternative implementation, probably cleaner = use only 1 board
2 class AliceRules
extends ChessRules
4 static get ALICE_PIECES()
15 static get ALICE_CODES()
29 return (Object
.keys(this.ALICE_PIECES
).includes(b
[1]) ? "Alice/" : "") + b
;
34 return ChessRules
.PIECES
.concat(Object
.keys(V
.ALICE_PIECES
));
37 setOtherVariables(fen
)
39 super.setOtherVariables(fen
);
40 const rows
= V
.ParseFen(fen
).position
.split("/");
41 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0)
43 // INIT_COL_XXX won't be required if Alice kings are found (means 'king moved')
44 for (let i
=0; i
<rows
.length
; i
++)
46 let k
= 0; //column index on board
47 for (let j
=0; j
<rows
[i
].length
; j
++)
49 switch (rows
[i
].charAt(j
))
52 this.kingPos
['b'] = [i
,k
];
55 this.kingPos
['w'] = [i
,k
];
58 const num
= parseInt(rows
[i
].charAt(j
));
68 // Return the (standard) color+piece notation at a square for a board
69 getSquareOccupation(i
, j
, mirrorSide
)
71 const piece
= this.getPiece(i
,j
);
72 if (mirrorSide
==1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
73 return this.board
[i
][j
];
74 else if (mirrorSide
==2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
75 return this.getColor(i
,j
) + V
.ALICE_PIECES
[piece
];
79 // Build board of the given (mirror)side
80 getSideBoard(mirrorSide
)
82 // Build corresponding board from complete board
83 let sideBoard
= doubleArray(V
.size
.x
, V
.size
.y
, "");
84 for (let i
=0; i
<V
.size
.x
; i
++)
86 for (let j
=0; j
<V
.size
.y
; j
++)
87 sideBoard
[i
][j
] = this.getSquareOccupation(i
, j
, mirrorSide
);
92 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
93 getPotentialMovesFrom([x
,y
], sideBoard
)
95 const pieces
= Object
.keys(V
.ALICE_CODES
);
96 const codes
= Object
.keys(V
.ALICE_PIECES
);
97 const mirrorSide
= (pieces
.includes(this.getPiece(x
,y
)) ? 1 : 2);
98 const color
= this.getColor(x
,y
);
100 // Search valid moves on sideBoard
101 let saveBoard
= this.board
;
102 this.board
= sideBoard
|| this.getSideBoard(mirrorSide
);
103 let moves
= super.getPotentialMovesFrom([x
,y
])
105 // Filter out king moves which result in under-check position on
106 // current board (before mirror traversing)
107 let aprioriValid
= true;
108 if (m
.appear
[0].p
== V
.KING
)
111 if (this.underCheck(color
))
112 aprioriValid
= false;
117 this.board
= saveBoard
;
119 // Finally filter impossible moves
120 let res
= moves
.filter(m
=> {
121 if (m
.appear
.length
== 2) //castle
123 // appear[i] must be an empty square on the other board
124 for (let psq
of m
.appear
)
126 if (this.getSquareOccupation(psq
.x
,psq
.y
,3-mirrorSide
) != V
.EMPTY
)
130 else if (this.board
[m
.end
.x
][m
.end
.y
] != V
.EMPTY
)
132 // Attempt to capture
133 const piece
= this.getPiece(m
.end
.x
,m
.end
.y
);
134 if ((mirrorSide
==1 && codes
.includes(piece
))
135 || (mirrorSide
==2 && pieces
.includes(piece
)))
140 // If the move is computed on board1, m.appear change for Alice pieces.
143 m
.appear
.forEach(psq
=> { //forEach: castling taken into account
144 psq
.p
= V
.ALICE_CODES
[psq
.p
]; //goto board2
147 else //move on board2: mark vanishing pieces as Alice
149 m
.vanish
.forEach(psq
=> {
150 psq
.p
= V
.ALICE_CODES
[psq
.p
];
153 // Fix en-passant captures
154 if (m
.vanish
[0].p
== V
.PAWN
&& m
.vanish
.length
== 2
155 && this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
)
157 m
.vanish
[1].c
= V
.GetOppCol(this.getColor(x
,y
));
158 // In the special case of en-passant, if
159 // - board1 takes board2 : vanish[1] --> Alice
160 // - board2 takes board1 : vanish[1] --> normal
161 let van
= m
.vanish
[1];
162 if (mirrorSide
==1 && codes
.includes(this.getPiece(van
.x
,van
.y
)))
163 van
.p
= V
.ALICE_CODES
[van
.p
];
164 else if (mirrorSide
==2 && pieces
.includes(this.getPiece(van
.x
,van
.y
)))
165 van
.p
= V
.ALICE_PIECES
[van
.p
];
174 if (moves
.length
== 0)
176 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
177 const color
= this.turn
;
178 return moves
.filter(m
=> {
179 this.playSide(m
, sideBoard
); //no need to track flags
180 const res
= !this.underCheck(color
, sideBoard
);
181 this.undoSide(m
, sideBoard
);
188 const color
= this.turn
;
189 const oppCol
= V
.GetOppCol(color
);
190 var potentialMoves
= [];
191 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
192 for (var i
=0; i
<V
.size
.x
; i
++)
194 for (var j
=0; j
<V
.size
.y
; j
++)
196 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
199 Object
.keys(V
.ALICE_CODES
).includes(this.getPiece(i
,j
))
202 Array
.prototype.push
.apply(potentialMoves
,
203 this.getPotentialMovesFrom([i
,j
], sideBoard
[mirrorSide
-1]));
207 return this.filterValid(potentialMoves
, sideBoard
);
210 // Play on sideboards [TODO: only one sideBoard required]
211 playSide(move, sideBoard
)
213 const pieces
= Object
.keys(V
.ALICE_CODES
);
214 move.vanish
.forEach(psq
=> {
215 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
216 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
218 move.appear
.forEach(psq
=> {
219 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
220 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
221 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
223 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
227 // Undo on sideboards
228 undoSide(move, sideBoard
)
230 const pieces
= Object
.keys(V
.ALICE_CODES
);
231 move.appear
.forEach(psq
=> {
232 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
233 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
235 move.vanish
.forEach(psq
=> {
236 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
237 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
238 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
240 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
244 underCheck(color
, sideBoard
) //sideBoard arg always provided
246 const kp
= this.kingPos
[color
];
247 const mirrorSide
= (sideBoard
[0][kp
[0]][kp
[1]] != V
.EMPTY
? 1 : 2);
248 let saveBoard
= this.board
;
249 this.board
= sideBoard
[mirrorSide
-1];
250 let res
= this.isAttacked(kp
, [V
.GetOppCol(color
)]);
251 this.board
= saveBoard
;
255 getCheckSquares(color
)
257 const pieces
= Object
.keys(V
.ALICE_CODES
);
258 const kp
= this.kingPos
[color
];
259 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
260 let sideBoard
= this.getSideBoard(mirrorSide
);
261 let saveBoard
= this.board
;
262 this.board
= sideBoard
;
263 let res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
264 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
266 this.board
= saveBoard
;
270 updateVariables(move)
272 super.updateVariables(move); //standard king
273 const piece
= move.vanish
[0].p
;
274 const c
= move.vanish
[0].c
;
278 this.kingPos
[c
][0] = move.appear
[0].x
;
279 this.kingPos
[c
][1] = move.appear
[0].y
;
280 this.castleFlags
[c
] = [false,false];
284 unupdateVariables(move)
286 super.unupdateVariables(move);
287 const c
= move.vanish
[0].c
;
288 if (move.vanish
[0].p
== "l")
289 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
294 const pieces
= Object
.keys(V
.ALICE_CODES
);
295 const color
= this.turn
;
296 const kp
= this.kingPos
[color
];
297 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
298 let sideBoard
= this.getSideBoard(mirrorSide
);
299 let saveBoard
= this.board
;
300 this.board
= sideBoard
;
302 if (!this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
305 res
= (color
== "w" ? "0-1" : "1-0");
306 this.board
= saveBoard
;
312 return Object
.assign(
327 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
329 if (move.end
.y
< move.start
.y
)
335 const finalSquare
= V
.CoordsToSquare(move.end
);
336 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
338 const captureMark
= (move.vanish
.length
> move.appear
.length
? "x" : "");
340 if (["p","s"].includes(piece
) && captureMark
.length
== 1)
341 pawnMark
= V
.CoordToColumn(move.start
.y
); //start column
343 // Piece or pawn movement
344 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
345 if (['s','p'].includes(piece
) && !['s','p'].includes(move.appear
[0].p
))
348 notation
+= "=" + move.appear
[0].p
.toUpperCase();