1291eda106f234058fbf9650740768f69fea525d
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class PandemoniumRules
extends ChessRules
{
6 static get PawnSpecs() {
12 promotions: [V
.GILDING
]
17 static get GILDING() {
21 static get SCEPTER() {
33 static get CARDINAL() {
41 static get MARSHAL() {
45 static get APRICOT() {
51 ChessRules
.PIECES
.concat([
52 V
.GILDING
, V
.SCEPTER
, V
.HORSE
, V
.DRAGON
,
53 V
.CARDINAL
, V
.WHOLE
, V
.MARSHAL
, V
.APRICOT
])
58 const prefix
= (ChessRules
.PIECES
.includes(b
[1]) ? "" : "Pandemonium/");
63 return { x: 10, y: 10};
67 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
68 return this.board
[i
][j
].charAt(0);
72 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
73 return this.board
[i
][j
].charAt(1);
76 setOtherVariables(fen
) {
77 super.setOtherVariables(fen
);
78 // Sub-turn is useful only at first move...
80 // Also init reserves (used by the interface to show landable pieces)
82 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
87 [V
.KNIGHT
]: reserve
[2],
88 [V
.BISHOP
]: reserve
[3],
89 [V
.QUEEN
]: reserve
[4],
90 [V
.CARDINAL
]: reserve
[5],
91 [V
.MARSHAL
]: reserve
[6],
96 [V
.KNIGHT
]: reserve
[9],
97 [V
.BISHOP
]: reserve
[10],
98 [V
.QUEEN
]: reserve
[11],
99 [V
.CARDINAL
]: reserve
[12],
100 [V
.MARSHAL
]: reserve
[13]
105 static IsGoodEnpassant(enpassant
) {
106 if (enpassant
!= "-") {
107 const squares
= enpassant
.split(",");
108 if (squares
.length
> 2) return false;
109 for (let sq
of squares
) {
110 if (!sq
.match(/[a-j0-9]/)) return false;
116 static IsGoodFen(fen
) {
117 if (!ChessRules
.IsGoodFen(fen
)) return false;
118 const fenParsed
= V
.ParseFen(fen
);
120 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
125 static ParseFen(fen
) {
126 const fenParts
= fen
.split(" ");
127 return Object
.assign(
128 ChessRules
.ParseFen(fen
),
129 { reserve: fenParts
[5] }
134 return super.getFen() + " " + this.getReserveFen();
138 return super.getFenForRepeat() + "_" + this.getReserveFen();
142 let counts
= new Array(14);
143 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
144 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
145 counts
[7 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
147 return counts
.join("");
150 static GenRandInitFen(randomness
) {
151 // No randomization here for now (but initial setup choice)
153 "rnbqkmcbnr/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/RNBQKMCBNR " +
154 "w 0 ajaj - 00000000000000"
156 // TODO later: randomization too --> 2 bishops, not next to each other.
157 // then knights next to bishops. Then other pieces (...).
161 const L
= this.epSquares
.length
;
162 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
164 this.epSquares
[L
- 1].forEach(sq
=> {
165 res
+= V
.CoordsToSquare(sq
) + ",";
167 return res
.slice(0, -1); //remove last comma
170 getEpSquare(moveOrSquare
) {
171 if (!moveOrSquare
) return undefined;
172 if (typeof moveOrSquare
=== "string") {
173 const square
= moveOrSquare
;
174 if (square
== "-") return undefined;
176 square
.split(",").forEach(sq
=> {
177 res
.push(V
.SquareToCoords(sq
));
181 // Argument is a move:
182 const move = moveOrSquare
;
183 const [sx
, sy
, ex
] = [move.start
.x
, move.start
.y
, move.end
.x
];
184 if (this.getPiece(sx
, sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2) {
185 const step
= (ex
- sx
) / Math
.abs(ex
- sx
);
190 if (sx
+ 2 * step
!= ex
) {
199 return undefined; //default
202 getReservePpath(index
, color
) {
203 const p
= V
.RESERVE_PIECES
[index
];
204 const prefix
= (ChessRules
.PIECES
.includes(p
) ? "" : "Pandemonium/");
205 return prefix
+ color
+ p
;;
208 // Ordering on reserve pieces
209 static get RESERVE_PIECES() {
211 [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.CARDINAL
, V
.MARSHAL
]
215 getReserveMoves([x
, y
]) {
216 const color
= this.turn
;
217 const oppCol
= V
.GetOppCol(color
);
218 const p
= V
.RESERVE_PIECES
[y
];
219 if (this.reserve
[color
][p
] == 0) return [];
220 const bounds
= (p
== V
.PAWN
? [1, V
.size
.x
- 1] : [0, V
.size
.x
]);
222 for (let i
= bounds
[0]; i
< bounds
[1]; i
++) {
223 for (let j
= 0; j
< V
.size
.y
; j
++) {
224 if (this.board
[i
][j
] == V
.EMPTY
) {
235 start: { x: x
, y: y
}, //a bit artificial...
239 // Do not drop on checkmate:
242 this.underCheck(oppCol
) && !this.atLeastOneMove("noReserve")
254 static get PromoteMap() {
264 getPotentialMovesFrom([x
, y
]) {
265 const c
= this.getColor(x
, y
);
266 const oppCol
= V
.GetOppCol(c
);
267 if (this.movesCount
<= 1) {
268 if (this.kingPos
[c
][0] == x
&& this.kingPos
[c
][1] == y
) {
269 // Pass (if setup is ok)
274 start: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
275 end: { x: this.kingPos
[oppCol
][0], y: this.kingPos
[oppCol
][1] }
279 const firstRank
= (this.movesCount
== 0 ? 9 : 0);
280 // TODO: initDestFile currently hardcoded for deterministic setup
281 const initDestFile
= new Map([[1, 2], [8, 7]]);
282 // Only option is knight --> bishop swap:
285 !!initDestFile
.get(y
) &&
286 this.getPiece(x
, y
) == V
.KNIGHT
288 const destFile
= initDestFile
.get(y
);
319 start: { x: x
, y: y
},
320 end: { x: x
, y: destFile
}
326 // Normal move (after initial setup)
327 if (x
>= V
.size
.x
) return this.getReserveMoves(x
, y
);
328 const p
= this.getPiece(x
, y
);
331 if (ChessRules
.PIECES
.includes(p
))
332 moves
= super.getPotentialMovesFrom(sq
);
333 if ([V
.GILDING
, V
.APRICOT
, V
.WHOLE
].includes(p
))
334 moves
= super.getPotentialQueenMoves(sq
);
337 moves
= this.getPotentialScepterMoves(sq
);
340 moves
= this.getPotentialHorseMoves(sq
);
343 moves
= this.getPotentialDragonMoves(sq
);
346 moves
= this.getPotentialCardinalMoves(sq
);
349 moves
= this.getPotentialMarshalMoves(sq
);
352 // Maybe apply promotions:
353 if (Object
.keys(V
.PromoteMap
).includes(p
)) {
354 const promoted
= V
.PromoteMap
[p
];
355 const lastRank
= (c
== 'w' ? 0 : 9);
358 if (m
.start
.x
== lastRank
|| m
.end
.x
== lastRank
) {
359 let pMove
= JSON
.parse(JSON
.stringify(m
));
360 pMove
.appear
[0].p
= promoted
;
361 promotions
.push(pMove
);
364 Array
.prototype.push
.apply(moves
, promotions
);
369 getPotentialPawnMoves([x
, y
]) {
370 const color
= this.turn
;
371 const shiftX
= V
.PawnSpecs
.directions
[color
];
373 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
374 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
375 if ((color
== 'w' && x
>= V
.size
.x
- 3) || (color
== 'b' && x
<= 3)) {
376 if (this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
) {
377 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
380 (color
== 'w' && x
>= V
.size
.x
- 2) ||
381 (color
== 'b' && x
<= 2)
384 this.board
[x
+ 3 * shiftX
][y
] == V
.EMPTY
386 moves
.push(this.getBasicMove([x
, y
], [x
+ 3 * shiftX
, y
]));
391 for (let shiftY
of [-1, 1]) {
392 if (y
+ shiftY
>= 0 && y
+ shiftY
< V
.size
.y
) {
394 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
395 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
397 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
401 Array
.prototype.push
.apply(
403 this.getEnpassantCaptures([x
, y
], shiftX
)
408 getPotentialMarshalMoves(sq
) {
409 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
410 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
414 getPotentialCardinalMoves(sq
) {
415 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
416 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
420 getPotentialScepterMoves(sq
) {
422 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.BISHOP
]).concat(V
.steps
[V
.ROOK
]);
423 return this.getSlideNJumpMoves(sq
, steps
, "oneStep");
426 getPotentialHorseMoves(sq
) {
427 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
428 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep"));
431 getPotentialDragonMoves(sq
) {
432 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
433 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep"));
436 getEnpassantCaptures([x
, y
], shiftX
) {
437 const Lep
= this.epSquares
.length
;
438 const epSquare
= this.epSquares
[Lep
- 1];
441 for (let epsq
of epSquare
) {
442 // TODO: some redundant checks
443 if (epsq
.x
== x
+ shiftX
&& Math
.abs(epsq
.y
- y
) == 1) {
444 let enpassantMove
= this.getBasicMove([x
, y
], [epsq
.x
, epsq
.y
]);
445 // WARNING: the captured pawn may be diagonally behind us,
446 // if it's a 3-squares jump and we take on 1st passing square
447 const px
= this.board
[x
][epsq
.y
] != V
.EMPTY
? x : x
- shiftX
;
448 enpassantMove
.vanish
.push({
452 c: this.getColor(px
, epsq
.y
)
454 moves
.push(enpassantMove
);
461 getPotentialKingMoves(sq
) {
462 // Initialize with normal moves
463 let moves
= this.getSlideNJumpMoves(
465 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
470 this.castleFlags
[c
][0] < V
.size
.y
||
471 this.castleFlags
[c
][1] < V
.size
.y
473 const finalSquares
= [
477 moves
= moves
.concat(super.getCastleMoves(sq
, finalSquares
));
482 isAttacked(sq
, color
) {
484 this.isAttackedByPawn(sq
, color
) ||
485 this.isAttackedByRook(sq
, color
) ||
486 this.isAttackedByKnight(sq
, color
) ||
487 this.isAttackedByBishop(sq
, color
) ||
488 this.isAttackedByKing(sq
, color
) ||
489 this.isAttackedByQueens(sq
, color
) ||
490 this.isAttackedByScepter(sq
, color
) ||
491 this.isAttackedByDragon(sq
, color
) ||
492 this.isAttackedByHorse(sq
, color
) ||
493 this.isAttackedByMarshal(sq
, color
) ||
494 this.isAttackedByCardinal(sq
, color
)
498 isAttackedByQueens([x
, y
], color
) {
499 // pieces: because queen = gilding = whole = apricot
500 const pieces
= [V
.QUEEN
, V
.GILDING
, V
.WHOLE
, V
.APRICOT
];
501 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
502 for (let step
of steps
) {
503 let rx
= x
+ step
[0],
505 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
) {
511 this.board
[rx
][ry
] != V
.EMPTY
&&
512 pieces
.includes(this.getPiece(rx
, ry
)) &&
513 this.getColor(rx
, ry
) == color
521 isAttackedByScepter(sq
, color
) {
523 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.BISHOP
]);
525 super.isAttackedBySlideNJump(sq
, color
, V
.SCEPTER
, steps
, "oneStep")
529 isAttackedByHorse(sq
, color
) {
531 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.BISHOP
], V
.HORSE
) ||
532 super.isAttackedBySlideNJump(
533 sq
, color
, V
.HORSE
, V
.steps
[V
.ROOK
], "oneStep")
537 isAttackedByDragon(sq
, color
) {
539 super.isAttackedBySlideNJump(sq
, color
, V
.steps
[V
.ROOK
], V
.DRAGON
) ||
540 super.isAttackedBySlideNJump(
541 sq
, color
, V
.DRAGON
, V
.steps
[V
.BISHOP
], "oneStep")
545 isAttackedByMarshal(sq
, color
) {
547 super.isAttackedBySlideNJump(sq
, color
, V
.MARSHAL
, V
.steps
[V
.ROOK
]) ||
548 super.isAttackedBySlideNJump(
558 isAttackedByCardinal(sq
, color
) {
560 super.isAttackedBySlideNJump(sq
, color
, V
.CARDINAL
, V
.steps
[V
.BISHOP
]) ||
561 super.isAttackedBySlideNJump(
572 let moves
= super.getAllPotentialMoves();
573 if (this.movesCount
>= 2) {
574 const color
= this.turn
;
575 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
576 moves
= moves
.concat(
577 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
581 return this.filterValid(moves
);
584 atLeastOneMove(noReserve
) {
585 if (!super.atLeastOneMove()) {
587 // Search one reserve move
588 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
589 let moves
= this.filterValid(
590 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
592 if (moves
.length
> 0) return true;
600 // Reverse 'PromoteMap'
601 static get P_CORRESPONDANCES() {
611 static MayDecode(piece
) {
612 if (Object
.keys(V
.P_CORRESPONDANCES
).includes(piece
))
613 return V
.P_CORRESPONDANCES
[piece
];
618 move.subTurn
= this.subTurn
; //much easier
619 if (this.movesCount
>= 2 || this.subTurn
== 2 || move.vanish
.length
== 0) {
620 this.turn
= V
.GetOppCol(this.turn
);
624 else this.subTurn
= 2;
625 move.flags
= JSON
.stringify(this.aggregateFlags());
626 this.epSquares
.push(this.getEpSquare(move));
627 V
.PlayOnBoard(this.board
, move);
632 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
633 super.postPlay(move);
634 const color
= move.appear
[0].c
;
635 if (move.vanish
.length
== 0)
636 // Drop unpromoted piece:
637 this.reserve
[color
][move.appear
[0].p
]--;
638 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
639 // May capture a promoted piece:
640 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
644 this.epSquares
.pop();
645 this.disaggregateFlags(JSON
.parse(move.flags
));
646 V
.UndoOnBoard(this.board
, move);
647 if (this.movesCount
>= 2 || this.subTurn
== 1 || move.vanish
.length
== 0) {
648 this.turn
= V
.GetOppCol(this.turn
);
651 this.subTurn
= move.subTurn
;
656 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
657 super.postUndo(move);
658 const color
= move.appear
[0].c
;
659 if (move.vanish
.length
== 0)
660 this.reserve
[color
][move.appear
[0].p
]++;
661 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
662 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
665 static get VALUES() {
666 return Object
.assign(
670 n: 2.5, //knight is weaker
683 static get SEARCH_DEPTH() {
688 if (this.movesCount
<= 1) {
689 // Special case: swap and pass at random
690 const moves1
= this.getAllValidMoves();
691 const m1
= moves1
[randInt(moves1
.length
)];
693 if (m1
.vanish
.length
== 0) {
697 const moves2
= this.getAllValidMoves();
698 const m2
= moves2
[randInt(moves2
.length
)];
702 return super.getComputerMove();
706 let evaluation
= super.evalPosition();
708 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
709 const p
= V
.RESERVE_PIECES
[i
];
710 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
711 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
717 if (move.vanish
.length
== 0) {
718 if (move.appear
.length
== 0) return "pass";
720 (move.appear
[0].p
== V
.PAWN
? "" : move.appear
[0].p
.toUpperCase());
721 return pieceName
+ "@" + V
.CoordsToSquare(move.end
);
723 if (move.appear
.length
== 2) {
724 if (move.appear
[0].p
!= V
.KING
)
725 return V
.CoordsToSquare(move.start
) + "S" + V
.CoordsToSquare(move.end
);
726 return (move.end
.y
< move.start
.y
? "0-0" : "0-0-0");
728 let notation
= super.getNotation(move);
729 if (move.vanish
[0].p
!= V
.PAWN
&& move.appear
[0].p
!= move.vanish
[0].p
)
730 // Add promotion indication:
731 notation
+= "=" + move.appear
[0].p
.toUpperCase();