Commit | Line | Data |
---|---|---|
b866a62a BA |
1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
2 | ||
0d5335de | 3 | export class DynamoRules extends ChessRules { |
61656127 BA |
4 | // TODO: later, allow to push out pawns on a and h files? |
5 | static get HasEnpassant() { | |
6 | return false; | |
7 | } | |
8 | ||
c7550017 BA |
9 | canIplay(side, [x, y]) { |
10 | // Sometimes opponent's pieces can be moved directly | |
11 | return true; | |
12 | } | |
13 | ||
61656127 BA |
14 | setOtherVariables(fen) { |
15 | super.setOtherVariables(fen); | |
16 | this.subTurn = 1; | |
17 | // Local stack of "action moves" | |
18 | this.amoves = []; | |
19 | const amove = V.ParseFen(fen).amove; | |
20 | if (cmove == "-") this.amoves.push(null); | |
c7550017 | 21 | else { |
61656127 BA |
22 | const amoveParts = amove.split("/"); |
23 | let amove = { | |
24 | // No need for start & end | |
25 | appear: [], | |
26 | vanish: [] | |
27 | }; | |
28 | [0, 1].map(i => { | |
29 | amoveParts[0].split(".").forEach(av => { | |
30 | // Format is "bpe3" | |
31 | const xy = V.SquareToCoords(av.substr(2)); | |
32 | move[i == 0 ? "appear" : "vanish"].push( | |
33 | new PiPo({ | |
34 | x: xy.x, | |
35 | y: xy.y, | |
36 | c: av[0], | |
37 | p: av[1] | |
38 | }) | |
39 | ); | |
40 | }); | |
41 | }); | |
42 | this.amoves.push(move); | |
c7550017 | 43 | } |
c7550017 BA |
44 | } |
45 | ||
61656127 BA |
46 | static ParseFen(fen) { |
47 | return Object.assign( | |
48 | ChessRules.ParseFen(fen), | |
49 | { cmove: fen.split(" ")[4] } | |
50 | ); | |
51 | } | |
52 | ||
53 | static IsGoodFen(fen) { | |
54 | if (!ChessRules.IsGoodFen(fen)) return false; | |
55 | const fenParts = fen.split(" "); | |
56 | if (fenParts.length != 6) return false; | |
57 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) | |
58 | return false; | |
59 | return true; | |
60 | } | |
61 | ||
62 | getAmove(move) { | |
63 | if (move.appear.length == 2 && move.vanish.length == 2) | |
64 | return { appear: move.appear, vanish: move.vanish }; | |
65 | return null; | |
66 | } | |
67 | ||
68 | // TODO: this.firstMove + rooks location in setOtherVariables | |
69 | // only rooks location in FEN (firstMove is forgotten if quit game and come back) | |
70 | doClick(square) { | |
71 | // If subTurn == 2 && square is the final square of last move, | |
72 | // then return an empty move | |
73 | if ( | |
74 | this.subTurn == 2 && | |
75 | square.x == this.firstMove.end.x && | |
f9385686 | 76 | square.y == this.firstMove.end.y |
61656127 BA |
77 | ) { |
78 | return { | |
79 | appear: [], | |
80 | vanish: [] | |
81 | }; | |
82 | } | |
83 | return null; | |
84 | } | |
85 | ||
86 | canTake() { | |
87 | // Captures don't occur (only pulls & pushes) | |
88 | return false; | |
89 | } | |
90 | ||
91 | // "pa" : piece (as a square) doing this push/pull action | |
92 | getActionMoves([sx, sy], [ex, ey], pa) { | |
93 | const color = this.getColor(sx, sy); | |
94 | const lastRank = (color == 'w' ? 0 : 7); | |
95 | const piece = this.getPiece(sx, sy); | |
96 | let moves = []; | |
97 | if (ex == lastRank && piece == V.PAWN) { | |
98 | // Promotion by push or pull | |
99 | V.PawnSpecs.promotions.forEach(p => { | |
100 | let move = super.getBasicMove([sx, sy], [ex, ey], { c: color, p: p }); | |
101 | moves.push(move); | |
102 | }); | |
103 | } else moves.push(super.getBasicMove([sx, sy], [ex, ey])); | |
104 | const actionType = | |
105 | ( | |
106 | Math.abs(pa[0] - sx) < Math.abs(pa[0] - ex) || | |
107 | Math.abs(pa[1] - sy) < Math.abs(pa[1] - ey) | |
108 | ) | |
109 | ? "push" | |
110 | : "pull"; | |
111 | moves.forEach(m => m.action = [{ by: pa, type: actionType }]); | |
112 | return moves; | |
113 | } | |
114 | ||
115 | // TODO: if type is given, consider only actions of this type | |
116 | getPactions(sq, color, type) { | |
b866a62a BA |
117 | const [x, y] = sq; |
118 | let moves = []; | |
119 | let squares = {}; | |
b866a62a | 120 | if (!by) { |
61656127 | 121 | const oppCol = V.GetOppCol(color); |
b866a62a BA |
122 | // Look in all directions for a "color" piece |
123 | for (let step of V.steps[V.KNIGHT]) { | |
124 | const xx = x + step[0], | |
125 | yy = y + step[1]; | |
126 | if ( | |
127 | V.OnBoard(xx, yy) && | |
128 | this.getPiece(xx, yy) == V.KNIGHT && | |
129 | this.getColor(xx, yy) == color | |
130 | ) { | |
131 | const px = x - step[0], | |
132 | py = y - step[1]; | |
61656127 BA |
133 | if (V.OnBoard(px, py)) { |
134 | if (this.board[px][py] == V.EMPTY) { | |
135 | const hash = "s" + px + py; | |
136 | if (!squares[hash]) { | |
137 | squares[hash] = true; | |
138 | Array.prototype.push.apply( | |
139 | moves, | |
140 | this.getActionMoves([x, y], [px, py], [xx, yy]) | |
141 | ); | |
142 | } | |
143 | else { //add piece doing action | |
144 | } | |
b866a62a BA |
145 | } |
146 | } else { | |
147 | const hash = "s" + xx + yy; | |
148 | if (!squares[hash]) { | |
149 | squares[hash] = true; | |
150 | moves.push( | |
151 | new Move({ | |
152 | start: { x: x, y: y }, | |
153 | end: { x: xx, y: yy }, | |
154 | appear: [], | |
155 | vanish: [ | |
156 | new PiPo({ | |
157 | x: x, | |
158 | y: y, | |
159 | p: this.getPiece(x, y), | |
160 | c: oppCol | |
161 | }) | |
162 | ] | |
163 | }) | |
164 | ); | |
165 | } | |
166 | } | |
167 | } | |
168 | } | |
169 | for (let step in V.steps[V.ROOK]) { | |
61656127 BA |
170 | // (+ if color is ours, pawn pushes) king, rook and queen |
171 | // --> pawns special case can push from a little distance if on 2nd rank (or 1st rank) | |
b866a62a BA |
172 | } |
173 | for (let step in V.steps[V.BISHOP]) { | |
61656127 | 174 | // King, bishop, queen, and possibly pawns attacks (if color is enemy) |
b866a62a BA |
175 | } |
176 | } | |
b866a62a BA |
177 | return moves; |
178 | } | |
179 | ||
180 | // NOTE: to push a piece out of the board, make it slide until our piece | |
181 | // (doing the action, moving or not) | |
61656127 BA |
182 | // TODO: for pushes, play the pushed piece first. |
183 | // for pulls: play the piece doing the action first | |
b866a62a BA |
184 | getPotentialMovesFrom([x, y]) { |
185 | const color = this.turn; | |
61656127 BA |
186 | if (this.getColor(x, y) != color) |
187 | // The only moves possible with enemy pieces are pulls and pushes: | |
188 | return this.getPactions([x, y], color); | |
f9385686 BA |
189 | // Playing my pieces: either on their own, or pushed by another |
190 | // If subTurn == 2 then we should have a first move, | |
191 | // TODO = use it to allow some type of action | |
192 | if (this.subTurn == 2) { | |
193 | return ( | |
194 | this.moveOnSubturn1.isAnAction | |
195 | ? super.getPotentialMovesFrom([x, y]) | |
196 | : this.getPactions([x, y], color, TODO_arg) | |
197 | ); | |
b866a62a | 198 | } |
f9385686 BA |
199 | // Both options are possible at subTurn1: normal move, or push |
200 | return ( | |
201 | super.getPotentialMovesFrom([x, y]) | |
202 | .concat(this.getPactions([x, y], color, "push")) | |
203 | // TODO: discard moves that let the king underCheck, and no second | |
204 | // move can counter check. Example: pinned queen pushes pinned pawn. | |
205 | .filter(m => { | |
206 | this.play(m); | |
207 | const res = this.filterMoves(this.getPotentialMoves(/* TODO: args? */)).length > 0; | |
208 | this.undo(m); | |
209 | return res; | |
210 | }) | |
211 | ); | |
61656127 BA |
212 | } |
213 | ||
214 | // TODO: track rooks locations, should be a field in FEN, in castleflags? | |
215 | // --> only useful if castleFlags is still ON | |
216 | getCastleMoves(sq) { | |
217 | // TODO: if rook1 isn't at its place (with castleFlags ON), set it off | |
218 | // same for rook2. | |
219 | let moves = super.getCastleMoves(sq); | |
220 | // TODO: restore castleFlags | |
221 | } | |
222 | ||
223 | // Does m2 un-do m1 ? (to disallow undoing actions) | |
224 | oppositeMoves(m1, m2) { | |
225 | const isEqual = (av1, av2) => { | |
226 | // Precondition: av1 and av2 length = 2 | |
227 | for (let av of av1) { | |
228 | const avInAv2 = av2.find(elt => { | |
229 | return ( | |
230 | elt.x == av.x && | |
231 | elt.y == av.y && | |
232 | elt.c == av.c && | |
233 | elt.p == av.p | |
234 | ); | |
235 | }); | |
236 | if (!avInAv2) return false; | |
237 | } | |
238 | return true; | |
239 | }; | |
240 | return ( | |
241 | !!m1 && | |
242 | m1.appear.length == 2 && | |
243 | m2.appear.length == 2 && | |
244 | m1.vanish.length == 2 && | |
245 | m2.vanish.length == 2 && | |
246 | isEqual(m1.appear, m2.vanish) && | |
247 | isEqual(m1.vanish, m2.appear) | |
248 | ); | |
249 | } | |
250 | ||
251 | filterValid(moves) { | |
252 | if (moves.length == 0) return []; | |
253 | const color = this.turn; | |
254 | return moves.filter(m => { | |
255 | const L = this.amoves.length; //at least 1: init from FEN | |
256 | return !this.oppositeMoves(this.amoves[L - 1], m); | |
257 | }); | |
b866a62a BA |
258 | } |
259 | ||
c7550017 BA |
260 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { |
261 | for (let step of steps) { | |
262 | let rx = x + step[0], | |
263 | ry = y + step[1]; | |
264 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { | |
265 | rx += step[0]; | |
266 | ry += step[1]; | |
267 | } | |
268 | if ( | |
269 | V.OnBoard(rx, ry) && | |
270 | this.getPiece(rx, ry) == piece && | |
271 | this.getColor(rx, ry) == color | |
272 | ) { | |
273 | // Now step in the other direction: if end of the world, then attacked | |
274 | rx = x - step[0]; | |
275 | ry = y - step[1]; | |
2c5d7b20 BA |
276 | while ( |
277 | V.OnBoard(rx, ry) && | |
278 | this.board[rx][ry] == V.EMPTY && | |
279 | !oneStep | |
280 | ) { | |
c7550017 BA |
281 | rx -= step[0]; |
282 | ry -= step[1]; | |
283 | } | |
284 | if (!V.OnBoard(rx, ry)) return true; | |
285 | } | |
286 | } | |
287 | return false; | |
288 | } | |
289 | ||
290 | isAttackedByPawn([x, y], color) { | |
291 | const lastRank = (color == 'w' ? 0 : 7); | |
292 | if (y != lastRank) | |
293 | // The king can be pushed out by a pawn only on last rank | |
294 | return false; | |
295 | const pawnShift = (color == "w" ? 1 : -1); | |
296 | for (let i of [-1, 1]) { | |
297 | if ( | |
298 | y + i >= 0 && | |
299 | y + i < V.size.y && | |
300 | this.getPiece(x + pawnShift, y + i) == V.PAWN && | |
301 | this.getColor(x + pawnShift, y + i) == color | |
302 | ) { | |
303 | return true; | |
304 | } | |
305 | } | |
306 | return false; | |
307 | } | |
61656127 BA |
308 | |
309 | getCurrentScore() { | |
310 | if (this.subTurn == 2) | |
311 | // Move not over | |
312 | return "*"; | |
313 | return super.getCurrentScore(); | |
314 | } | |
315 | ||
316 | play(move) { | |
317 | move.flags = JSON.stringify(this.aggregateFlags()); | |
318 | V.PlayOnBoard(this.board, move); | |
319 | if (this.subTurn == 1) { | |
320 | // TODO: is there a second move possible? | |
321 | // (if the first move is a normal one, there may be no actions available) | |
322 | // --> If not, just change turn as ion the else {} section | |
323 | this.subTurn = 2; | |
324 | this.movesCount++; | |
325 | } else { | |
326 | // subTurn == 2 | |
327 | this.turn = V.GetOppCol(this.turn); | |
328 | this.subTurn = 1; | |
329 | } | |
330 | this.postPlay(move); | |
331 | } | |
332 | ||
333 | updateCastleFlags(move, piece) { | |
334 | const c = V.GetOppCol(this.turn); | |
335 | const firstRank = (c == "w" ? V.size.x - 1 : 0); | |
336 | // Update castling flags if rooks are moved (only) | |
337 | if (piece == V.KING && move.appear.length > 0) | |
338 | this.castleFlags[c] = [V.size.y, V.size.y]; | |
339 | else if ( | |
340 | move.start.x == firstRank && | |
341 | this.castleFlags[c].includes(move.start.y) | |
342 | ) { | |
343 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
344 | this.castleFlags[c][flagIdx] = V.size.y; | |
345 | } | |
346 | } | |
347 | ||
348 | undo(move) { | |
349 | this.disaggregateFlags(JSON.parse(move.flags)); | |
350 | V.UndoOnBoard(this.board, move); | |
351 | if (this.subTurn == 2) { | |
352 | this.subTurn = 1; | |
353 | this.movesCount--; | |
5e1bc651 | 354 | } else { |
61656127 BA |
355 | // subTurn == 1 (after a move played) |
356 | this.turn = V.GetOppCol(this.turn); | |
357 | this.subTurn = 2; | |
358 | } | |
359 | this.postUndo(move); | |
360 | } | |
0d5335de | 361 | }; |