d457f5f5b2afe9933b1efff5e4e539937b7c294e
1 class AliceRules
extends ChessRules
3 static get ALICE_PIECES()
14 static get ALICE_CODES()
28 return (Object
.keys(this.ALICE_PIECES
).includes(b
[1]) ? "Alice/" : "") + b
;
33 super.initVariables(fen
);
34 const fenParts
= fen
.split(" ");
35 const position
= fenParts
[0].split("/");
36 if (this.kingPos
["w"][0] < 0 || this.kingPos
["b"][0] < 0)
38 // INIT_COL_XXX won't be used, so no need to set them for Alice kings
39 for (let i
=0; i
<position
.length
; i
++)
41 let k
= 0; //column index on board
42 for (let j
=0; j
<position
[i
].length
; j
++)
44 switch (position
[i
].charAt(j
))
47 this.kingPos
['b'] = [i
,k
];
50 this.kingPos
['w'] = [i
,k
];
53 let num
= parseInt(position
[i
].charAt(j
));
63 getBoardOfPiece([x
,y
])
65 const V
= VariantRules
;
66 // Build board where the piece is
67 const mirrorSide
= (Object
.keys(V
.ALICE_CODES
).includes(this.getPiece(x
,y
)) ? 1 : 2);
68 // Build corresponding board from complete board
69 const [sizeX
,sizeY
] = V
.size
;
70 let sideBoard
= doubleArray(sizeX
, sizeY
, "");
71 for (let i
=0; i
<sizeX
; i
++)
73 for (let j
=0; j
<sizeY
; j
++)
75 const piece
= this.getPiece(i
,j
);
76 if (mirrorSide
==1 && Object
.keys(V
.ALICE_CODES
).includes(piece
))
77 sideBoard
[i
][j
] = this.board
[i
][j
];
78 else if (mirrorSide
==2 && Object
.keys(V
.ALICE_PIECES
).includes(piece
))
79 sideBoard
[i
][j
] = this.getColor(i
,j
) + V
.ALICE_PIECES
[piece
];
85 // TODO: move board building one level up (findAllMoves()) to avoid re-building at every piece...
86 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
87 // --> Should be OK as is.
88 getPotentialMovesFrom([x
,y
])
90 let sideBoard
= this.getBoardOfPiece([x
,y
]);
92 // Search valid moves on sideBoard
93 let saveBoard
= this.board
;
94 this.board
= sideBoard
;
95 let moves
= super.getPotentialMovesFrom([x
,y
]);
96 this.board
= saveBoard
;
98 // Finally filter impossible moves
99 const mirrorSide
= (Object
.keys(VariantRules
.ALICE_CODES
).includes(this.getPiece(x
,y
)) ? 1 : 2);
100 return moves
.filter(m
=> {
101 if (m
.appear
.length
== 2) //castle
103 // If appear[i] not in vanish array, then must be empty square on other board
104 m
.appear
.forEach(psq
=> {
105 if (this.board
[psq
.x
][psq
.y
] != VariantRules
.EMPTY
&&
106 ![m
.vanish
[0].y
,m
.vanish
[1].y
].includes(psq
.y
))
112 else if (this.board
[m
.end
.x
][m
.end
.y
] != VariantRules
.EMPTY
)
114 // Attempt to capture
115 const piece
= this.getPiece(m
.end
.x
,m
.end
.y
);
116 if ((mirrorSide
==1 && Object
.keys(VariantRules
.ALICE_PIECES
).includes(piece
))
117 || (mirrorSide
==2 && Object
.keys(VariantRules
.ALICE_CODES
).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
= VariantRules
.ALICE_CODES
[psq
.p
]; //goto board2
129 else //move on board2: mark vanishing pieces as Alice
131 m
.vanish
.forEach(psq
=> {
132 psq
.p
= VariantRules
.ALICE_CODES
[psq
.p
];
141 const color
= this.turn
;
143 let sideBoard
= this.getBoardOfPiece(this.kingPos
[color
]);
144 let saveBoard
= this.board
;
145 this.board
= sideBoard
;
146 let res
= this.isAttacked(this.kingPos
[color
], this.getOppCol(color
));
147 this.board
= saveBoard
;
152 getCheckSquares(move)
155 const color
= this.turn
; //opponent
156 let sideBoard
= this.getBoardOfPiece(this.kingPos
[color
]);
157 let saveBoard
= this.board
;
158 this.board
= sideBoard
;
159 let res
= this.isAttacked(this.kingPos
[color
], this.getOppCol(color
))
160 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
162 this.board
= saveBoard
;
167 updateVariables(move)
169 super.updateVariables(move); //standard king
170 const piece
= this.getPiece(move.start
.x
,move.start
.y
);
171 const c
= this.getColor(move.start
.x
,move.start
.y
);
175 this.kingPos
[c
][0] = move.appear
[0].x
;
176 this.kingPos
[c
][1] = move.appear
[0].y
;
177 this.castleFlags
[c
] = [false,false];
181 unupdateVariables(move)
183 super.unupdateVariables(move);
184 const c
= this.getColor(move.start
.x
,move.start
.y
);
185 if (this.getPiece(move.start
.x
,move.start
.y
) == "l")
186 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
191 if (move.appear
.length
== 2 && move.appear
[0].p
== VariantRules
.KING
)
193 if (move.end
.y
< move.start
.y
)
200 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
201 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
203 // Piece or pawn movement
204 let notation
= piece
.toUpperCase() +
205 (move.vanish
.length
> move.appear
.length
? "x" : "") + finalSquare
;
206 if (['s','p'].includes(piece
) && !['s','p'].includes(move.appear
[0].p
))
209 notation
+= "=" + move.appear
[0].p
.toUpperCase();
216 const color
= this.turn
;
217 let sideBoard
= this.getBoardOfPiece(this.kingPos
[color
]);
218 let saveBoard
= this.board
;
219 this.board
= sideBoard
;
221 if (!this.isAttacked(this.kingPos
[color
], this.getOppCol(color
)))
224 res
= (color
== "w" ? "0-1" : "1-0");
225 this.board
= saveBoard
;