5e10a059f36a0bd185ce7770fd272fef2e62d093
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 super.initVariables(fen
);
35 const fenParts
= fen
.split(" ");
36 const position
= fenParts
[0].split("/");
37 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0)
39 // INIT_COL_XXX won't be used, so no need to set them for Alice kings
40 for (let i
=0; i
<position
.length
; i
++)
42 let k
= 0; //column index on board
43 for (let j
=0; j
<position
[i
].length
; j
++)
45 switch (position
[i
].charAt(j
))
48 this.kingPos
['b'] = [i
,k
];
51 this.kingPos
['w'] = [i
,k
];
54 let num
= parseInt(position
[i
].charAt(j
));
64 // Return the (standard) color+piece notation at a square for a board
65 getSquareOccupation(i
, j
, mirrorSide
)
67 const piece
= this.getPiece(i
,j
);
68 if (mirrorSide
==1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
69 return this.board
[i
][j
];
70 else if (mirrorSide
==2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
71 return this.getColor(i
,j
) + V
.ALICE_PIECES
[piece
];
75 // Build board of the given (mirror)side
76 getSideBoard(mirrorSide
)
78 // Build corresponding board from complete board
79 let sideBoard
= doubleArray(V
.size
.x
, V
.size
.y
, "");
80 for (let i
=0; i
<V
.size
.x
; i
++)
82 for (let j
=0; j
<V
.size
.y
; j
++)
83 sideBoard
[i
][j
] = this.getSquareOccupation(i
, j
, mirrorSide
);
88 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
89 getPotentialMovesFrom([x
,y
], sideBoard
)
91 const pieces
= Object
.keys(V
.ALICE_CODES
);
92 const codes
= Object
.keys(V
.ALICE_PIECES
);
93 const mirrorSide
= (pieces
.includes(this.getPiece(x
,y
)) ? 1 : 2);
95 // Search valid moves on sideBoard
96 let saveBoard
= this.board
;
97 this.board
= sideBoard
|| this.getSideBoard(mirrorSide
);
98 let moves
= super.getPotentialMovesFrom([x
,y
]);
99 this.board
= saveBoard
;
101 // Finally filter impossible moves
102 let res
= moves
.filter(m
=> {
103 if (m
.appear
.length
== 2) //castle
105 // appear[i] must be an empty square on the other board
106 for (let psq
of m
.appear
)
108 if (this.getSquareOccupation(psq
.x
,psq
.y
,3-mirrorSide
) != V
.EMPTY
)
112 else if (this.board
[m
.end
.x
][m
.end
.y
] != V
.EMPTY
)
114 // Attempt to capture
115 const piece
= this.getPiece(m
.end
.x
,m
.end
.y
);
116 if ((mirrorSide
==1 && codes
.includes(piece
))
117 || (mirrorSide
==2 && pieces
.includes(piece
)))
122 // If the move is computed on board1, m.appear change for Alice pieces.
125 m
.appear
.forEach(psq
=> { //forEach: castling taken into account
126 psq
.p
= V
.ALICE_CODES
[psq
.p
]; //goto board2
129 else //move on board2: mark vanishing pieces as Alice
131 m
.vanish
.forEach(psq
=> {
132 psq
.p
= V
.ALICE_CODES
[psq
.p
];
135 // Fix en-passant captures
136 if (m
.vanish
[0].p
== V
.PAWN
&& m
.vanish
.length
== 2
137 && this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
)
139 m
.vanish
[1].c
= this.getOppCol(this.getColor(x
,y
));
140 // In the special case of en-passant, if
141 // - board1 takes board2 : vanish[1] --> Alice
142 // - board2 takes board1 : vanish[1] --> normal
143 let van
= m
.vanish
[1];
144 if (mirrorSide
==1 && codes
.includes(this.getPiece(van
.x
,van
.y
)))
145 van
.p
= V
.ALICE_CODES
[van
.p
];
146 else if (mirrorSide
==2 && pieces
.includes(this.getPiece(van
.x
,van
.y
)))
147 van
.p
= V
.ALICE_PIECES
[van
.p
];
156 if (moves
.length
== 0)
158 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
159 return moves
.filter(m
=> { return !this.underCheck(m
, sideBoard
); });
164 const color
= this.turn
;
165 const oppCol
= this.getOppCol(color
);
166 var potentialMoves
= [];
167 let sideBoard
= [this.getSideBoard(1), this.getSideBoard(2)];
168 for (var i
=0; i
<V
.size
.x
; i
++)
170 for (var j
=0; j
<V
.size
.y
; j
++)
172 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
175 Object
.keys(V
.ALICE_CODES
).includes(this.getPiece(i
,j
))
178 Array
.prototype.push
.apply(potentialMoves
,
179 this.getPotentialMovesFrom([i
,j
], sideBoard
[mirrorSide
-1]));
183 return this.filterValid(potentialMoves
, sideBoard
);
186 // Play on sideboards [TODO: only one sideBoard required]
187 playSide(move, sideBoard
)
189 const pieces
= Object
.keys(V
.ALICE_CODES
);
190 move.vanish
.forEach(psq
=> {
191 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
192 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
194 move.appear
.forEach(psq
=> {
195 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
196 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
197 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
199 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
203 // Undo on sideboards
204 undoSide(move, sideBoard
)
206 const pieces
= Object
.keys(V
.ALICE_CODES
);
207 move.appear
.forEach(psq
=> {
208 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
209 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = V
.EMPTY
;
211 move.vanish
.forEach(psq
=> {
212 const mirrorSide
= (pieces
.includes(psq
.p
) ? 1 : 2);
213 const piece
= (mirrorSide
== 1 ? psq
.p : V
.ALICE_PIECES
[psq
.p
]);
214 sideBoard
[mirrorSide
-1][psq
.x
][psq
.y
] = psq
.c
+ piece
;
216 this.kingPos
[psq
.c
] = [psq
.x
,psq
.y
];
220 underCheck(move, sideBoard
) //sideBoard arg always provided
222 const color
= this.turn
;
223 this.playSide(move, sideBoard
); //no need to track flags
224 const kp
= this.kingPos
[color
];
225 const mirrorSide
= (sideBoard
[0][kp
[0]][kp
[1]] != V
.EMPTY
? 1 : 2);
226 let saveBoard
= this.board
;
227 this.board
= sideBoard
[mirrorSide
-1];
228 let res
= this.isAttacked(kp
, [this.getOppCol(color
)]);
229 this.board
= saveBoard
;
230 this.undoSide(move, sideBoard
);
234 getCheckSquares(move)
237 const color
= this.turn
; //opponent
238 const pieces
= Object
.keys(V
.ALICE_CODES
);
239 const kp
= this.kingPos
[color
];
240 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
241 let sideBoard
= this.getSideBoard(mirrorSide
);
242 let saveBoard
= this.board
;
243 this.board
= sideBoard
;
244 let res
= this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
)])
245 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
247 this.board
= saveBoard
;
252 updateVariables(move)
254 super.updateVariables(move); //standard king
255 const piece
= this.getPiece(move.start
.x
,move.start
.y
);
256 const c
= this.getColor(move.start
.x
,move.start
.y
);
260 this.kingPos
[c
][0] = move.appear
[0].x
;
261 this.kingPos
[c
][1] = move.appear
[0].y
;
262 this.castleFlags
[c
] = [false,false];
266 unupdateVariables(move)
268 super.unupdateVariables(move);
269 const c
= this.getColor(move.start
.x
,move.start
.y
);
270 if (this.getPiece(move.start
.x
,move.start
.y
) == "l")
271 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
276 const pieces
= Object
.keys(V
.ALICE_CODES
);
277 const color
= this.turn
;
278 const kp
= this.kingPos
[color
];
279 const mirrorSide
= (pieces
.includes(this.getPiece(kp
[0],kp
[1])) ? 1 : 2);
280 let sideBoard
= this.getSideBoard(mirrorSide
);
281 let saveBoard
= this.board
;
282 this.board
= sideBoard
;
284 if (!this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
)]))
287 res
= (color
== "w" ? "0-1" : "1-0");
288 this.board
= saveBoard
;
292 static get VALUES() {
293 return Object
.assign(
308 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
310 if (move.end
.y
< move.start
.y
)
316 const finalSquare
= String
.fromCharCode(97 + move.end
.y
) + (V
.size
.x
-move.end
.x
);
317 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
319 const captureMark
= (move.vanish
.length
> move.appear
.length
? "x" : "");
321 if (["p","s"].includes(piece
) && captureMark
.length
== 1)
322 pawnMark
= String
.fromCharCode(97 + move.start
.y
); //start column
324 // Piece or pawn movement
325 let notation
= piece
.toUpperCase() + pawnMark
+ captureMark
+ finalSquare
;
326 if (['s','p'].includes(piece
) && !['s','p'].includes(move.appear
[0].p
))
329 notation
+= "=" + move.appear
[0].p
.toUpperCase();