Commit | Line | Data |
---|---|---|
270968d6 | 1 | class AliceRules extends ChessRules |
5bfb0956 | 2 | { |
0cd8f2bd | 3 | static get ALICE_PIECES() |
5bfb0956 | 4 | { |
270968d6 BA |
5 | return { |
6 | 's': 'p', | |
7 | 't': 'q', | |
8 | 'u': 'r', | |
9 | 'c': 'b', | |
10 | 'o': 'n', | |
11 | 'l': 'k', | |
12 | }; | |
13 | } | |
14 | static get ALICE_CODES() | |
15 | { | |
16 | return { | |
17 | 'p': 's', | |
18 | 'q': 't', | |
19 | 'r': 'u', | |
20 | 'b': 'c', | |
21 | 'n': 'o', | |
22 | 'k': 'l', | |
23 | }; | |
5bfb0956 | 24 | } |
a3eb4cc5 | 25 | |
0cd8f2bd BA |
26 | static getPpath(b) |
27 | { | |
270968d6 | 28 | return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b; |
0cd8f2bd BA |
29 | } |
30 | ||
0b5fa571 BA |
31 | initVariables(fen) |
32 | { | |
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) | |
37 | { | |
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++) | |
40 | { | |
41 | let k = 0; //column index on board | |
42 | for (let j=0; j<position[i].length; j++) | |
43 | { | |
44 | switch (position[i].charAt(j)) | |
45 | { | |
46 | case 'l': | |
47 | this.kingPos['b'] = [i,k]; | |
48 | break; | |
49 | case 'L': | |
50 | this.kingPos['w'] = [i,k]; | |
51 | break; | |
52 | default: | |
53 | let num = parseInt(position[i].charAt(j)); | |
54 | if (!isNaN(num)) | |
55 | k += (num-1); | |
56 | } | |
57 | k++; | |
58 | } | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
270968d6 | 63 | getBoardOfPiece([x,y]) |
0cd8f2bd | 64 | { |
270968d6 BA |
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, ""); | |
0cd8f2bd BA |
71 | for (let i=0; i<sizeX; i++) |
72 | { | |
73 | for (let j=0; j<sizeY; j++) | |
74 | { | |
75 | const piece = this.getPiece(i,j); | |
270968d6 BA |
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]; | |
0cd8f2bd BA |
80 | } |
81 | } | |
270968d6 BA |
82 | return sideBoard; |
83 | } | |
0cd8f2bd | 84 | |
0b5fa571 | 85 | // TODO: move board building one level up (findAllMoves()) to avoid re-building at every piece... |
55eb331d BA |
86 | // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html |
87 | // --> Should be OK as is. | |
270968d6 BA |
88 | getPotentialMovesFrom([x,y]) |
89 | { | |
90 | let sideBoard = this.getBoardOfPiece([x,y]); | |
0cd8f2bd | 91 | |
270968d6 BA |
92 | // Search valid moves on sideBoard |
93 | let saveBoard = this.board; | |
94 | this.board = sideBoard; | |
95 | let moves = super.getPotentialMovesFrom([x,y]); | |
0cd8f2bd BA |
96 | this.board = saveBoard; |
97 | ||
98 | // Finally filter impossible moves | |
270968d6 BA |
99 | const mirrorSide = (Object.keys(VariantRules.ALICE_CODES).includes(this.getPiece(x,y)) ? 1 : 2); |
100 | return moves.filter(m => { | |
55eb331d | 101 | if (m.appear.length == 2) //castle |
270968d6 | 102 | { |
55eb331d BA |
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)) | |
107 | { | |
108 | return false; | |
109 | } | |
110 | }); | |
111 | } | |
112 | else if (this.board[m.end.x][m.end.y] != VariantRules.EMPTY) | |
113 | { | |
114 | // Attempt to capture | |
270968d6 BA |
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))) | |
118 | { | |
119 | return false; | |
120 | } | |
121 | } | |
f6cc7faf BA |
122 | // If the move is computed on board1, m.appear change for Alice pieces. |
123 | if (mirrorSide==1) | |
124 | { | |
125 | m.appear.forEach(psq => { //forEach: castling taken into account | |
270968d6 | 126 | psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2 |
f6cc7faf BA |
127 | }); |
128 | } | |
0b5fa571 BA |
129 | else //move on board2: mark vanishing pieces as Alice |
130 | { | |
131 | m.vanish.forEach(psq => { | |
132 | psq.p = VariantRules.ALICE_CODES[psq.p]; | |
133 | }); | |
134 | } | |
9de73b71 BA |
135 | // Fix en-passant captures |
136 | if (m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY) | |
137 | m.vanish[1].c = this.getOppCol(this.getColor(x,y)); | |
270968d6 BA |
138 | return true; |
139 | }); | |
0cd8f2bd BA |
140 | } |
141 | ||
142 | underCheck(move) | |
143 | { | |
0cd8f2bd BA |
144 | const color = this.turn; |
145 | this.play(move); | |
270968d6 BA |
146 | let sideBoard = this.getBoardOfPiece(this.kingPos[color]); |
147 | let saveBoard = this.board; | |
148 | this.board = sideBoard; | |
0cd8f2bd | 149 | let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)); |
270968d6 | 150 | this.board = saveBoard; |
0cd8f2bd BA |
151 | this.undo(move); |
152 | return res; | |
153 | } | |
154 | ||
270968d6 | 155 | getCheckSquares(move) |
0cd8f2bd | 156 | { |
270968d6 BA |
157 | this.play(move); |
158 | const color = this.turn; //opponent | |
159 | let sideBoard = this.getBoardOfPiece(this.kingPos[color]); | |
160 | let saveBoard = this.board; | |
161 | this.board = sideBoard; | |
162 | let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)) | |
163 | ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] | |
164 | : [ ]; | |
165 | this.board = saveBoard; | |
166 | this.undo(move); | |
167 | return res; | |
0cd8f2bd | 168 | } |
270968d6 | 169 | |
0b5fa571 BA |
170 | updateVariables(move) |
171 | { | |
172 | super.updateVariables(move); //standard king | |
173 | const piece = this.getPiece(move.start.x,move.start.y); | |
174 | const c = this.getColor(move.start.x,move.start.y); | |
175 | // "l" = Alice king | |
176 | if (piece == "l") | |
177 | { | |
178 | this.kingPos[c][0] = move.appear[0].x; | |
179 | this.kingPos[c][1] = move.appear[0].y; | |
180 | this.castleFlags[c] = [false,false]; | |
181 | } | |
182 | } | |
183 | ||
184 | unupdateVariables(move) | |
185 | { | |
186 | super.unupdateVariables(move); | |
187 | const c = this.getColor(move.start.x,move.start.y); | |
188 | if (this.getPiece(move.start.x,move.start.y) == "l") | |
189 | this.kingPos[c] = [move.start.x, move.start.y]; | |
190 | } | |
191 | ||
270968d6 | 192 | getNotation(move) |
0cd8f2bd | 193 | { |
270968d6 BA |
194 | if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING) |
195 | { | |
196 | if (move.end.y < move.start.y) | |
197 | return "0-0-0"; | |
198 | else | |
199 | return "0-0"; | |
200 | } | |
201 | ||
202 | const finalSquare = | |
203 | String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); | |
204 | const piece = this.getPiece(move.start.x, move.start.y); | |
205 | ||
206 | // Piece or pawn movement | |
207 | let notation = piece.toUpperCase() + | |
208 | (move.vanish.length > move.appear.length ? "x" : "") + finalSquare; | |
209 | if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p)) | |
210 | { | |
211 | // Promotion | |
212 | notation += "=" + move.appear[0].p.toUpperCase(); | |
213 | } | |
214 | return notation; | |
0cd8f2bd BA |
215 | } |
216 | ||
217 | checkGameEnd() | |
218 | { | |
219 | const color = this.turn; | |
270968d6 BA |
220 | let sideBoard = this.getBoardOfPiece(this.kingPos[color]); |
221 | let saveBoard = this.board; | |
222 | this.board = sideBoard; | |
223 | let res = "*"; | |
0cd8f2bd | 224 | if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))) |
270968d6 BA |
225 | res = "1/2"; |
226 | else | |
227 | res = (color == "w" ? "0-1" : "1-0"); | |
228 | this.board = saveBoard; | |
229 | return res; | |
0cd8f2bd | 230 | } |
9de73b71 BA |
231 | |
232 | static get VALUES() { | |
233 | return { | |
234 | 'p': 1, | |
235 | 's': 1, | |
236 | 'r': 5, | |
237 | 'u': 5, | |
238 | 'n': 3, | |
239 | 'o': 3, | |
240 | 'b': 3, | |
241 | 'c': 3, | |
242 | 'q': 9, | |
243 | 't': 9, | |
244 | 'k': 1000, | |
245 | 'l': 1000 | |
246 | }; | |
247 | } | |
5bfb0956 | 248 | } |