Commit | Line | Data |
---|---|---|
b967d5ba | 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
5d75c82c | 2 | import { SuicideRules } from "@/variants/Suicide"; |
00eef1ca | 3 | import { ArrayFun } from "@/utils/array"; |
15d69043 | 4 | import { randInt } from "@/utils/alea"; |
6c7cbfed BA |
5 | |
6 | export class ChakartRules extends ChessRules { | |
7e8a7ea1 | 7 | |
74afb57d BA |
8 | static get Options() { |
9 | return { | |
10 | select: [ | |
11 | { | |
12 | label: "Randomness", | |
13 | variable: "randomness", | |
14 | defaut: 2, | |
15 | options: [ | |
16 | { label: "Deterministic", value: 0 }, | |
17 | { label: "Symmetric random", value: 1 }, | |
18 | { label: "Asymmetric random", value: 2 } | |
19 | ] | |
20 | } | |
21 | ] | |
22 | }; | |
23 | } | |
24 | ||
5d75c82c BA |
25 | static get PawnSpecs() { |
26 | return SuicideRules.PawnSpecs; | |
27 | } | |
28 | ||
29 | static get HasCastle() { | |
30 | return false; | |
31 | } | |
32 | ||
596e24d0 BA |
33 | static get HasEnpassant() { |
34 | return false; | |
35 | } | |
36 | ||
ad030c7d BA |
37 | static get CorrConfirm() { |
38 | // Because of bonus effects | |
39 | return false; | |
40 | } | |
90df90bc | 41 | |
ad030c7d | 42 | static get CanAnalyze() { |
77fc0c65 | 43 | return false; |
00eef1ca BA |
44 | } |
45 | ||
46 | static get SomeHiddenMoves() { | |
47 | return true; | |
ad030c7d | 48 | } |
6c7cbfed | 49 | |
b9ce3d0f BA |
50 | static get IMMOBILIZE_CODE() { |
51 | return { | |
52 | 'p': 's', | |
53 | 'r': 'u', | |
54 | 'n': 'o', | |
55 | 'b': 'c', | |
56 | 'q': 't', | |
57 | 'k': 'l' | |
58 | }; | |
59 | } | |
60 | ||
61 | static get IMMOBILIZE_DECODE() { | |
62 | return { | |
63 | 's': 'p', | |
64 | 'u': 'r', | |
65 | 'o': 'n', | |
66 | 'c': 'b', | |
67 | 't': 'q', | |
68 | 'l': 'k' | |
69 | }; | |
70 | } | |
71 | ||
72 | static get INVISIBLE_QUEEN() { | |
73 | return 'i'; | |
74 | } | |
75 | ||
ad030c7d BA |
76 | // Fictive color 'a', bomb banana mushroom egg |
77 | static get BOMB() { | |
00eef1ca | 78 | return 'w'; //"Wario" |
ad030c7d BA |
79 | } |
80 | static get BANANA() { | |
00eef1ca | 81 | return 'd'; //"Donkey" |
ad030c7d BA |
82 | } |
83 | static get EGG() { | |
84 | return 'e'; | |
85 | } | |
86 | static get MUSHROOM() { | |
87 | return 'm'; | |
88 | } | |
89 | ||
00eef1ca BA |
90 | static fen2board(f) { |
91 | return ( | |
92 | f.charCodeAt() <= 90 | |
93 | ? "w" + f.toLowerCase() | |
94 | : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f | |
95 | ); | |
96 | } | |
97 | ||
ad030c7d BA |
98 | static get PIECES() { |
99 | return ( | |
100 | ChessRules.PIECES.concat( | |
101 | Object.keys(V.IMMOBILIZE_DECODE)).concat( | |
102 | [V.BANANA, V.BOMB, V.EGG, V.MUSHROOM, V.INVISIBLE_QUEEN]) | |
103 | ); | |
104 | } | |
105 | ||
b9ce3d0f BA |
106 | getPpath(b) { |
107 | let prefix = ""; | |
108 | if ( | |
ad030c7d | 109 | b[0] == 'a' || |
b9ce3d0f BA |
110 | b[1] == V.INVISIBLE_QUEEN || |
111 | Object.keys(V.IMMOBILIZE_DECODE).includes(b[1]) | |
112 | ) { | |
113 | prefix = "Chakart/"; | |
114 | } | |
115 | return prefix + b; | |
116 | } | |
117 | ||
00eef1ca | 118 | getPPpath(m) { |
b36db525 | 119 | if (!!m.promoteInto) return m.promoteInto; |
a1075a51 BA |
120 | if (m.appear.length == 0 && m.vanish.length == 1) |
121 | // King 'remote shell capture', on an adjacent square: | |
122 | return this.getPpath(m.vanish[0].c + m.vanish[0].p); | |
00eef1ca BA |
123 | let piece = m.appear[0].p; |
124 | if (Object.keys(V.IMMOBILIZE_DECODE).includes(piece)) | |
a1075a51 | 125 | // Promotion by capture into immobilized piece: do not reveal! |
00eef1ca BA |
126 | piece = V.IMMOBILIZE_DECODE[piece]; |
127 | return this.getPpath(m.appear[0].c + piece); | |
128 | } | |
129 | ||
b9ce3d0f BA |
130 | static ParseFen(fen) { |
131 | const fenParts = fen.split(" "); | |
132 | return Object.assign( | |
133 | ChessRules.ParseFen(fen), | |
596e24d0 | 134 | { captured: fenParts[4] } |
b9ce3d0f BA |
135 | ); |
136 | } | |
137 | ||
596e24d0 BA |
138 | static IsGoodFen(fen) { |
139 | if (!ChessRules.IsGoodFen(fen)) return false; | |
140 | const captured = V.ParseFen(fen).captured; | |
141 | if (!captured || !captured.match(/^[0-9]{12,12}$/)) return false; | |
142 | return true; | |
143 | } | |
144 | ||
b9ce3d0f | 145 | // King can be l or L (immobilized) --> similar to Alice variant |
90df90bc BA |
146 | static IsGoodPosition(position) { |
147 | if (position.length == 0) return false; | |
148 | const rows = position.split("/"); | |
149 | if (rows.length != V.size.x) return false; | |
b9ce3d0f | 150 | let kings = { "k": 0, "K": 0, 'l': 0, 'L': 0 }; |
90df90bc BA |
151 | for (let row of rows) { |
152 | let sumElts = 0; | |
153 | for (let i = 0; i < row.length; i++) { | |
00eef1ca | 154 | if (['K', 'k', 'L', 'l'].includes(row[i])) kings[row[i]]++; |
90df90bc BA |
155 | if (V.PIECES.includes(row[i].toLowerCase())) sumElts++; |
156 | else { | |
e50a8025 | 157 | const num = parseInt(row[i], 10); |
90df90bc BA |
158 | if (isNaN(num)) return false; |
159 | sumElts += num; | |
160 | } | |
161 | } | |
162 | if (sumElts != V.size.y) return false; | |
163 | } | |
978fa11c | 164 | if (kings['k'] + kings['l'] == 0 || kings['K'] + kings['L'] == 0) |
b9ce3d0f | 165 | return false; |
90df90bc BA |
166 | return true; |
167 | } | |
168 | ||
b9ce3d0f | 169 | static IsGoodFlags(flags) { |
5d75c82c BA |
170 | // 4 for Peach + Mario w, b |
171 | return !!flags.match(/^[01]{4,4}$/); | |
b9ce3d0f BA |
172 | } |
173 | ||
174 | setFlags(fenflags) { | |
b967d5ba | 175 | // King can send shell? Queen can be invisible? |
b9ce3d0f | 176 | this.powerFlags = { |
00eef1ca BA |
177 | w: { 'k': false, 'q': false }, |
178 | b: { 'k': false, 'q': false } | |
b9ce3d0f | 179 | }; |
b9ce3d0f | 180 | for (let c of ["w", "b"]) { |
b967d5ba BA |
181 | for (let p of ['k', 'q']) { |
182 | this.powerFlags[c][p] = | |
15d69043 | 183 | fenflags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1"; |
b967d5ba | 184 | } |
b9ce3d0f BA |
185 | } |
186 | } | |
187 | ||
188 | aggregateFlags() { | |
5d75c82c | 189 | return this.powerFlags; |
b9ce3d0f BA |
190 | } |
191 | ||
192 | disaggregateFlags(flags) { | |
5d75c82c | 193 | this.powerFlags = flags; |
b9ce3d0f BA |
194 | } |
195 | ||
196 | getFen() { | |
197 | return super.getFen() + " " + this.getCapturedFen(); | |
198 | } | |
199 | ||
200 | getFenForRepeat() { | |
201 | return super.getFenForRepeat() + "_" + this.getCapturedFen(); | |
202 | } | |
203 | ||
204 | getCapturedFen() { | |
596e24d0 | 205 | let counts = [...Array(12).fill(0)]; |
b9ce3d0f | 206 | let i = 0; |
596e24d0 | 207 | for (let p of V.RESERVE_PIECES) { |
b9ce3d0f | 208 | counts[i] = this.captured["w"][p]; |
596e24d0 | 209 | counts[6 + i] = this.captured["b"][p]; |
b9ce3d0f BA |
210 | i++; |
211 | } | |
212 | return counts.join(""); | |
213 | } | |
214 | ||
15d69043 BA |
215 | scanKings() {} |
216 | ||
6c7cbfed | 217 | setOtherVariables(fen) { |
15d69043 | 218 | super.setOtherVariables(fen); |
b9ce3d0f | 219 | // Initialize captured pieces' counts from FEN |
e50a8025 BA |
220 | const captured = |
221 | V.ParseFen(fen).captured.split("").map(x => parseInt(x, 10)); | |
b9ce3d0f BA |
222 | this.captured = { |
223 | w: { | |
e50a8025 BA |
224 | [V.PAWN]: captured[0], |
225 | [V.ROOK]: captured[1], | |
226 | [V.KNIGHT]: captured[2], | |
227 | [V.BISHOP]: captured[3], | |
228 | [V.QUEEN]: captured[4], | |
229 | [V.KING]: captured[5] | |
b9ce3d0f BA |
230 | }, |
231 | b: { | |
e50a8025 BA |
232 | [V.PAWN]: captured[6], |
233 | [V.ROOK]: captured[7], | |
234 | [V.KNIGHT]: captured[8], | |
235 | [V.BISHOP]: captured[9], | |
236 | [V.QUEEN]: captured[10], | |
237 | [V.KING]: captured[11] | |
b9ce3d0f BA |
238 | } |
239 | }; | |
54879803 | 240 | this.effects = []; |
6c7cbfed BA |
241 | this.subTurn = 1; |
242 | } | |
243 | ||
b9ce3d0f | 244 | getFlagsFen() { |
5d75c82c | 245 | let fen = ""; |
b9ce3d0f BA |
246 | // Add power flags |
247 | for (let c of ["w", "b"]) | |
b967d5ba | 248 | for (let p of ['k', 'q']) fen += (this.powerFlags[c][p] ? "1" : "0"); |
b9ce3d0f BA |
249 | return fen; |
250 | } | |
251 | ||
596e24d0 BA |
252 | getColor(i, j) { |
253 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; | |
254 | return this.board[i][j].charAt(0); | |
255 | } | |
256 | ||
257 | getPiece(i, j) { | |
258 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; | |
259 | return this.board[i][j].charAt(1); | |
260 | } | |
261 | ||
00eef1ca BA |
262 | getReservePpath(index, color) { |
263 | return color + V.RESERVE_PIECES[index]; | |
264 | } | |
265 | ||
b967d5ba | 266 | static get RESERVE_PIECES() { |
596e24d0 | 267 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING]; |
b967d5ba BA |
268 | } |
269 | ||
270 | getReserveMoves([x, y]) { | |
271 | const color = this.turn; | |
272 | const p = V.RESERVE_PIECES[y]; | |
273 | if (this.reserve[color][p] == 0) return []; | |
274 | let moves = []; | |
275 | const start = (color == 'w' && p == V.PAWN ? 1 : 0); | |
276 | const end = (color == 'b' && p == V.PAWN ? 7 : 8); | |
277 | for (let i = start; i < end; i++) { | |
278 | for (let j = 0; j < V.size.y; j++) { | |
42e37983 BA |
279 | if ( |
280 | this.board[i][j] == V.EMPTY || | |
281 | this.getColor(i, j) == 'a' || | |
282 | this.getPiece(i, j) == V.INVISIBLE_QUEEN | |
283 | ) { | |
77fc0c65 BA |
284 | let m = this.getBasicMove({ p: p, x: i, y: j}); |
285 | m.start = { x: x, y: y }; | |
286 | moves.push(m); | |
b967d5ba BA |
287 | } |
288 | } | |
289 | } | |
290 | return moves; | |
291 | } | |
292 | ||
293 | getPotentialMovesFrom([x, y]) { | |
00eef1ca BA |
294 | let moves = []; |
295 | if (this.subTurn == 1) { | |
296 | moves = super.getPotentialMovesFrom([x, y]); | |
297 | const finalPieces = V.PawnSpecs.promotions; | |
298 | const color = this.turn; | |
299 | const lastRank = (color == "w" ? 0 : 7); | |
300 | let pMoves = []; | |
301 | moves.forEach(m => { | |
302 | if ( | |
303 | m.appear.length > 0 && | |
304 | ['p', 's'].includes(m.appear[0].p) && | |
305 | m.appear[0].x == lastRank | |
306 | ) { | |
307 | for (let i = 1; i < finalPieces.length; i++) { | |
308 | const piece = finalPieces[i]; | |
309 | let otherM = JSON.parse(JSON.stringify(m)); | |
310 | otherM.appear[0].p = | |
311 | m.appear[0].p == V.PAWN | |
312 | ? finalPieces[i] | |
313 | : V.IMMOBILIZE_CODE[finalPieces[i]]; | |
314 | pMoves.push(otherM); | |
315 | } | |
316 | // Finally alter m itself: | |
317 | m.appear[0].p = | |
318 | m.appear[0].p == V.PAWN | |
319 | ? finalPieces[0] | |
320 | : V.IMMOBILIZE_CODE[finalPieces[0]]; | |
321 | } | |
322 | }); | |
323 | Array.prototype.push.apply(moves, pMoves); | |
324 | } | |
325 | else { | |
326 | // Subturn == 2 | |
54879803 BA |
327 | const L = this.effects.length; |
328 | switch (this.effects[L-1]) { | |
00eef1ca | 329 | case "kingboo": |
ccbb9dfc | 330 | // Exchange position with any visible piece, |
00eef1ca BA |
331 | // except pawns if arriving on last rank. |
332 | const lastRank = { 'w': 0, 'b': 7 }; | |
333 | const color = this.turn; | |
334 | const allowLastRank = (this.getPiece(x, y) != V.PAWN); | |
b967d5ba BA |
335 | for (let i=0; i<8; i++) { |
336 | for (let j=0; j<8; j++) { | |
337 | const colIJ = this.getColor(i, j); | |
ccbb9dfc | 338 | const pieceIJ = this.getPiece(i, j); |
b967d5ba | 339 | if ( |
00eef1ca | 340 | (i != x || j != y) && |
b967d5ba | 341 | this.board[i][j] != V.EMPTY && |
ccbb9dfc | 342 | pieceIJ != V.INVISIBLE_QUEEN && |
b967d5ba BA |
343 | colIJ != 'a' |
344 | ) { | |
00eef1ca BA |
345 | if ( |
346 | (pieceIJ != V.PAWN || x != lastRank[colIJ]) && | |
347 | (allowLastRank || i != lastRank[color]) | |
348 | ) { | |
349 | const movedUnit = new PiPo({ | |
350 | x: x, | |
351 | y: y, | |
352 | c: colIJ, | |
353 | p: this.getPiece(i, j) | |
354 | }); | |
77fc0c65 | 355 | let mMove = this.getBasicMove({ x: x, y: y }, [i, j]); |
00eef1ca BA |
356 | mMove.appear.push(movedUnit); |
357 | moves.push(mMove); | |
358 | } | |
b967d5ba BA |
359 | } |
360 | } | |
361 | } | |
362 | break; | |
00eef1ca | 363 | case "toadette": |
b967d5ba BA |
364 | // Resurrect a captured piece |
365 | if (x >= V.size.x) moves = this.getReserveMoves([x, y]); | |
366 | break; | |
00eef1ca | 367 | case "daisy": |
54879803 BA |
368 | // Play again with any piece |
369 | moves = super.getPotentialMovesFrom([x, y]); | |
b967d5ba BA |
370 | break; |
371 | } | |
6c7cbfed | 372 | } |
00eef1ca | 373 | return moves; |
6c7cbfed BA |
374 | } |
375 | ||
ccbb9dfc | 376 | // Helper for getBasicMove(): banana/bomb effect |
15d69043 | 377 | getRandomSquare([x, y], steps) { |
ccbb9dfc | 378 | const validSteps = steps.filter(s => V.OnBoard(x + s[0], y + s[1])); |
15d69043 BA |
379 | const step = validSteps[randInt(validSteps.length)]; |
380 | return [x + step[0], y + step[1]]; | |
381 | } | |
382 | ||
77fc0c65 BA |
383 | // Apply mushroom, bomb or banana effect (hidden to the player). |
384 | // Determine egg effect, too, and apply its first part if possible. | |
385 | getBasicMove_aux(psq1, sq2, tr, initMove) { | |
386 | const [x1, y1] = [psq1.x, psq1.y]; | |
387 | const color1 = this.turn; | |
b36db525 | 388 | const piece1 = (!!tr ? tr.p : (psq1.p || this.getPiece(x1, y1))); |
15d69043 | 389 | const oppCol = V.GetOppCol(color1); |
77fc0c65 BA |
390 | if (!sq2) { |
391 | let move = { | |
392 | appear: [], | |
393 | vanish: [] | |
394 | }; | |
395 | // banana or bomb defines next square, or the move ends there | |
396 | move.appear = [ | |
397 | new PiPo({ | |
398 | x: x1, | |
399 | y: y1, | |
400 | c: color1, | |
401 | p: piece1 | |
402 | }) | |
403 | ]; | |
404 | if (this.board[x1][y1] != V.EMPTY) { | |
405 | const initP1 = this.getPiece(x1, y1); | |
406 | move.vanish = [ | |
407 | new PiPo({ | |
408 | x: x1, | |
409 | y: y1, | |
410 | c: this.getColor(x1, y1), | |
411 | p: initP1 | |
412 | }) | |
413 | ]; | |
414 | if ([V.BANANA, V.BOMB].includes(initP1)) { | |
415 | const steps = V.steps[initP1 == V.BANANA ? V.ROOK : V.BISHOP]; | |
416 | move.next = this.getRandomSquare([x1, y1], steps); | |
417 | } | |
418 | } | |
419 | move.end = { x: x1, y: y1 }; | |
420 | return move; | |
421 | } | |
422 | const [x2, y2] = [sq2[0], sq2[1]]; | |
423 | // The move starts normally, on board: | |
424 | let move = super.getBasicMove([x1, y1], [x2, y2], tr); | |
b36db525 | 425 | if (!!tr) move.promoteInto = tr.c + tr.p; //in case of (chomped...) |
54879803 | 426 | const L = this.effects.length; |
77fc0c65 BA |
427 | if ( |
428 | [V.PAWN, V.KNIGHT].includes(piece1) && | |
429 | !!initMove && | |
54879803 | 430 | (this.subTurn == 1 || this.effects[L-1] == "daisy") |
77fc0c65 | 431 | ) { |
15d69043 BA |
432 | switch (piece1) { |
433 | case V.PAWN: { | |
434 | const twoSquaresMove = (Math.abs(x2 - x1) == 2); | |
435 | const mushroomX = x1 + (twoSquaresMove ? (x2 - x1) / 2 : 0); | |
436 | move.appear.push( | |
437 | new PiPo({ | |
438 | x: mushroomX, | |
439 | y: y1, | |
440 | c: 'a', | |
441 | p: V.MUSHROOM | |
442 | }) | |
443 | ); | |
444 | if (this.getColor(mushroomX, y1) == 'a') { | |
445 | move.vanish.push( | |
446 | new PiPo({ | |
447 | x: mushroomX, | |
448 | y: y1, | |
449 | c: 'a', | |
450 | p: this.getPiece(mushroomX, y1) | |
451 | }) | |
452 | ); | |
453 | } | |
454 | break; | |
455 | } | |
456 | case V.KNIGHT: { | |
457 | const deltaX = Math.abs(x2 - x1); | |
458 | const deltaY = Math.abs(y2 - y1); | |
00eef1ca | 459 | let eggSquare = [ |
15d69043 BA |
460 | x1 + (deltaX == 2 ? (x2 - x1) / 2 : 0), |
461 | y1 + (deltaY == 2 ? (y2 - y1) / 2 : 0) | |
462 | ]; | |
463 | if ( | |
00eef1ca BA |
464 | this.board[eggSquare[0]][eggSquare[1]] != V.EMPTY && |
465 | this.getColor(eggSquare[0], eggSquare[1]) != 'a' | |
15d69043 | 466 | ) { |
00eef1ca BA |
467 | eggSquare[0] = x1; |
468 | eggSquare[1] = y1; | |
469 | } | |
470 | move.appear.push( | |
471 | new PiPo({ | |
472 | x: eggSquare[0], | |
473 | y: eggSquare[1], | |
474 | c: 'a', | |
475 | p: V.EGG | |
476 | }) | |
477 | ); | |
478 | if (this.getColor(eggSquare[0], eggSquare[1]) == 'a') { | |
479 | move.vanish.push( | |
15d69043 BA |
480 | new PiPo({ |
481 | x: eggSquare[0], | |
482 | y: eggSquare[1], | |
483 | c: 'a', | |
00eef1ca | 484 | p: this.getPiece(eggSquare[0], eggSquare[1]) |
15d69043 BA |
485 | }) |
486 | ); | |
15d69043 BA |
487 | } |
488 | break; | |
489 | } | |
490 | } | |
491 | } | |
15d69043 BA |
492 | // For (wa)luigi effect: |
493 | const changePieceColor = (color) => { | |
494 | let pieces = []; | |
00eef1ca | 495 | const oppLastRank = (color == 'w' ? 7 : 0); |
15d69043 BA |
496 | for (let i=0; i<8; i++) { |
497 | for (let j=0; j<8; j++) { | |
ccbb9dfc | 498 | const piece = this.getPiece(i, j); |
77fc0c65 BA |
499 | if ( |
500 | (i != move.vanish[0].x || j != move.vanish[0].y) && | |
501 | this.board[i][j] != V.EMPTY && | |
ccbb9dfc | 502 | piece != V.INVISIBLE_QUEEN && |
77fc0c65 BA |
503 | this.getColor(i, j) == color |
504 | ) { | |
00eef1ca | 505 | if (piece != V.KING && (piece != V.PAWN || i != oppLastRank)) |
15d69043 BA |
506 | pieces.push({ x: i, y: j, p: piece }); |
507 | } | |
508 | } | |
509 | } | |
77fc0c65 | 510 | // Special case of the current piece (still at its initial position) |
596e24d0 BA |
511 | if (color == color1) |
512 | pieces.push({ x: move.appear[0].x, y: move.appear[0].y, p: piece1 }); | |
15d69043 | 513 | const cp = pieces[randInt(pieces.length)]; |
596e24d0 BA |
514 | if (move.appear[0].x != cp.x || move.appear[0].y != cp.y) { |
515 | move.vanish.push( | |
516 | new PiPo({ | |
517 | x: cp.x, | |
518 | y: cp.y, | |
519 | c: color, | |
520 | p: cp.p | |
521 | }) | |
522 | ); | |
523 | } | |
77fc0c65 | 524 | else move.appear.shift(); |
15d69043 BA |
525 | move.appear.push( |
526 | new PiPo({ | |
527 | x: cp.x, | |
528 | y: cp.y, | |
529 | c: V.GetOppCol(color), | |
530 | p: cp.p | |
531 | }) | |
532 | ); | |
533 | }; | |
534 | const applyEggEffect = () => { | |
00eef1ca BA |
535 | if (this.subTurn == 2) |
536 | // No egg effects at subTurn 2 | |
537 | return; | |
538 | // 1) Determine the effect (some may be impossible) | |
54879803 | 539 | let effects = ["kingboo", "koopa", "chomp", "bowser", "daisy"]; |
00eef1ca BA |
540 | if (Object.values(this.captured[color1]).some(c => c >= 1)) |
541 | effects.push("toadette"); | |
542 | const lastRank = { 'w': 0, 'b': 7 }; | |
00eef1ca BA |
543 | if ( |
544 | this.board.some((b,i) => | |
545 | b.some(cell => { | |
546 | return ( | |
547 | cell[0] == oppCol && | |
548 | cell[1] != V.KING && | |
596e24d0 | 549 | (cell[1] != V.PAWN || i != lastRank[color1]) |
00eef1ca BA |
550 | ); |
551 | }) | |
552 | ) | |
553 | ) { | |
554 | effects.push("luigi"); | |
555 | } | |
556 | if ( | |
596e24d0 BA |
557 | ( |
558 | piece1 != V.KING && | |
559 | (piece1 != V.PAWN || move.appear[0].x != lastRank[oppCol]) | |
560 | ) || | |
00eef1ca BA |
561 | this.board.some((b,i) => |
562 | b.some(cell => { | |
563 | return ( | |
564 | cell[0] == color1 && | |
565 | cell[1] != V.KING && | |
596e24d0 | 566 | (cell[1] != V.PAWN || i != lastRank[oppCol]) |
00eef1ca BA |
567 | ); |
568 | }) | |
569 | ) | |
570 | ) { | |
571 | effects.push("waluigi"); | |
572 | } | |
573 | const effect = effects[randInt(effects.length)]; | |
574 | move.end.effect = effect; | |
575 | // 2) Apply it if possible | |
576 | if (!(["kingboo", "toadette", "daisy"].includes(effect))) { | |
577 | switch (effect) { | |
578 | case "koopa": | |
579 | move.appear = []; | |
580 | // Maybe egg effect was applied after others, | |
581 | // so just shift vanish array: | |
582 | move.vanish.shift(); | |
583 | break; | |
584 | case "chomp": | |
585 | move.appear = []; | |
586 | break; | |
587 | case "bowser": | |
588 | move.appear[0].p = V.IMMOBILIZE_CODE[piece1]; | |
589 | break; | |
590 | case "luigi": | |
591 | changePieceColor(oppCol); | |
592 | break; | |
593 | case "waluigi": | |
594 | changePieceColor(color1); | |
595 | break; | |
15d69043 BA |
596 | } |
597 | } | |
598 | }; | |
599 | const applyMushroomEffect = () => { | |
600 | if ([V.PAWN, V.KING, V.KNIGHT].includes(piece1)) { | |
601 | // Just make another similar step, if possible (non-capturing) | |
602 | const [i, j] = [ | |
00eef1ca BA |
603 | move.appear[0].x + (x2 - x1), |
604 | move.appear[0].y + (y2 - y1) | |
15d69043 BA |
605 | ]; |
606 | if ( | |
607 | V.OnBoard(i, j) && | |
ccbb9dfc BA |
608 | ( |
609 | this.board[i][j] == V.EMPTY || | |
610 | this.getPiece(i, j) == V.INVISIBLE_QUEEN || | |
611 | this.getColor(i, j) == 'a' | |
612 | ) | |
15d69043 BA |
613 | ) { |
614 | move.appear[0].x = i; | |
615 | move.appear[0].y = j; | |
616 | if (this.board[i][j] != V.EMPTY) { | |
617 | const object = this.getPiece(i, j); | |
ccbb9dfc | 618 | const color = this.getColor(i, j); |
15d69043 BA |
619 | move.vanish.push( |
620 | new PiPo({ | |
621 | x: i, | |
622 | y: j, | |
ccbb9dfc | 623 | c: color, |
15d69043 BA |
624 | p: object |
625 | }) | |
626 | ); | |
627 | switch (object) { | |
628 | case V.BANANA: | |
15d69043 | 629 | case V.BOMB: |
77fc0c65 BA |
630 | const steps = V.steps[object == V.BANANA ? V.ROOK : V.BISHOP]; |
631 | move.next = this.getRandomSquare([i, j], steps); | |
15d69043 BA |
632 | break; |
633 | case V.EGG: | |
634 | applyEggEffect(); | |
635 | break; | |
636 | case V.MUSHROOM: | |
637 | applyMushroomEffect(); | |
638 | break; | |
639 | } | |
640 | } | |
641 | } | |
642 | } | |
643 | else { | |
644 | // Queen, bishop or rook: | |
645 | const step = [ | |
646 | (x2 - x1) / Math.abs(x2 - x1) || 0, | |
647 | (y2 - y1) / Math.abs(y2 - y1) || 0 | |
648 | ]; | |
649 | const next = [move.appear[0].x + step[0], move.appear[0].y + step[1]]; | |
650 | if ( | |
651 | V.OnBoard(next[0], next[1]) && | |
652 | this.board[next[0]][next[1]] != V.EMPTY && | |
ccbb9dfc | 653 | this.getPiece(next[0], next[1]) != V.INVISIBLE_QUEEN && |
15d69043 BA |
654 | this.getColor(next[0], next[1]) != 'a' |
655 | ) { | |
656 | const afterNext = [next[0] + step[0], next[1] + step[1]]; | |
596e24d0 | 657 | if (V.OnBoard(afterNext[0], afterNext[1])) { |
ccbb9dfc | 658 | const afterColor = this.getColor(afterNext[0], afterNext[1]); |
596e24d0 | 659 | if ( |
15d69043 | 660 | this.board[afterNext[0]][afterNext[1]] == V.EMPTY || |
d73b8521 | 661 | afterColor == 'a' |
596e24d0 BA |
662 | ) { |
663 | move.appear[0].x = afterNext[0]; | |
664 | move.appear[0].y = afterNext[1]; | |
665 | if (this.board[afterNext[0]][afterNext[1]] != V.EMPTY) { | |
d73b8521 | 666 | // object = banana, bomb, mushroom or egg |
596e24d0 BA |
667 | const object = this.getPiece(afterNext[0], afterNext[1]); |
668 | move.vanish.push( | |
669 | new PiPo({ | |
670 | x: afterNext[0], | |
671 | y: afterNext[1], | |
672 | c: afterColor, | |
673 | p: object | |
674 | }) | |
675 | ); | |
676 | switch (object) { | |
677 | case V.BANANA: | |
596e24d0 | 678 | case V.BOMB: |
77fc0c65 BA |
679 | const steps = |
680 | V.steps[object == V.BANANA ? V.ROOK : V.BISHOP]; | |
681 | move.next = this.getRandomSquare( | |
682 | [afterNext[0], afterNext[1]], steps); | |
596e24d0 BA |
683 | break; |
684 | case V.EGG: | |
685 | applyEggEffect(); | |
686 | break; | |
687 | case V.MUSHROOM: | |
688 | applyMushroomEffect(); | |
689 | break; | |
690 | } | |
15d69043 BA |
691 | } |
692 | } | |
693 | } | |
694 | } | |
695 | } | |
696 | }; | |
596e24d0 BA |
697 | const color2 = this.getColor(x2, y2); |
698 | const piece2 = this.getPiece(x2, y2); | |
15d69043 BA |
699 | if (color2 == 'a') { |
700 | switch (piece2) { | |
701 | case V.BANANA: | |
15d69043 | 702 | case V.BOMB: |
77fc0c65 BA |
703 | const steps = V.steps[piece2 == V.BANANA ? V.ROOK : V.BISHOP]; |
704 | move.next = this.getRandomSquare([x2, y2], steps); | |
15d69043 BA |
705 | break; |
706 | case V.MUSHROOM: | |
707 | applyMushroomEffect(); | |
708 | break; | |
709 | case V.EGG: | |
596e24d0 | 710 | if (this.subTurn == 1) |
15d69043 | 711 | // No egg effect at subTurn 2 |
15d69043 | 712 | applyEggEffect(); |
15d69043 BA |
713 | break; |
714 | } | |
715 | } | |
00eef1ca | 716 | if ( |
15d69043 | 717 | this.subTurn == 1 && |
77fc0c65 | 718 | !move.next && |
596e24d0 | 719 | move.appear.length > 0 && |
15d69043 BA |
720 | [V.ROOK, V.BISHOP].includes(piece1) |
721 | ) { | |
596e24d0 BA |
722 | const finalSquare = [move.appear[0].x, move.appear[0].y]; |
723 | if ( | |
724 | color2 != 'a' || | |
725 | this.getColor(finalSquare[0], finalSquare[1]) != 'a' || | |
726 | this.getPiece(finalSquare[0], finalSquare[1]) != V.EGG | |
727 | ) { | |
728 | const validSteps = | |
729 | V.steps[piece1 == V.ROOK ? V.BISHOP : V.ROOK].filter(s => { | |
730 | const [i, j] = [finalSquare[0] + s[0], finalSquare[1] + s[1]]; | |
731 | return ( | |
732 | V.OnBoard(i, j) && | |
ccbb9dfc | 733 | // NOTE: do not place a bomb or banana on the invisible queen! |
596e24d0 BA |
734 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a') |
735 | ); | |
736 | }); | |
77fc0c65 | 737 | if (validSteps.length >= 1) { |
ccbb9dfc | 738 | const randIdx = randInt(validSteps.length); |
596e24d0 | 739 | const [x, y] = [ |
ccbb9dfc BA |
740 | finalSquare[0] + validSteps[randIdx][0], |
741 | finalSquare[1] + validSteps[randIdx][1] | |
596e24d0 BA |
742 | ]; |
743 | move.appear.push( | |
744 | new PiPo({ | |
745 | x: x, | |
746 | y: y, | |
747 | c: 'a', | |
748 | p: (piece1 == V.ROOK ? V.BANANA : V.BOMB) | |
749 | }) | |
750 | ); | |
751 | if (this.board[x][y] != V.EMPTY) { | |
752 | move.vanish.push( | |
753 | new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) })); | |
754 | } | |
755 | } | |
756 | } | |
15d69043 | 757 | } |
77fc0c65 BA |
758 | return move; |
759 | } | |
760 | ||
761 | getBasicMove(psq1, sq2, tr) { | |
762 | let moves = []; | |
763 | if (Array.isArray(psq1)) psq1 = { x: psq1[0], y: psq1[1] }; | |
764 | let m = this.getBasicMove_aux(psq1, sq2, tr, "initMove"); | |
765 | while (!!m.next) { | |
766 | // Last move ended on bomb or banana, direction change | |
767 | V.PlayOnBoard(this.board, m); | |
768 | moves.push(m); | |
769 | m = this.getBasicMove_aux( | |
770 | { x: m.appear[0].x, y: m.appear[0].y }, m.next); | |
771 | } | |
772 | for (let i=moves.length-1; i>=0; i--) V.UndoOnBoard(this.board, moves[i]); | |
773 | moves.push(m); | |
774 | // Now merge moves into one | |
775 | let move = {}; | |
776 | // start is wrong for Toadette moves --> it's fixed later | |
777 | move.start = { x: psq1.x, y: psq1.y }; | |
778 | move.end = !!sq2 ? { x: sq2[0], y: sq2[1] } : { x: psq1.x, y: psq1.y }; | |
b36db525 | 779 | if (!!tr) move.promoteInto = moves[0].promoteInto; |
77fc0c65 BA |
780 | let lm = moves[moves.length-1]; |
781 | if (this.subTurn == 1 && !!lm.end.effect) | |
782 | move.end.effect = lm.end.effect; | |
783 | if (moves.length == 1) { | |
784 | move.appear = moves[0].appear; | |
785 | move.vanish = moves[0].vanish; | |
786 | } | |
787 | else { | |
788 | // Keep first vanish and last appear (if any) | |
789 | move.appear = lm.appear; | |
790 | move.vanish = moves[0].vanish; | |
791 | if ( | |
792 | move.vanish.length >= 1 && | |
793 | move.appear.length >= 1 && | |
794 | move.vanish[0].x == move.appear[0].x && | |
795 | move.vanish[0].y == move.appear[0].y | |
796 | ) { | |
797 | // Loopback on initial square: | |
798 | move.vanish.shift(); | |
799 | move.appear.shift(); | |
800 | } | |
801 | for (let i=1; i < moves.length - 1; i++) { | |
802 | for (let v of moves[i].vanish) { | |
803 | // Only vanishing objects, not appearing at init move | |
804 | if ( | |
805 | v.c == 'a' && | |
806 | ( | |
807 | moves[0].appear.length == 1 || | |
808 | moves[0].appear[1].x != v.x || | |
809 | moves[0].appear[1].y != v.y | |
810 | ) | |
811 | ) { | |
812 | move.vanish.push(v); | |
813 | } | |
814 | } | |
815 | } | |
816 | // Final vanish is our piece, but others might be relevant | |
817 | // (for some egg bonuses at least). | |
818 | for (let i=1; i < lm.vanish.length; i++) { | |
819 | if ( | |
820 | lm.vanish[i].c != 'a' || | |
821 | moves[0].appear.length == 1 || | |
822 | moves[0].appear[1].x != lm.vanish[i].x || | |
823 | moves[0].appear[1].y != lm.vanish[i].y | |
824 | ) { | |
825 | move.vanish.push(lm.vanish[i]); | |
826 | } | |
827 | } | |
b967d5ba | 828 | } |
596e24d0 | 829 | return move; |
b967d5ba BA |
830 | } |
831 | ||
15d69043 BA |
832 | getPotentialPawnMoves([x, y]) { |
833 | const color = this.turn; | |
834 | const oppCol = V.GetOppCol(color); | |
835 | const [sizeX, sizeY] = [V.size.x, V.size.y]; | |
836 | const shiftX = V.PawnSpecs.directions[color]; | |
837 | const firstRank = (color == "w" ? sizeX - 1 : 0); | |
838 | let moves = []; | |
839 | if ( | |
840 | this.board[x + shiftX][y] == V.EMPTY || | |
ccbb9dfc BA |
841 | this.getColor(x + shiftX, y) == 'a' || |
842 | this.getPiece(x + shiftX, y) == V.INVISIBLE_QUEEN | |
15d69043 BA |
843 | ) { |
844 | this.addPawnMoves([x, y], [x + shiftX, y], moves); | |
845 | if ( | |
846 | [firstRank, firstRank + shiftX].includes(x) && | |
00eef1ca BA |
847 | ( |
848 | this.board[x + 2 * shiftX][y] == V.EMPTY || | |
ccbb9dfc BA |
849 | this.getColor(x + 2 * shiftX, y) == 'a' || |
850 | this.getPiece(x + 2 * shiftX, y) == V.INVISIBLE_QUEEN | |
00eef1ca | 851 | ) |
15d69043 | 852 | ) { |
77fc0c65 | 853 | moves.push(this.getBasicMove({ x: x, y: y }, [x + 2 * shiftX, y])); |
15d69043 BA |
854 | } |
855 | } | |
856 | for (let shiftY of [-1, 1]) { | |
857 | if ( | |
858 | y + shiftY >= 0 && | |
859 | y + shiftY < sizeY && | |
860 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
ccbb9dfc BA |
861 | // Pawns cannot capture invisible queen this way! |
862 | this.getPiece(x + shiftX, y + shiftY) != V.INVISIBLE_QUEEN && | |
596e24d0 | 863 | ['a', oppCol].includes(this.getColor(x + shiftX, y + shiftY)) |
15d69043 BA |
864 | ) { |
865 | this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves); | |
866 | } | |
867 | } | |
15d69043 BA |
868 | return moves; |
869 | } | |
870 | ||
b967d5ba BA |
871 | getPotentialQueenMoves(sq) { |
872 | const normalMoves = super.getPotentialQueenMoves(sq); | |
873 | // If flag allows it, add 'invisible movements' | |
874 | let invisibleMoves = []; | |
875 | if (this.powerFlags[this.turn][V.QUEEN]) { | |
876 | normalMoves.forEach(m => { | |
596e24d0 BA |
877 | if ( |
878 | m.appear.length == 1 && | |
879 | m.vanish.length == 1 && | |
880 | // Only simple non-capturing moves: | |
881 | m.vanish[0].c != 'a' | |
882 | ) { | |
b967d5ba | 883 | let im = JSON.parse(JSON.stringify(m)); |
00eef1ca BA |
884 | im.appear[0].p = V.INVISIBLE_QUEEN; |
885 | im.end.noHighlight = true; | |
b967d5ba BA |
886 | invisibleMoves.push(im); |
887 | } | |
888 | }); | |
889 | } | |
890 | return normalMoves.concat(invisibleMoves); | |
891 | } | |
892 | ||
5d75c82c BA |
893 | getPotentialKingMoves([x, y]) { |
894 | let moves = super.getPotentialKingMoves([x, y]); | |
b967d5ba BA |
895 | const color = this.turn; |
896 | // If flag allows it, add 'remote shell captures' | |
897 | if (this.powerFlags[this.turn][V.KING]) { | |
898 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(step => { | |
a1075a51 BA |
899 | let [i, j] = [x + step[0], y + step[1]]; |
900 | while ( | |
901 | V.OnBoard(i, j) && | |
596e24d0 | 902 | ( |
a1075a51 | 903 | this.board[i][j] == V.EMPTY || |
ccbb9dfc | 904 | this.getPiece(i, j) == V.INVISIBLE_QUEEN || |
596e24d0 | 905 | ( |
a1075a51 BA |
906 | this.getColor(i, j) == 'a' && |
907 | [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j)) | |
596e24d0 BA |
908 | ) |
909 | ) | |
00eef1ca | 910 | ) { |
a1075a51 BA |
911 | i += step[0]; |
912 | j += step[1]; | |
913 | } | |
914 | if (V.OnBoard(i, j)) { | |
915 | const colIJ = this.getColor(i, j); | |
916 | if (colIJ != color) { | |
917 | // May just destroy a bomb or banana: | |
918 | moves.push( | |
919 | new Move({ | |
920 | start: { x: x, y: y}, | |
921 | end: { x: i, y: j }, | |
922 | appear: [], | |
923 | vanish: [ | |
924 | new PiPo({ | |
925 | x: i, y: j, c: colIJ, p: this.getPiece(i, j) | |
926 | }) | |
927 | ] | |
928 | }) | |
929 | ); | |
00eef1ca | 930 | } |
b967d5ba | 931 | } |
b967d5ba BA |
932 | }); |
933 | } | |
5d75c82c BA |
934 | return moves; |
935 | } | |
936 | ||
937 | getSlideNJumpMoves([x, y], steps, oneStep) { | |
938 | let moves = []; | |
939 | outerLoop: for (let step of steps) { | |
940 | let i = x + step[0]; | |
941 | let j = y + step[1]; | |
942 | while ( | |
943 | V.OnBoard(i, j) && | |
944 | ( | |
945 | this.board[i][j] == V.EMPTY || | |
77fc0c65 | 946 | this.getPiece(i, j) == V.INVISIBLE_QUEEN || |
5d75c82c BA |
947 | ( |
948 | this.getColor(i, j) == 'a' && | |
949 | [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j)) | |
950 | ) | |
951 | ) | |
952 | ) { | |
77fc0c65 | 953 | moves.push(this.getBasicMove({ x: x, y: y }, [i, j])); |
5d75c82c BA |
954 | if (oneStep) continue outerLoop; |
955 | i += step[0]; | |
956 | j += step[1]; | |
957 | } | |
958 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j])) | |
77fc0c65 | 959 | moves.push(this.getBasicMove({ x: x, y: y }, [i, j])); |
5d75c82c BA |
960 | } |
961 | return moves; | |
90df90bc BA |
962 | } |
963 | ||
ad030c7d | 964 | getAllPotentialMoves() { |
5d75c82c | 965 | if (this.subTurn == 1) return super.getAllPotentialMoves(); |
b967d5ba | 966 | let moves = []; |
54879803 BA |
967 | const color = this.turn; |
968 | const L = this.effects.length; | |
969 | switch (this.effects[L-1]) { | |
00eef1ca | 970 | case "kingboo": { |
54879803 | 971 | let allPieces = []; |
15d69043 BA |
972 | for (let i=0; i<8; i++) { |
973 | for (let j=0; j<8; j++) { | |
974 | const colIJ = this.getColor(i, j); | |
ccbb9dfc | 975 | const pieceIJ = this.getPiece(i, j); |
15d69043 | 976 | if ( |
54879803 | 977 | i != x && j != y && |
15d69043 | 978 | this.board[i][j] != V.EMPTY && |
ccbb9dfc BA |
979 | colIJ != 'a' && |
980 | pieceIJ != V.INVISIBLE_QUEEN | |
15d69043 | 981 | ) { |
ccbb9dfc | 982 | allPieces.push({ x: i, y: j, c: colIJ, p: pieceIJ }); |
54879803 BA |
983 | } |
984 | } | |
985 | } | |
986 | for (let x=0; x<8; x++) { | |
987 | for (let y=0; y<8; y++) { | |
988 | if (this.getColor(i, j) == color) { | |
989 | // Add exchange with something | |
990 | allPieces.forEach(pp => { | |
991 | if (pp.x != i || pp.y != j) { | |
992 | const movedUnit = new PiPo({ | |
993 | x: x, | |
994 | y: y, | |
995 | c: pp.c, | |
996 | p: pp.p | |
997 | }); | |
998 | let mMove = this.getBasicMove({ x: x, y: y }, [pp.x, pp.y]); | |
999 | mMove.appear.push(movedUnit); | |
1000 | moves.push(mMove); | |
1001 | } | |
15d69043 | 1002 | }); |
15d69043 BA |
1003 | } |
1004 | } | |
1005 | } | |
1006 | break; | |
1007 | } | |
00eef1ca | 1008 | case "toadette": { |
15d69043 BA |
1009 | const x = V.size.x + (this.turn == 'w' ? 0 : 1); |
1010 | for (let y = 0; y < 8; y++) | |
1011 | Array.prototype.push.apply(moves, this.getReserveMoves([x, y])); | |
1012 | break; | |
1013 | } | |
00eef1ca | 1014 | case "daisy": |
54879803 | 1015 | moves = super.getAllPotentialMoves(); |
15d69043 BA |
1016 | break; |
1017 | } | |
1018 | return moves; | |
6c7cbfed BA |
1019 | } |
1020 | ||
5d75c82c | 1021 | play(move) { |
596e24d0 BA |
1022 | // if (!this.states) this.states = []; |
1023 | // const stateFen = this.getFen(); | |
1024 | // this.states.push(stateFen); | |
1025 | ||
b967d5ba | 1026 | move.flags = JSON.stringify(this.aggregateFlags()); |
b967d5ba | 1027 | V.PlayOnBoard(this.board, move); |
15d69043 | 1028 | move.turn = [this.turn, this.subTurn]; |
54879803 BA |
1029 | if (["kingboo", "toadette", "daisy"].includes(move.end.effect)) { |
1030 | this.effects.push(move.end.effect); | |
b967d5ba | 1031 | this.subTurn = 2; |
b967d5ba BA |
1032 | } |
1033 | else { | |
1034 | this.turn = V.GetOppCol(this.turn); | |
15d69043 | 1035 | this.movesCount++; |
b967d5ba | 1036 | this.subTurn = 1; |
b967d5ba | 1037 | } |
15d69043 | 1038 | this.postPlay(move); |
1c15969e | 1039 | } |
5d75c82c | 1040 | |
b9ce3d0f | 1041 | postPlay(move) { |
00eef1ca BA |
1042 | if (move.end.effect == "toadette") this.reserve = this.captured; |
1043 | else this.reserve = undefined; | |
596e24d0 | 1044 | const color = move.turn[0]; |
332f3169 BA |
1045 | if ( |
1046 | move.vanish.length == 2 && | |
1047 | move.vanish[1].c != 'a' && | |
1048 | move.appear.length == 1 //avoid king Boo! | |
1049 | ) { | |
b9ce3d0f | 1050 | // Capture: update this.captured |
77fc0c65 BA |
1051 | let capturedPiece = move.vanish[1].p; |
1052 | if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN; | |
1053 | else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece)) | |
1054 | capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece]; | |
1055 | this.captured[move.vanish[1].c][capturedPiece]++; | |
1056 | } | |
5d75c82c | 1057 | else if (move.vanish.length == 0) { |
00eef1ca | 1058 | if (move.appear.length == 0 || move.appear[0].c == 'a') return; |
5d75c82c | 1059 | // A piece is back on board |
00eef1ca | 1060 | this.captured[move.appear[0].c][move.appear[0].p]--; |
5d75c82c | 1061 | } |
596e24d0 BA |
1062 | if (move.appear.length == 0) { |
1063 | // Three cases: king "shell capture", Chomp or Koopa | |
1064 | if (this.getPiece(move.start.x, move.start.y) == V.KING) | |
1065 | // King remote capture: | |
1066 | this.powerFlags[color][V.KING] = false; | |
1067 | else if (move.end.effect == "chomp") | |
1068 | this.captured[color][move.vanish[0].p]++; | |
1069 | } | |
1070 | else if (move.appear[0].p == V.INVISIBLE_QUEEN) | |
1071 | this.powerFlags[move.appear[0].c][V.QUEEN] = false; | |
b36db525 | 1072 | if (this.subTurn == 2) return; |
596e24d0 | 1073 | if ( |
b36db525 | 1074 | move.turn[1] == 1 && |
596e24d0 BA |
1075 | move.appear.length == 0 || |
1076 | !(Object.keys(V.IMMOBILIZE_DECODE).includes(move.appear[0].p)) | |
1077 | ) { | |
1078 | // Look for an immobilized piece of my color: it can now move | |
596e24d0 BA |
1079 | for (let i=0; i<8; i++) { |
1080 | for (let j=0; j<8; j++) { | |
1081 | if (this.board[i][j] != V.EMPTY) { | |
596e24d0 BA |
1082 | const piece = this.getPiece(i, j); |
1083 | if ( | |
b36db525 | 1084 | this.getColor(i, j) == color && |
596e24d0 BA |
1085 | Object.keys(V.IMMOBILIZE_DECODE).includes(piece) |
1086 | ) { | |
1087 | this.board[i][j] = color + V.IMMOBILIZE_DECODE[piece]; | |
1088 | move.wasImmobilized = [i, j]; | |
1089 | } | |
15d69043 BA |
1090 | } |
1091 | } | |
1092 | } | |
1093 | } | |
b36db525 BA |
1094 | // Also make opponent invisible queen visible again, if any |
1095 | const oppCol = V.GetOppCol(color); | |
1096 | for (let i=0; i<8; i++) { | |
1097 | for (let j=0; j<8; j++) { | |
1098 | if ( | |
1099 | this.board[i][j] != V.EMPTY && | |
1100 | this.getColor(i, j) == oppCol && | |
1101 | this.getPiece(i, j) == V.INVISIBLE_QUEEN | |
1102 | ) { | |
1103 | this.board[i][j] = oppCol + V.QUEEN; | |
1104 | move.wasInvisible = [i, j]; | |
1105 | } | |
1106 | } | |
1107 | } | |
5d75c82c BA |
1108 | } |
1109 | ||
1110 | undo(move) { | |
15d69043 BA |
1111 | this.disaggregateFlags(JSON.parse(move.flags)); |
1112 | V.UndoOnBoard(this.board, move); | |
54879803 BA |
1113 | if (["kingboo", "toadette", "daisy"].includes(move.end.effect)) |
1114 | this.effects.pop(); | |
15d69043 | 1115 | else this.movesCount--; |
15d69043 BA |
1116 | this.turn = move.turn[0]; |
1117 | this.subTurn = move.turn[1]; | |
1118 | this.postUndo(move); | |
596e24d0 BA |
1119 | |
1120 | // const stateFen = this.getFen(); | |
1121 | // if (stateFen != this.states[this.states.length-1]) debugger; | |
1122 | // this.states.pop(); | |
b9ce3d0f BA |
1123 | } |
1124 | ||
1125 | postUndo(move) { | |
15d69043 BA |
1126 | if (!!move.wasImmobilized) { |
1127 | const [i, j] = move.wasImmobilized; | |
1128 | this.board[i][j] = | |
1129 | this.getColor(i, j) + V.IMMOBILIZE_CODE[this.getPiece(i, j)]; | |
1130 | } | |
1131 | if (!!move.wasInvisible) { | |
1132 | const [i, j] = move.wasInvisible; | |
596e24d0 | 1133 | this.board[i][j] = this.getColor(i, j) + V.INVISIBLE_QUEEN; |
15d69043 | 1134 | } |
77fc0c65 BA |
1135 | if (move.vanish.length == 2 && move.vanish[1].c != 'a') { |
1136 | let capturedPiece = move.vanish[1].p; | |
1137 | if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN; | |
1138 | else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece)) | |
1139 | capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece]; | |
1140 | this.captured[move.vanish[1].c][capturedPiece]--; | |
1141 | } | |
15d69043 | 1142 | else if (move.vanish.length == 0) { |
00eef1ca BA |
1143 | if (move.appear.length == 0 || move.appear[0].c == 'a') return; |
1144 | // A piece was back on board | |
1145 | this.captured[move.appear[0].c][move.appear[0].p]++; | |
15d69043 | 1146 | } |
596e24d0 BA |
1147 | else if (move.appear.length == 0 && move.end.effect == "chomp") |
1148 | this.captured[move.vanish[0].c][move.vanish[0].p]--; | |
15d69043 | 1149 | if (move.vanish.length == 0) this.reserve = this.captured; |
00eef1ca | 1150 | else this.reserve = undefined; |
b9ce3d0f | 1151 | } |
1c15969e | 1152 | |
5d75c82c BA |
1153 | getCheckSquares() { |
1154 | return []; | |
1155 | } | |
1156 | ||
6c7cbfed | 1157 | getCurrentScore() { |
5d75c82c BA |
1158 | // Find kings (not tracked in this variant) |
1159 | let kingThere = { w: false, b: false }; | |
1160 | for (let i=0; i<8; i++) { | |
1161 | for (let j=0; j<8; j++) { | |
00eef1ca BA |
1162 | if ( |
1163 | this.board[i][j] != V.EMPTY && | |
1164 | ['k', 'l'].includes(this.getPiece(i, j)) | |
1165 | ) { | |
5d75c82c | 1166 | kingThere[this.getColor(i, j)] = true; |
00eef1ca | 1167 | } |
5d75c82c BA |
1168 | } |
1169 | } | |
1170 | if (!kingThere['w']) return "0-1"; | |
1171 | if (!kingThere['b']) return "1-0"; | |
00eef1ca | 1172 | if (!this.atLeastOneMove()) return (this.turn == 'w' ? "0-1" : "1-0"); |
5d75c82c | 1173 | return "*"; |
6c7cbfed BA |
1174 | } |
1175 | ||
4313762d | 1176 | static GenRandInitFen(options) { |
b9ce3d0f | 1177 | return ( |
4313762d | 1178 | SuicideRules.GenRandInitFen(options).slice(0, -1) + |
596e24d0 BA |
1179 | // Add Peach + Mario flags + capture counts |
1180 | "1111 000000000000" | |
b9ce3d0f BA |
1181 | ); |
1182 | } | |
1183 | ||
5d75c82c BA |
1184 | filterValid(moves) { |
1185 | return moves; | |
1186 | } | |
1187 | ||
13ce1e9b BA |
1188 | static get VALUES() { |
1189 | return Object.assign( | |
1190 | {}, | |
1191 | ChessRules.VALUES, | |
1192 | { | |
1193 | s: 1, | |
1194 | u: 5, | |
1195 | o: 3, | |
1196 | c: 3, | |
1197 | t: 9, | |
1198 | l: 1000, | |
1199 | e: 0, | |
1200 | d: 0, | |
1201 | w: 0, | |
1202 | m: 0 | |
1203 | } | |
1204 | ); | |
1205 | } | |
1206 | ||
1207 | static get SEARCH_DEPTH() { | |
1208 | return 1; | |
1209 | } | |
1210 | ||
1c15969e | 1211 | getComputerMove() { |
5d75c82c | 1212 | const moves = this.getAllValidMoves(); |
13ce1e9b BA |
1213 | // Split into "normal" and "random" moves: |
1214 | // (Next splitting condition is OK because cannot take self object | |
1215 | // without a banana or bomb on the way). | |
1216 | const deterministicMoves = moves.filter(m => { | |
1217 | return m.vanish.every(a => a.c != 'a' || a.p == V.MUSHROOM); | |
1218 | }); | |
1219 | const randomMoves = moves.filter(m => { | |
1220 | return m.vanish.some(a => a.c == 'a' && a.p != V.MUSHROOM); | |
1221 | }); | |
1222 | if (Math.random() < deterministicMoves.length / randomMoves.length) | |
1223 | // Play a deterministic one: capture king or material if possible | |
1224 | return super.getComputerMove(deterministicMoves); | |
1225 | // Play a random effect move, at random: | |
032ecd58 | 1226 | let move1 = randomMoves[randInt(randomMoves.length)]; |
b967d5ba BA |
1227 | this.play(move1); |
1228 | let move2 = undefined; | |
1229 | if (this.subTurn == 2) { | |
1230 | const moves2 = this.getAllValidMoves(); | |
1231 | move2 = moves2[randInt(moves2.length)]; | |
1232 | } | |
1233 | this.undo(move1); | |
1234 | if (!move2) return move1; | |
1235 | return [move1, move2]; | |
1c15969e | 1236 | } |
b9ce3d0f BA |
1237 | |
1238 | getNotation(move) { | |
77fc0c65 | 1239 | if (move.vanish.length == 0 && move.appear.length == 0) return "-"; |
00eef1ca BA |
1240 | if ( |
1241 | !move.end.effect && | |
1242 | move.appear.length > 0 && | |
1243 | move.appear[0].p == V.INVISIBLE_QUEEN | |
1244 | ) { | |
1245 | return "Q??"; | |
1246 | } | |
1247 | const finalSquare = V.CoordsToSquare(move.end); | |
77fc0c65 BA |
1248 | // Next condition also includes Toadette placements: |
1249 | if (move.appear.length > 0 && move.vanish.every(a => a.c == 'a')) { | |
1250 | const piece = | |
1251 | move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : ""; | |
1252 | return piece + "@" + finalSquare; | |
1253 | } | |
1254 | else if (move.appear.length == 0) { | |
1255 | const piece = this.getPiece(move.start.x, move.start.y); | |
1256 | if (piece == V.KING && !move.end.effect) | |
1257 | // King remote capture | |
1258 | return "Kx" + finalSquare; | |
1259 | // Koopa or Chomp, or loopback after bananas, bombs & mushrooms: | |
00eef1ca BA |
1260 | return ( |
1261 | piece.toUpperCase() + "x" + finalSquare + | |
77fc0c65 BA |
1262 | ( |
1263 | !!move.end.effect | |
1264 | ? "*" + (move.end.effect == "koopa" ? "K" : "C") | |
1265 | : "" | |
1266 | ) | |
00eef1ca BA |
1267 | ); |
1268 | } | |
42e37983 BA |
1269 | if (move.appear.length == 1 && move.vanish.length == 1) { |
1270 | const moveStart = move.appear[0].p.toUpperCase() + "@"; | |
1271 | if (move.appear[0].c == 'a' && move.vanish[0].c == 'a') | |
1272 | // Bonus replacement: | |
1273 | return moveStart + finalSquare; | |
1274 | if ( | |
1275 | move.vanish[0].p == V.INVISIBLE_QUEEN && | |
1276 | move.appear[0].x == move.vanish[0].x && | |
1277 | move.appear[0].y == move.vanish[0].y | |
1278 | ) { | |
1279 | // Toadette takes invisible queen | |
1280 | return moveStart + "Q" + finalSquare; | |
1281 | } | |
596e24d0 | 1282 | } |
77fc0c65 BA |
1283 | if ( |
1284 | move.appear.length == 2 && | |
1285 | move.vanish.length == 2 && | |
1286 | move.appear.every(a => a.c != 'a') && | |
1287 | move.vanish.every(v => v.c != 'a') | |
1288 | ) { | |
1289 | // King Boo exchange | |
54879803 | 1290 | return V.CoordsToSquare(move.start) + finalSquare; |
77fc0c65 BA |
1291 | } |
1292 | const piece = move.vanish[0].p; | |
00eef1ca BA |
1293 | let notation = undefined; |
1294 | if (piece == V.PAWN) { | |
1295 | // Pawn move | |
580c3d09 | 1296 | if (this.board[move.end.x][move.end.y] != V.EMPTY) { |
00eef1ca BA |
1297 | // Capture |
1298 | const startColumn = V.CoordToColumn(move.start.y); | |
1299 | notation = startColumn + "x" + finalSquare; | |
1300 | } | |
1301 | else notation = finalSquare; | |
1302 | if (move.appear[0].p != V.PAWN) | |
1303 | // Promotion | |
1304 | notation += "=" + move.appear[0].p.toUpperCase(); | |
1305 | } | |
1306 | else { | |
1307 | notation = | |
1308 | piece.toUpperCase() + | |
580c3d09 | 1309 | (this.board[move.end.x][move.end.y] != V.EMPTY ? "x" : "") + |
00eef1ca BA |
1310 | finalSquare; |
1311 | } | |
1312 | if (!!move.end.effect) { | |
1313 | switch (move.end.effect) { | |
1314 | case "kingboo": | |
1315 | notation += "*B"; | |
1316 | break; | |
1317 | case "toadette": | |
1318 | notation += "*T"; | |
1319 | break; | |
1320 | case "daisy": | |
1321 | notation += "*D"; | |
1322 | break; | |
1323 | case "bowser": | |
1324 | notation += "*M"; | |
1325 | break; | |
1326 | case "luigi": | |
00eef1ca | 1327 | case "waluigi": |
77fc0c65 BA |
1328 | const lastAppear = move.appear[move.appear.length - 1]; |
1329 | const effectOn = | |
1330 | V.CoordsToSquare({ x: lastAppear.x, y : lastAppear.y }); | |
1331 | notation += "*" + move.end.effect[0].toUpperCase() + effectOn; | |
00eef1ca BA |
1332 | break; |
1333 | } | |
1334 | } | |
00eef1ca | 1335 | return notation; |
b9ce3d0f | 1336 | } |
7e8a7ea1 | 1337 | |
6c7cbfed | 1338 | }; |