1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
3 import { ArrayFun
} from "@/utils/array";
5 export class PandemoniumRules
extends ChessRules
{
7 static get PawnSpecs() {
11 { promotions: [V
.GILDING
] }
16 // If current side is under check: lost
17 return this.underCheck(this.turn
);
20 static get GILDING() {
24 static get SCEPTER() {
36 static get CARDINAL() {
44 static get MARSHAL() {
48 static get APRICOT() {
54 ChessRules
.PIECES
.concat([
55 V
.GILDING
, V
.SCEPTER
, V
.HORSE
, V
.DRAGON
,
56 V
.CARDINAL
, V
.WHOLE
, V
.MARSHAL
, V
.APRICOT
])
61 const prefix
= (ChessRules
.PIECES
.includes(b
[1]) ? "" : "Pandemonium/");
66 return { x: 10, y: 10};
70 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
71 return this.board
[i
][j
].charAt(0);
75 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
76 return this.board
[i
][j
].charAt(1);
79 setOtherVariables(fen
) {
80 super.setOtherVariables(fen
);
81 // Sub-turn is useful only at first move...
83 // Also init reserves (used by the interface to show landable pieces)
85 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
90 [V
.KNIGHT
]: reserve
[2],
91 [V
.BISHOP
]: reserve
[3],
92 [V
.QUEEN
]: reserve
[4],
93 [V
.CARDINAL
]: reserve
[5],
94 [V
.MARSHAL
]: reserve
[6],
99 [V
.KNIGHT
]: reserve
[9],
100 [V
.BISHOP
]: reserve
[10],
101 [V
.QUEEN
]: reserve
[11],
102 [V
.CARDINAL
]: reserve
[12],
103 [V
.MARSHAL
]: reserve
[13]
108 static IsGoodEnpassant(enpassant
) {
109 if (enpassant
!= "-") {
110 const squares
= enpassant
.split(",");
111 if (squares
.length
> 2) return false;
112 for (let sq
of squares
) {
113 if (!sq
.match(/[a-j0-9]/)) return false;
119 static IsGoodFen(fen
) {
120 if (!ChessRules
.IsGoodFen(fen
)) return false;
121 const fenParsed
= V
.ParseFen(fen
);
123 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
128 static ParseFen(fen
) {
129 const fenParts
= fen
.split(" ");
130 return Object
.assign(
131 ChessRules
.ParseFen(fen
),
132 { reserve: fenParts
[5] }
137 return super.getFen() + " " + this.getReserveFen();
141 return super.getFenForRepeat() + "_" + this.getReserveFen();
145 let counts
= new Array(14);
146 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
147 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
148 counts
[7 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
150 return counts
.join("");
153 static GenRandInitFen(randomness
) {
154 if (randomness
== 0) {
156 "rnbqkmcbnr/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/RNBQKMCBNR " +
157 "w 0 ajaj - 00000000000000"
161 let pieces
= { w: new Array(10), b: new Array(10) };
163 for (let c
of ["w", "b"]) {
164 if (c
== 'b' && randomness
== 1) {
165 pieces
['b'] = pieces
['w'];
170 let positions
= ArrayFun
.range(10);
172 // Get random squares for bishops (different colors)
173 let randIndex
= 2 * randInt(5);
174 let bishop1Pos
= positions
[randIndex
];
175 let randIndex_tmp
= 2 * randInt(5) + 1;
176 let bishop2Pos
= positions
[randIndex_tmp
];
177 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
178 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
180 randIndex
= randInt(8);
181 let knight1Pos
= positions
[randIndex
];
182 positions
.splice(randIndex
, 1);
183 randIndex
= randInt(7);
184 let knight2Pos
= positions
[randIndex
];
185 positions
.splice(randIndex
, 1);
187 randIndex
= randInt(6);
188 let queenPos
= positions
[randIndex
];
189 positions
.splice(randIndex
, 1);
191 // Random squares for cardinal + marshal
192 randIndex
= randInt(5);
193 let cardinalPos
= positions
[randIndex
];
194 positions
.splice(randIndex
, 1);
195 randIndex
= randInt(4);
196 let marshalPos
= positions
[randIndex
];
197 positions
.splice(randIndex
, 1);
199 let rook1Pos
= positions
[0];
200 let kingPos
= positions
[1];
201 let rook2Pos
= positions
[2];
203 pieces
[c
][rook1Pos
] = "r";
204 pieces
[c
][knight1Pos
] = "n";
205 pieces
[c
][bishop1Pos
] = "b";
206 pieces
[c
][queenPos
] = "q";
207 pieces
[c
][kingPos
] = "k";
208 pieces
[c
][marshalPos
] = "m";
209 pieces
[c
][cardinalPos
] = "c";
210 pieces
[c
][bishop2Pos
] = "b";
211 pieces
[c
][knight2Pos
] = "n";
212 pieces
[c
][rook2Pos
] = "r";
213 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
216 pieces
["b"].join("") +
217 "/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/" +
218 pieces
["w"].join("").toUpperCase() +
219 " w 0 " + flags
+ " -"
224 const L
= this.epSquares
.length
;
225 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
227 this.epSquares
[L
- 1].forEach(sq
=> {
228 res
+= V
.CoordsToSquare(sq
) + ",";
230 return res
.slice(0, -1); //remove last comma
233 getEpSquare(moveOrSquare
) {
234 if (!moveOrSquare
) return undefined;
235 if (typeof moveOrSquare
=== "string") {
236 const square
= moveOrSquare
;
237 if (square
== "-") return undefined;
239 square
.split(",").forEach(sq
=> {
240 res
.push(V
.SquareToCoords(sq
));
244 // Argument is a move:
245 const move = moveOrSquare
;
246 const [sx
, sy
, ex
] = [move.start
.x
, move.start
.y
, move.end
.x
];
247 if (this.getPiece(sx
, sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2) {
248 const step
= (ex
- sx
) / Math
.abs(ex
- sx
);
253 if (sx
+ 2 * step
!= ex
) {
262 return undefined; //default
265 getReservePpath(index
, color
) {
266 const p
= V
.RESERVE_PIECES
[index
];
267 const prefix
= (ChessRules
.PIECES
.includes(p
) ? "" : "Pandemonium/");
268 return prefix
+ color
+ p
;;
271 // Ordering on reserve pieces
272 static get RESERVE_PIECES() {
274 [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.CARDINAL
, V
.MARSHAL
]
278 getReserveMoves([x
, y
]) {
279 const color
= this.turn
;
280 const oppCol
= V
.GetOppCol(color
);
281 const p
= V
.RESERVE_PIECES
[y
];
282 if (this.reserve
[color
][p
] == 0) return [];
283 const bounds
= (p
== V
.PAWN
? [1, V
.size
.x
- 1] : [0, V
.size
.x
]);
285 for (let i
= bounds
[0]; i
< bounds
[1]; i
++) {
286 for (let j
= 0; j
< V
.size
.y
; j
++) {
287 if (this.board
[i
][j
] == V
.EMPTY
) {
298 start: { x: x
, y: y
}, //a bit artificial...
302 // Do not drop on checkmate:
305 this.underCheck(oppCol
) && !this.atLeastOneMove("noReserve")
317 static get PromoteMap() {
327 getPotentialMovesFrom([x
, y
]) {
328 const c
= this.getColor(x
, y
);
329 const oppCol
= V
.GetOppCol(c
);
330 if (this.movesCount
<= 1) {
331 if (this.kingPos
[c
][0] == x
&& this.kingPos
[c
][1] == y
) {
332 // Pass (if setup is ok)
337 start: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
338 end: { x: this.kingPos
[oppCol
][0], y: this.kingPos
[oppCol
][1] }
342 const firstRank
= (this.movesCount
== 0 ? 9 : 0);
343 if (x
!= firstRank
|| this.getPiece(x
, y
) != V
.KNIGHT
) return [];
344 // Swap with who? search for matching bishop:
347 for (let i
= 0; i
< 10; i
++) {
348 const elt
= this.board
[x
][i
][1];
349 if (elt
== 'n') knights
.push(i
);
350 else if (elt
== 'b') bishops
.push(i
);
352 const destFile
= (knights
[0] == y
? bishops
[0] : bishops
[1]);
383 start: { x: x
, y: y
},
384 end: { x: x
, y: destFile
}
388 // Normal move (after initial setup)
389 if (x
>= V
.size
.x
) return this.getReserveMoves([x
, y
]);
390 const p
= this.getPiece(x
, y
);
393 if (ChessRules
.PIECES
.includes(p
))
394 moves
= super.getPotentialMovesFrom(sq
);
395 if ([V
.GILDING
, V
.APRICOT
, V
.WHOLE
].includes(p
))
396 moves
= super.getPotentialQueenMoves(sq
);
399 moves
= this.getPotentialScepterMoves(sq
);
402 moves
= this.getPotentialHorseMoves(sq
);
405 moves
= this.getPotentialDragonMoves(sq
);
408 moves
= this.getPotentialCardinalMoves(sq
);
411 moves
= this.getPotentialMarshalMoves(sq
);
414 // Maybe apply promotions:
415 if (Object
.keys(V
.PromoteMap
).includes(p
)) {
416 const promoted
= V
.PromoteMap
[p
];
417 const lastRank
= (c
== 'w' ? 0 : 9);
420 if (m
.start
.x
== lastRank
|| m
.end
.x
== lastRank
) {
421 let pMove
= JSON
.parse(JSON
.stringify(m
));
422 pMove
.appear
[0].p
= promoted
;
423 promotions
.push(pMove
);
426 Array
.prototype.push
.apply(moves
, promotions
);
431 getPotentialPawnMoves([x
, y
]) {
432 const color
= this.turn
;
433 const shiftX
= (color
== 'w' ? -1 : 1);
435 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
436 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
437 if ((color
== 'w' && x
>= V
.size
.x
- 3) || (color
== 'b' && x
<= 2)) {
438 if (this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
) {
439 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
442 (color
== 'w' && x
== V
.size
.x
- 2) ||
443 (color
== 'b' && x
== 1)
446 this.board
[x
+ 3 * shiftX
][y
] == V
.EMPTY
448 moves
.push(this.getBasicMove([x
, y
], [x
+ 3 * shiftX
, y
]));
453 for (let shiftY
of [-1, 1]) {
454 if (y
+ shiftY
>= 0 && y
+ shiftY
< V
.size
.y
) {
456 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
457 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
459 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
463 Array
.prototype.push
.apply(
465 this.getEnpassantCaptures([x
, y
], shiftX
)
470 getPotentialMarshalMoves(sq
) {
471 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
472 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
476 getPotentialCardinalMoves(sq
) {
477 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
478 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
482 getPotentialScepterMoves(sq
) {
484 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.BISHOP
]).concat(V
.steps
[V
.ROOK
]);
485 return this.getSlideNJumpMoves(sq
, steps
, "oneStep");
488 getPotentialHorseMoves(sq
) {
489 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
490 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep"));
493 getPotentialDragonMoves(sq
) {
494 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
495 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep"));
498 getEnpassantCaptures([x
, y
], shiftX
) {
499 const Lep
= this.epSquares
.length
;
500 const epSquare
= this.epSquares
[Lep
- 1];
503 for (let epsq
of epSquare
) {
504 // TODO: some redundant checks
505 if (epsq
.x
== x
+ shiftX
&& Math
.abs(epsq
.y
- y
) == 1) {
506 let enpassantMove
= this.getBasicMove([x
, y
], [epsq
.x
, epsq
.y
]);
507 // WARNING: the captured pawn may be diagonally behind us,
508 // if it's a 3-squares jump and we take on 1st passing square
509 const px
= this.board
[x
][epsq
.y
] != V
.EMPTY
? x : x
- shiftX
;
510 enpassantMove
.vanish
.push({
514 c: this.getColor(px
, epsq
.y
)
516 moves
.push(enpassantMove
);
523 getPotentialKingMoves(sq
) {
524 // Initialize with normal moves
525 let moves
= this.getSlideNJumpMoves(
527 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
532 this.castleFlags
[c
][0] < V
.size
.y
||
533 this.castleFlags
[c
][1] < V
.size
.y
535 const finalSquares
= [
539 moves
= moves
.concat(super.getCastleMoves(sq
, finalSquares
));
544 isAttacked(sq
, color
) {
546 this.isAttackedByPawn(sq
, color
) ||
547 this.isAttackedByRook(sq
, color
) ||
548 this.isAttackedByKnight(sq
, color
) ||
549 this.isAttackedByBishop(sq
, color
) ||
550 this.isAttackedByKing(sq
, color
) ||
551 this.isAttackedByQueens(sq
, color
) ||
552 this.isAttackedByScepter(sq
, color
) ||
553 this.isAttackedByDragon(sq
, color
) ||
554 this.isAttackedByHorse(sq
, color
) ||
555 this.isAttackedByMarshal(sq
, color
) ||
556 this.isAttackedByCardinal(sq
, color
)
560 isAttackedByQueens([x
, y
], color
) {
561 // pieces: because queen = gilding = whole = apricot
562 const pieces
= [V
.QUEEN
, V
.GILDING
, V
.WHOLE
, V
.APRICOT
];
563 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
564 for (let step
of steps
) {
565 let rx
= x
+ step
[0],
567 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
) {
573 this.board
[rx
][ry
] != V
.EMPTY
&&
574 pieces
.includes(this.getPiece(rx
, ry
)) &&
575 this.getColor(rx
, ry
) == color
583 isAttackedByScepter(sq
, color
) {
585 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.BISHOP
]);
587 super.isAttackedBySlideNJump(sq
, color
, V
.SCEPTER
, steps
, "oneStep")
591 isAttackedByHorse(sq
, color
) {
593 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.BISHOP
], V
.HORSE
) ||
594 super.isAttackedBySlideNJump(
595 sq
, color
, V
.HORSE
, V
.steps
[V
.ROOK
], "oneStep")
599 isAttackedByDragon(sq
, color
) {
601 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.ROOK
], V
.DRAGON
) ||
602 super.isAttackedBySlideNJump(
603 sq
, color
, V
.DRAGON
, V
.steps
[V
.BISHOP
], "oneStep")
607 isAttackedByMarshal(sq
, color
) {
609 super.isAttackedBySlideNJump(sq
, color
, V
.MARSHAL
, V
.steps
[V
.ROOK
]) ||
610 super.isAttackedBySlideNJump(
620 isAttackedByCardinal(sq
, color
) {
622 super.isAttackedBySlideNJump(sq
, color
, V
.CARDINAL
, V
.steps
[V
.BISHOP
]) ||
623 super.isAttackedBySlideNJump(
634 let moves
= super.getAllPotentialMoves();
635 if (this.movesCount
>= 2) {
636 const color
= this.turn
;
637 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
638 moves
= moves
.concat(
639 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
643 return this.filterValid(moves
);
646 atLeastOneMove(noReserve
) {
647 if (!super.atLeastOneMove()) {
649 // Search one reserve move
650 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
651 let moves
= this.filterValid(
652 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
654 if (moves
.length
> 0) return true;
662 // Reverse 'PromoteMap'
663 static get P_CORRESPONDANCES() {
674 static MayDecode(piece
) {
675 if (Object
.keys(V
.P_CORRESPONDANCES
).includes(piece
))
676 return V
.P_CORRESPONDANCES
[piece
];
681 move.subTurn
= this.subTurn
; //much easier
682 if (this.movesCount
>= 2 || this.subTurn
== 2 || move.vanish
.length
== 0) {
683 this.turn
= V
.GetOppCol(this.turn
);
687 else this.subTurn
= 2;
688 move.flags
= JSON
.stringify(this.aggregateFlags());
689 this.epSquares
.push(this.getEpSquare(move));
690 V
.PlayOnBoard(this.board
, move);
695 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
696 super.postPlay(move);
697 const color
= move.appear
[0].c
;
698 if (move.vanish
.length
== 0)
699 // Drop unpromoted piece:
700 this.reserve
[color
][move.appear
[0].p
]--;
701 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
702 // May capture a promoted piece:
703 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
707 this.epSquares
.pop();
708 this.disaggregateFlags(JSON
.parse(move.flags
));
709 V
.UndoOnBoard(this.board
, move);
710 if (this.movesCount
>= 2 || this.subTurn
== 1 || move.vanish
.length
== 0) {
711 this.turn
= V
.GetOppCol(this.turn
);
714 this.subTurn
= move.subTurn
;
719 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
720 super.postUndo(move);
721 const color
= move.appear
[0].c
;
722 if (move.vanish
.length
== 0)
723 this.reserve
[color
][move.appear
[0].p
]++;
724 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
725 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
728 static get VALUES() {
729 return Object
.assign(
733 n: 2.5, //knight is weaker
746 static get SEARCH_DEPTH() {
751 if (this.movesCount
<= 1) {
752 // Special case: swap and pass at random
753 const moves1
= this.getAllValidMoves();
754 const m1
= moves1
[randInt(moves1
.length
)];
756 if (m1
.vanish
.length
== 0) {
760 const moves2
= this.getAllValidMoves();
761 const m2
= moves2
[randInt(moves2
.length
)];
765 return super.getComputerMove();
769 let evaluation
= super.evalPosition();
771 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
772 const p
= V
.RESERVE_PIECES
[i
];
773 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
774 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
780 if (move.vanish
.length
== 0) {
781 if (move.appear
.length
== 0) return "pass";
783 (move.appear
[0].p
== V
.PAWN
? "" : move.appear
[0].p
.toUpperCase());
784 return pieceName
+ "@" + V
.CoordsToSquare(move.end
);
786 if (move.appear
.length
== 2) {
787 if (move.appear
[0].p
!= V
.KING
)
788 return V
.CoordsToSquare(move.start
) + "S" + V
.CoordsToSquare(move.end
);
789 return (move.end
.y
< move.start
.y
? "0-0" : "0-0-0");
791 let notation
= super.getNotation(move);
792 if (move.vanish
[0].p
!= V
.PAWN
&& move.appear
[0].p
!= move.vanish
[0].p
)
793 // Add promotion indication:
794 notation
+= "=" + move.appear
[0].p
.toUpperCase();