1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
3 import { ArrayFun
} from "@/utils/array";
5 export class Pandemonium2Rules
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: 8, 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 IsGoodFen(fen
) {
109 if (!ChessRules
.IsGoodFen(fen
)) return false;
110 const fenParsed
= V
.ParseFen(fen
);
112 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
117 static ParseFen(fen
) {
118 const fenParts
= fen
.split(" ");
119 return Object
.assign(
120 ChessRules
.ParseFen(fen
),
121 { reserve: fenParts
[5] }
126 return super.getFen() + " " + this.getReserveFen();
130 return super.getFenForRepeat() + "_" + this.getReserveFen();
134 let counts
= new Array(14);
135 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
136 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
137 counts
[7 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
139 return counts
.join("");
142 static GenRandInitFen(randomness
) {
143 if (randomness
== 0) {
145 "rnbqkmcbnr/pppppppppp/91/91/91/91/PPPPPPPPPP/RNBQKMCBNR " +
146 "w 0 ajaj - 00000000000000"
150 let pieces
= { w: new Array(10), b: new Array(10) };
152 for (let c
of ["w", "b"]) {
153 if (c
== 'b' && randomness
== 1) {
154 pieces
['b'] = pieces
['w'];
159 let positions
= ArrayFun
.range(10);
161 // Get random squares for bishops (different colors)
162 let randIndex
= 2 * randInt(5);
163 let bishop1Pos
= positions
[randIndex
];
164 let randIndex_tmp
= 2 * randInt(5) + 1;
165 let bishop2Pos
= positions
[randIndex_tmp
];
166 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
167 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
169 randIndex
= randInt(8);
170 let knight1Pos
= positions
[randIndex
];
171 positions
.splice(randIndex
, 1);
172 randIndex
= randInt(7);
173 let knight2Pos
= positions
[randIndex
];
174 positions
.splice(randIndex
, 1);
176 randIndex
= randInt(6);
177 let queenPos
= positions
[randIndex
];
178 positions
.splice(randIndex
, 1);
180 // Random squares for cardinal + marshal
181 randIndex
= randInt(5);
182 let cardinalPos
= positions
[randIndex
];
183 positions
.splice(randIndex
, 1);
184 randIndex
= randInt(4);
185 let marshalPos
= positions
[randIndex
];
186 positions
.splice(randIndex
, 1);
188 let rook1Pos
= positions
[0];
189 let kingPos
= positions
[1];
190 let rook2Pos
= positions
[2];
192 pieces
[c
][rook1Pos
] = "r";
193 pieces
[c
][knight1Pos
] = "n";
194 pieces
[c
][bishop1Pos
] = "b";
195 pieces
[c
][queenPos
] = "q";
196 pieces
[c
][kingPos
] = "k";
197 pieces
[c
][marshalPos
] = "m";
198 pieces
[c
][cardinalPos
] = "c";
199 pieces
[c
][bishop2Pos
] = "b";
200 pieces
[c
][knight2Pos
] = "n";
201 pieces
[c
][rook2Pos
] = "r";
202 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
205 pieces
["b"].join("") +
206 "/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/" +
207 pieces
["w"].join("").toUpperCase() +
208 " w 0 " + flags
+ " - 00000000000000"
212 getReservePpath(index
, color
) {
213 const p
= V
.RESERVE_PIECES
[index
];
214 const prefix
= (ChessRules
.PIECES
.includes(p
) ? "" : "Pandemonium/");
215 return prefix
+ color
+ p
;;
218 // Ordering on reserve pieces
219 static get RESERVE_PIECES() {
221 [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.CARDINAL
, V
.MARSHAL
]
225 getReserveMoves([x
, y
]) {
226 const color
= this.turn
;
227 const oppCol
= V
.GetOppCol(color
);
228 const p
= V
.RESERVE_PIECES
[y
];
229 if (this.reserve
[color
][p
] == 0) return [];
230 const bounds
= (p
== V
.PAWN
? [1, V
.size
.x
- 1] : [0, V
.size
.x
]);
232 for (let i
= bounds
[0]; i
< bounds
[1]; i
++) {
233 for (let j
= 0; j
< V
.size
.y
; j
++) {
234 if (this.board
[i
][j
] == V
.EMPTY
) {
245 start: { x: x
, y: y
}, //a bit artificial...
249 // Do not drop on checkmate:
252 this.underCheck(oppCol
) && !this.atLeastOneMove("noReserve")
264 static get PromoteMap() {
274 applyPromotions(moves
, promoted
) {
275 const lastRank
= (this.turn
== 'w' ? 0 : V
.size
.x
- 1);
278 if ([m
.start
.x
, m
.end
.x
].includes(lastRank
)) {
279 let pMove
= JSON
.parse(JSON
.stringify(m
));
280 pMove
.appear
[0].p
= promoted
;
281 promotions
.push(pMove
);
284 Array
.prototype.push
.apply(moves
, promotions
);
287 getPotentialMovesFrom([x
, y
]) {
288 const c
= this.getColor(x
, y
);
289 const oppCol
= V
.GetOppCol(c
);
290 if (this.movesCount
<= 1) {
291 if (this.kingPos
[c
][0] == x
&& this.kingPos
[c
][1] == y
) {
292 // Pass (if setup is ok)
297 start: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
298 end: { x: this.kingPos
[oppCol
][0], y: this.kingPos
[oppCol
][1] }
302 const firstRank
= (this.movesCount
== 0 ? V
.size
.x
- 1 : 0);
303 if (x
!= firstRank
|| this.getPiece(x
, y
) != V
.KNIGHT
) return [];
304 // Swap with who? search for matching bishop:
307 for (let i
= 0; i
< V
.size
.y
; i
++) {
308 const elt
= this.board
[x
][i
][1];
309 if (elt
== 'n') knights
.push(i
);
310 else if (elt
== 'b') bishops
.push(i
);
312 const destFile
= (knights
[0] == y
? bishops
[0] : bishops
[1]);
343 start: { x: x
, y: y
},
344 end: { x: x
, y: destFile
}
348 // Normal move (after initial setup)
349 if (x
>= V
.size
.x
) return this.getReserveMoves([x
, y
]);
350 const p
= this.getPiece(x
, y
);
353 if (ChessRules
.PIECES
.includes(p
))
354 moves
= super.getPotentialMovesFrom(sq
);
355 if ([V
.GILDING
, V
.APRICOT
, V
.WHOLE
].includes(p
))
356 moves
= super.getPotentialQueenMoves(sq
);
359 moves
= this.getPotentialScepterMoves(sq
);
362 moves
= this.getPotentialHorseMoves(sq
);
365 moves
= this.getPotentialDragonMoves(sq
);
368 moves
= this.getPotentialCardinalMoves(sq
);
371 moves
= this.getPotentialMarshalMoves(sq
);
374 // Maybe apply promotions:
375 if (Object
.keys(V
.PromoteMap
).includes(p
))
376 this.applyPromotions(moves
, V
.PromoteMap
[p
]);
380 getPotentialMarshalMoves(sq
) {
381 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
382 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
386 getPotentialCardinalMoves(sq
) {
387 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
388 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
392 getPotentialScepterMoves(sq
) {
394 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.BISHOP
]).concat(V
.steps
[V
.ROOK
]);
395 return this.getSlideNJumpMoves(sq
, steps
, "oneStep");
398 getPotentialHorseMoves(sq
) {
399 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
400 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep"));
403 getPotentialDragonMoves(sq
) {
404 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
405 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep"));
408 getPotentialKingMoves(sq
) {
409 // Initialize with normal moves
410 let moves
= this.getSlideNJumpMoves(
412 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
417 this.castleFlags
[c
][0] < V
.size
.y
||
418 this.castleFlags
[c
][1] < V
.size
.y
420 const finalSquares
= [
424 moves
= moves
.concat(super.getCastleMoves(sq
, finalSquares
));
429 isAttacked(sq
, color
) {
431 this.isAttackedByPawn(sq
, color
) ||
432 this.isAttackedByRook(sq
, color
) ||
433 this.isAttackedByKnight(sq
, color
) ||
434 this.isAttackedByBishop(sq
, color
) ||
435 this.isAttackedByKing(sq
, color
) ||
436 this.isAttackedByQueens(sq
, color
) ||
437 this.isAttackedByScepter(sq
, color
) ||
438 this.isAttackedByDragon(sq
, color
) ||
439 this.isAttackedByHorse(sq
, color
) ||
440 this.isAttackedByMarshal(sq
, color
) ||
441 this.isAttackedByCardinal(sq
, color
)
445 isAttackedByQueens([x
, y
], color
) {
446 // pieces: because queen = gilding = whole = apricot
447 const pieces
= [V
.QUEEN
, V
.GILDING
, V
.WHOLE
, V
.APRICOT
];
448 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
449 for (let step
of steps
) {
450 let rx
= x
+ step
[0],
452 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
) {
458 this.board
[rx
][ry
] != V
.EMPTY
&&
459 pieces
.includes(this.getPiece(rx
, ry
)) &&
460 this.getColor(rx
, ry
) == color
468 isAttackedByScepter(sq
, color
) {
470 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.BISHOP
]);
472 super.isAttackedBySlideNJump(sq
, color
, V
.SCEPTER
, steps
, "oneStep")
476 isAttackedByHorse(sq
, color
) {
478 super.isAttackedBySlideNJump(sq
, color
, V
.HORSE
, V
.steps
[V
.BISHOP
]) ||
479 super.isAttackedBySlideNJump(
480 sq
, color
, V
.HORSE
, V
.steps
[V
.ROOK
], "oneStep")
484 isAttackedByDragon(sq
, color
) {
486 super.isAttackedBySlideNJump(sq
, color
, V
.DRAGON
, V
.steps
[V
.ROOK
]) ||
487 super.isAttackedBySlideNJump(
488 sq
, color
, V
.DRAGON
, V
.steps
[V
.BISHOP
], "oneStep")
492 isAttackedByMarshal(sq
, color
) {
494 super.isAttackedBySlideNJump(sq
, color
, V
.MARSHAL
, V
.steps
[V
.ROOK
]) ||
495 super.isAttackedBySlideNJump(
505 isAttackedByCardinal(sq
, color
) {
507 super.isAttackedBySlideNJump(sq
, color
, V
.CARDINAL
, V
.steps
[V
.BISHOP
]) ||
508 super.isAttackedBySlideNJump(
519 let moves
= super.getAllPotentialMoves();
520 if (this.movesCount
>= 2) {
521 const color
= this.turn
;
522 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
523 moves
= moves
.concat(
524 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
528 return this.filterValid(moves
);
531 atLeastOneMove(noReserve
) {
532 if (!super.atLeastOneMove()) {
534 // Search one reserve move
535 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
536 let moves
= this.filterValid(
537 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
539 if (moves
.length
> 0) return true;
547 // Reverse 'PromoteMap'
548 static get P_CORRESPONDANCES() {
559 static MayDecode(piece
) {
560 if (Object
.keys(V
.P_CORRESPONDANCES
).includes(piece
))
561 return V
.P_CORRESPONDANCES
[piece
];
566 move.subTurn
= this.subTurn
; //much easier
567 if (this.movesCount
>= 2 || this.subTurn
== 2 || move.vanish
.length
== 0) {
568 this.turn
= V
.GetOppCol(this.turn
);
572 else this.subTurn
= 2;
573 move.flags
= JSON
.stringify(this.aggregateFlags());
574 this.epSquares
.push(this.getEpSquare(move));
575 V
.PlayOnBoard(this.board
, move);
580 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
581 super.postPlay(move);
582 const color
= move.appear
[0].c
;
583 if (move.vanish
.length
== 0)
584 // Drop unpromoted piece:
585 this.reserve
[color
][move.appear
[0].p
]--;
586 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
587 // May capture a promoted piece:
588 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
592 this.epSquares
.pop();
593 this.disaggregateFlags(JSON
.parse(move.flags
));
594 V
.UndoOnBoard(this.board
, move);
595 if (this.movesCount
>= 2 || this.subTurn
== 1 || move.vanish
.length
== 0) {
596 this.turn
= V
.GetOppCol(this.turn
);
599 this.subTurn
= move.subTurn
;
604 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return;
605 super.postUndo(move);
606 const color
= move.appear
[0].c
;
607 if (move.vanish
.length
== 0)
608 this.reserve
[color
][move.appear
[0].p
]++;
609 else if (move.vanish
.length
== 2 && move.appear
.length
== 1)
610 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
613 static get VALUES() {
614 return Object
.assign(
618 n: 2.5, //knight is weaker
631 static get SEARCH_DEPTH() {
636 if (this.movesCount
<= 1) {
637 // Special case: swap and pass at random
638 const moves1
= this.getAllValidMoves();
639 const m1
= moves1
[randInt(moves1
.length
)];
641 if (m1
.vanish
.length
== 0) {
645 const moves2
= this.getAllValidMoves();
646 const m2
= moves2
[randInt(moves2
.length
)];
650 return super.getComputerMove();
654 let evaluation
= super.evalPosition();
656 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
657 const p
= V
.RESERVE_PIECES
[i
];
658 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
659 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
665 if (move.vanish
.length
== 0) {
666 if (move.appear
.length
== 0) return "pass";
668 (move.appear
[0].p
== V
.PAWN
? "" : move.appear
[0].p
.toUpperCase());
669 return pieceName
+ "@" + V
.CoordsToSquare(move.end
);
671 if (move.appear
.length
== 2) {
672 if (move.appear
[0].p
!= V
.KING
)
673 return V
.CoordsToSquare(move.start
) + "S" + V
.CoordsToSquare(move.end
);
674 return (move.end
.y
< move.start
.y
? "0-0" : "0-0-0");
676 let notation
= super.getNotation(move);
677 if (move.vanish
[0].p
!= V
.PAWN
&& move.appear
[0].p
!= move.vanish
[0].p
)
678 // Add promotion indication:
679 notation
+= "=" + move.appear
[0].p
.toUpperCase();