1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class PandemoniumRules
extends ChessRules
{
6 static get PawnSpecs() {
10 { promotions: [V
.GILDING
] }
15 // If current side is under check: lost
16 return this.underCheck(this.turn
);
19 static get GILDING() {
23 static get SCEPTER() {
35 static get CARDINAL() {
43 static get MARSHAL() {
47 static get APRICOT() {
53 ChessRules
.PIECES
.concat([
54 V
.GILDING
, V
.SCEPTER
, V
.HORSE
, V
.DRAGON
,
55 V
.CARDINAL
, V
.WHOLE
, V
.MARSHAL
, V
.APRICOT
])
60 const prefix
= (ChessRules
.PIECES
.includes(b
[1]) ? "" : "Pandemonium/");
65 return { x: 10, y: 10};
69 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
70 return this.board
[i
][j
].charAt(0);
74 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
75 return this.board
[i
][j
].charAt(1);
78 setOtherVariables(fen
) {
79 super.setOtherVariables(fen
);
80 // Sub-turn is useful only at first move...
82 // Also init reserves (used by the interface to show landable pieces)
84 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
89 [V
.KNIGHT
]: reserve
[2],
90 [V
.BISHOP
]: reserve
[3],
91 [V
.QUEEN
]: reserve
[4],
92 [V
.CARDINAL
]: reserve
[5],
93 [V
.MARSHAL
]: reserve
[6],
98 [V
.KNIGHT
]: reserve
[9],
99 [V
.BISHOP
]: reserve
[10],
100 [V
.QUEEN
]: reserve
[11],
101 [V
.CARDINAL
]: reserve
[12],
102 [V
.MARSHAL
]: reserve
[13]
107 static IsGoodEnpassant(enpassant
) {
108 if (enpassant
!= "-") {
109 const squares
= enpassant
.split(",");
110 if (squares
.length
> 2) return false;
111 for (let sq
of squares
) {
112 if (!sq
.match(/[a-j0-9]/)) return false;
118 static IsGoodFen(fen
) {
119 if (!ChessRules
.IsGoodFen(fen
)) return false;
120 const fenParsed
= V
.ParseFen(fen
);
122 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
127 static ParseFen(fen
) {
128 const fenParts
= fen
.split(" ");
129 return Object
.assign(
130 ChessRules
.ParseFen(fen
),
131 { reserve: fenParts
[5] }
136 return super.getFen() + " " + this.getReserveFen();
140 return super.getFenForRepeat() + "_" + this.getReserveFen();
144 let counts
= new Array(14);
145 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
146 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
147 counts
[7 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
149 return counts
.join("");
152 static GenRandInitFen(randomness
) {
153 // No randomization here for now (but initial setup choice)
155 "rnbqkmcbnr/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/RNBQKMCBNR " +
156 "w 0 ajaj - 00000000000000"
158 // TODO later: randomization too --> 2 bishops, not next to each other.
159 // then knights next to bishops. Then other pieces (...).
163 const L
= this.epSquares
.length
;
164 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
166 this.epSquares
[L
- 1].forEach(sq
=> {
167 res
+= V
.CoordsToSquare(sq
) + ",";
169 return res
.slice(0, -1); //remove last comma
172 getEpSquare(moveOrSquare
) {
173 if (!moveOrSquare
) return undefined;
174 if (typeof moveOrSquare
=== "string") {
175 const square
= moveOrSquare
;
176 if (square
== "-") return undefined;
178 square
.split(",").forEach(sq
=> {
179 res
.push(V
.SquareToCoords(sq
));
183 // Argument is a move:
184 const move = moveOrSquare
;
185 const [sx
, sy
, ex
] = [move.start
.x
, move.start
.y
, move.end
.x
];
186 if (this.getPiece(sx
, sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2) {
187 const step
= (ex
- sx
) / Math
.abs(ex
- sx
);
192 if (sx
+ 2 * step
!= ex
) {
201 return undefined; //default
204 getReservePpath(index
, color
) {
205 const p
= V
.RESERVE_PIECES
[index
];
206 const prefix
= (ChessRules
.PIECES
.includes(p
) ? "" : "Pandemonium/");
207 return prefix
+ color
+ p
;;
210 // Ordering on reserve pieces
211 static get RESERVE_PIECES() {
213 [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.CARDINAL
, V
.MARSHAL
]
217 getReserveMoves([x
, y
]) {
218 const color
= this.turn
;
219 const oppCol
= V
.GetOppCol(color
);
220 const p
= V
.RESERVE_PIECES
[y
];
221 if (this.reserve
[color
][p
] == 0) return [];
222 const bounds
= (p
== V
.PAWN
? [1, V
.size
.x
- 1] : [0, V
.size
.x
]);
224 for (let i
= bounds
[0]; i
< bounds
[1]; i
++) {
225 for (let j
= 0; j
< V
.size
.y
; j
++) {
226 if (this.board
[i
][j
] == V
.EMPTY
) {
237 start: { x: x
, y: y
}, //a bit artificial...
241 // Do not drop on checkmate:
244 this.underCheck(oppCol
) && !this.atLeastOneMove("noReserve")
256 static get PromoteMap() {
266 getPotentialMovesFrom([x
, y
]) {
267 const c
= this.getColor(x
, y
);
268 const oppCol
= V
.GetOppCol(c
);
269 if (this.movesCount
<= 1) {
270 if (this.kingPos
[c
][0] == x
&& this.kingPos
[c
][1] == y
) {
271 // Pass (if setup is ok)
276 start: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
277 end: { x: this.kingPos
[oppCol
][0], y: this.kingPos
[oppCol
][1] }
281 const firstRank
= (this.movesCount
== 0 ? 9 : 0);
282 // TODO: initDestFile currently hardcoded for deterministic setup
283 const initDestFile
= new Map([[1, 2], [8, 7]]);
284 // Only option is knight --> bishop swap:
287 !!initDestFile
.get(y
) &&
288 this.getPiece(x
, y
) == V
.KNIGHT
290 const destFile
= initDestFile
.get(y
);
321 start: { x: x
, y: y
},
322 end: { x: x
, y: destFile
}
328 // Normal move (after initial setup)
329 if (x
>= V
.size
.x
) return this.getReserveMoves([x
, y
]);
330 const p
= this.getPiece(x
, y
);
333 if (ChessRules
.PIECES
.includes(p
))
334 moves
= super.getPotentialMovesFrom(sq
);
335 if ([V
.GILDING
, V
.APRICOT
, V
.WHOLE
].includes(p
))
336 moves
= super.getPotentialQueenMoves(sq
);
339 moves
= this.getPotentialScepterMoves(sq
);
342 moves
= this.getPotentialHorseMoves(sq
);
345 moves
= this.getPotentialDragonMoves(sq
);
348 moves
= this.getPotentialCardinalMoves(sq
);
351 moves
= this.getPotentialMarshalMoves(sq
);
354 // Maybe apply promotions:
355 if (Object
.keys(V
.PromoteMap
).includes(p
)) {
356 const promoted
= V
.PromoteMap
[p
];
357 const lastRank
= (c
== 'w' ? 0 : 9);
360 if (m
.start
.x
== lastRank
|| m
.end
.x
== lastRank
) {
361 let pMove
= JSON
.parse(JSON
.stringify(m
));
362 pMove
.appear
[0].p
= promoted
;
363 promotions
.push(pMove
);
366 Array
.prototype.push
.apply(moves
, promotions
);
371 getPotentialPawnMoves([x
, y
]) {
372 const color
= this.turn
;
373 const shiftX
= (color
== 'w' ? -1 : 1);
375 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
376 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
377 if ((color
== 'w' && x
>= V
.size
.x
- 3) || (color
== 'b' && x
<= 2)) {
378 if (this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
) {
379 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
382 (color
== 'w' && x
== V
.size
.x
- 2) ||
383 (color
== 'b' && x
== 1)
386 this.board
[x
+ 3 * shiftX
][y
] == V
.EMPTY
388 moves
.push(this.getBasicMove([x
, y
], [x
+ 3 * shiftX
, y
]));
393 for (let shiftY
of [-1, 1]) {
394 if (y
+ shiftY
>= 0 && y
+ shiftY
< V
.size
.y
) {
396 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
397 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
399 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
403 Array
.prototype.push
.apply(
405 this.getEnpassantCaptures([x
, y
], shiftX
)
410 getPotentialMarshalMoves(sq
) {
411 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
412 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
416 getPotentialCardinalMoves(sq
) {
417 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
418 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
422 getPotentialScepterMoves(sq
) {
424 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.BISHOP
]).concat(V
.steps
[V
.ROOK
]);
425 return this.getSlideNJumpMoves(sq
, steps
, "oneStep");
428 getPotentialHorseMoves(sq
) {
429 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
430 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep"));
433 getPotentialDragonMoves(sq
) {
434 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
435 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep"));
438 getEnpassantCaptures([x
, y
], shiftX
) {
439 const Lep
= this.epSquares
.length
;
440 const epSquare
= this.epSquares
[Lep
- 1];
443 for (let epsq
of epSquare
) {
444 // TODO: some redundant checks
445 if (epsq
.x
== x
+ shiftX
&& Math
.abs(epsq
.y
- y
) == 1) {
446 let enpassantMove
= this.getBasicMove([x
, y
], [epsq
.x
, epsq
.y
]);
447 // WARNING: the captured pawn may be diagonally behind us,
448 // if it's a 3-squares jump and we take on 1st passing square
449 const px
= this.board
[x
][epsq
.y
] != V
.EMPTY
? x : x
- shiftX
;
450 enpassantMove
.vanish
.push({
454 c: this.getColor(px
, epsq
.y
)
456 moves
.push(enpassantMove
);
463 getPotentialKingMoves(sq
) {
464 // Initialize with normal moves
465 let moves
= this.getSlideNJumpMoves(
467 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
472 this.castleFlags
[c
][0] < V
.size
.y
||
473 this.castleFlags
[c
][1] < V
.size
.y
475 const finalSquares
= [
479 moves
= moves
.concat(super.getCastleMoves(sq
, finalSquares
));
484 isAttacked(sq
, color
) {
486 this.isAttackedByPawn(sq
, color
) ||
487 this.isAttackedByRook(sq
, color
) ||
488 this.isAttackedByKnight(sq
, color
) ||
489 this.isAttackedByBishop(sq
, color
) ||
490 this.isAttackedByKing(sq
, color
) ||
491 this.isAttackedByQueens(sq
, color
) ||
492 this.isAttackedByScepter(sq
, color
) ||
493 this.isAttackedByDragon(sq
, color
) ||
494 this.isAttackedByHorse(sq
, color
) ||
495 this.isAttackedByMarshal(sq
, color
) ||
496 this.isAttackedByCardinal(sq
, color
)
500 isAttackedByQueens([x
, y
], color
) {
501 // pieces: because queen = gilding = whole = apricot
502 const pieces
= [V
.QUEEN
, V
.GILDING
, V
.WHOLE
, V
.APRICOT
];
503 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
504 for (let step
of steps
) {
505 let rx
= x
+ step
[0],
507 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
) {
513 this.board
[rx
][ry
] != V
.EMPTY
&&
514 pieces
.includes(this.getPiece(rx
, ry
)) &&
515 this.getColor(rx
, ry
) == color
523 isAttackedByScepter(sq
, color
) {
525 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.BISHOP
]);
527 super.isAttackedBySlideNJump(sq
, color
, V
.SCEPTER
, steps
, "oneStep")
531 isAttackedByHorse(sq
, color
) {
533 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.BISHOP
], V
.HORSE
) ||
534 super.isAttackedBySlideNJump(
535 sq
, color
, V
.HORSE
, V
.steps
[V
.ROOK
], "oneStep")
539 isAttackedByDragon(sq
, color
) {
541 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.ROOK
], V
.DRAGON
) ||
542 super.isAttackedBySlideNJump(
543 sq
, color
, V
.DRAGON
, V
.steps
[V
.BISHOP
], "oneStep")
547 isAttackedByMarshal(sq
, color
) {
549 super.isAttackedBySlideNJump(sq
, color
, V
.MARSHAL
, V
.steps
[V
.ROOK
]) ||
550 super.isAttackedBySlideNJump(
560 isAttackedByCardinal(sq
, color
) {
562 super.isAttackedBySlideNJump(sq
, color
, V
.CARDINAL
, V
.steps
[V
.BISHOP
]) ||
563 super.isAttackedBySlideNJump(
574 let moves
= super.getAllPotentialMoves();
575 if (this.movesCount
>= 2) {
576 const color
= this.turn
;
577 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
578 moves
= moves
.concat(
579 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
583 return this.filterValid(moves
);
586 atLeastOneMove(noReserve
) {
587 if (!super.atLeastOneMove()) {
589 // Search one reserve move
590 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
591 let moves
= this.filterValid(
592 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
594 if (moves
.length
> 0) return true;
602 // Reverse 'PromoteMap'
603 static get P_CORRESPONDANCES() {
614 static MayDecode(piece
) {
615 if (Object
.keys(V
.P_CORRESPONDANCES
).includes(piece
))
616 return V
.P_CORRESPONDANCES
[piece
];
621 move.subTurn
= this.subTurn
; //much easier
622 if (this.movesCount
>= 2 || this.subTurn
== 2 || move.vanish
.length
== 0) {
623 this.turn
= V
.GetOppCol(this.turn
);
627 else this.subTurn
= 2;
628 move.flags
= JSON
.stringify(this.aggregateFlags());
629 this.epSquares
.push(this.getEpSquare(move));
630 V
.PlayOnBoard(this.board
, move);
635 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
636 super.postPlay(move);
637 const color
= move.appear
[0].c
;
638 if (move.vanish
.length
== 0)
639 // Drop unpromoted piece:
640 this.reserve
[color
][move.appear
[0].p
]--;
641 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
642 // May capture a promoted piece:
643 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
647 this.epSquares
.pop();
648 this.disaggregateFlags(JSON
.parse(move.flags
));
649 V
.UndoOnBoard(this.board
, move);
650 if (this.movesCount
>= 2 || this.subTurn
== 1 || move.vanish
.length
== 0) {
651 this.turn
= V
.GetOppCol(this.turn
);
654 this.subTurn
= move.subTurn
;
659 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
660 super.postUndo(move);
661 const color
= move.appear
[0].c
;
662 if (move.vanish
.length
== 0)
663 this.reserve
[color
][move.appear
[0].p
]++;
664 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
665 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
668 static get VALUES() {
669 return Object
.assign(
673 n: 2.5, //knight is weaker
686 static get SEARCH_DEPTH() {
691 if (this.movesCount
<= 1) {
692 // Special case: swap and pass at random
693 const moves1
= this.getAllValidMoves();
694 const m1
= moves1
[randInt(moves1
.length
)];
696 if (m1
.vanish
.length
== 0) {
700 const moves2
= this.getAllValidMoves();
701 const m2
= moves2
[randInt(moves2
.length
)];
705 return super.getComputerMove();
709 let evaluation
= super.evalPosition();
711 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
712 const p
= V
.RESERVE_PIECES
[i
];
713 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
714 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
720 if (move.vanish
.length
== 0) {
721 if (move.appear
.length
== 0) return "pass";
723 (move.appear
[0].p
== V
.PAWN
? "" : move.appear
[0].p
.toUpperCase());
724 return pieceName
+ "@" + V
.CoordsToSquare(move.end
);
726 if (move.appear
.length
== 2) {
727 if (move.appear
[0].p
!= V
.KING
)
728 return V
.CoordsToSquare(move.start
) + "S" + V
.CoordsToSquare(move.end
);
729 return (move.end
.y
< move.start
.y
? "0-0" : "0-0-0");
731 let notation
= super.getNotation(move);
732 if (move.vanish
[0].p
!= V
.PAWN
&& move.appear
[0].p
!= move.vanish
[0].p
)
733 // Add promotion indication:
734 notation
+= "=" + move.appear
[0].p
.toUpperCase();