1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class RococoRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
15 return ChessRules
.PIECES
.concat([V
.IMMOBILIZER
]);
20 //'m' for Immobilizer (I is too similar to 1)
22 return b
; //usual piece
26 // The only "choice" case is between a swap and a mutual destruction:
27 // show empty square in case of mutual destruction.
28 if (m
.appear
.length
== 0) return "Rococo/empty";
29 return m
.appear
[0].c
+ m
.appear
[0].p
;
32 setOtherVariables(fen
) {
33 // No castling, but checks, so keep track of kings
34 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
35 const fenParts
= fen
.split(" ");
36 const position
= fenParts
[0].split("/");
37 for (let i
= 0; i
< position
.length
; i
++) {
39 for (let j
= 0; j
< position
[i
].length
; j
++) {
40 switch (position
[i
].charAt(j
)) {
42 this.kingPos
["b"] = [i
, k
];
45 this.kingPos
["w"] = [i
, k
];
48 const num
= parseInt(position
[i
].charAt(j
));
49 if (!isNaN(num
)) k
+= num
- 1;
55 // Local stack of swaps:
57 const smove
= V
.ParseFen(fen
).smove
;
58 if (smove
== "-") this.smoves
.push(null);
61 start: ChessRules
.SquareToCoords(smove
.substr(0, 2)),
62 end: ChessRules
.SquareToCoords(smove
.substr(2))
67 static ParseFen(fen
) {
69 ChessRules
.ParseFen(fen
),
70 { smove: fen
.split(" ")[3] }
74 static IsGoodFen(fen
) {
75 if (!ChessRules
.IsGoodFen(fen
)) return false;
76 const fenParts
= fen
.split(" ");
77 if (fenParts
.length
!= 4) return false;
78 if (fenParts
[3] != "-" && !fenParts
[3].match(/^([a-h][1-8]){2}$/))
84 if (move.appear
.length
== 2)
85 return { start: move.start
, end: move.end
};
90 // Add the "capturing edge"
91 return { x: 10, y: 10 };
94 static get IMMOBILIZER() {
97 // Although other pieces keep their names here for coding simplicity,
99 // - a "rook" is a swapper, exchanging positions and "capturing" by
100 // mutual destruction only.
101 // - a "knight" is a long-leaper, capturing as in draughts
102 // - a "bishop" is a chameleon, capturing as its prey
103 // - a "queen" is a withdrawer+advancer, capturing by moving away from
104 // pieces or advancing in front of them.
106 // Is piece on square (x,y) immobilized?
107 isImmobilized([x
, y
]) {
108 const piece
= this.getPiece(x
, y
);
109 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
110 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
111 for (let step
of adjacentSteps
) {
112 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
115 this.board
[i
][j
] != V
.EMPTY
&&
116 this.getColor(i
, j
) == oppCol
118 const oppPiece
= this.getPiece(i
, j
);
119 if (oppPiece
== V
.IMMOBILIZER
) return [i
, j
];
120 // Only immobilizers are immobilized by chameleons:
121 if (oppPiece
== V
.BISHOP
&& piece
== V
.IMMOBILIZER
) return [i
, j
];
127 static OnEdge(x
, y
) {
128 return x
== 0 || y
== 0 || x
== V
.size
.x
- 1 || y
== V
.size
.y
- 1;
131 getPotentialMovesFrom([x
, y
]) {
132 // Pre-check: is thing on this square immobilized?
133 const imSq
= this.isImmobilized([x
, y
]);
134 const piece
= this.getPiece(x
, y
);
135 if (!!imSq
&& piece
!= V
.KING
) {
136 // Only option is suicide, if I'm not a king:
139 start: { x: x
, y: y
},
140 end: { x: imSq
[0], y: imSq
[1] },
146 c: this.getColor(x
, y
),
147 p: this.getPiece(x
, y
)
156 moves
= this.getPotentialImmobilizerMoves([x
, y
]);
159 moves
= super.getPotentialMovesFrom([x
, y
]);
161 // Post-processing: prune redundant non-minimal capturing moves,
162 // and non-capturing moves ending on the edge:
164 // Useful precomputation
165 m
.dist
= Math
.abs(m
.end
.x
- m
.start
.x
) + Math
.abs(m
.end
.y
- m
.start
.y
);
167 return moves
.filter(m
=> {
168 if (!V
.OnEdge(m
.end
.x
, m
.end
.y
)) return true;
170 if (m
.vanish
.length
== 1) return false;
171 // Capture or swap: only captures get filtered
172 if (m
.appear
.length
== 2) return true;
173 // Can we find other moves with a shorter path to achieve the same
174 // capture? Apply to queens and knights.
179 mv
.vanish
.length
== m
.vanish
.length
&&
180 mv
.vanish
.every(v
=> {
181 return m
.vanish
.some(vv
=> {
183 vv
.x
== v
.x
&& vv
.y
== v
.y
&& vv
.c
== v
.c
&& vv
.p
== v
.p
194 // NOTE: not removing "dist" field; shouldn't matter much...
197 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
198 const piece
= this.getPiece(x
, y
);
200 outerLoop: for (let step
of steps
) {
203 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
204 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
205 if (oneStep
!== undefined) continue outerLoop
;
209 // Only king can take on occupied square:
210 if (piece
== V
.KING
&& V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
211 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
216 // "Cannon/grasshopper pawn"
217 getPotentialPawnMoves([x
, y
]) {
218 const oppCol
= V
.GetOppCol(this.turn
);
220 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
221 adjacentSteps
.forEach(step
=> {
222 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
223 if (V
.OnBoard(i
, j
)) {
224 if (this.board
[i
][j
] == V
.EMPTY
)
225 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
228 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
229 if (V
.OnBoard(ii
, jj
) && this.getColor(ii
, jj
) == oppCol
)
230 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
237 // NOTE: not really captures, but let's keep the name
238 getRookCaptures([x
, y
], byChameleon
) {
240 const oppCol
= V
.GetOppCol(this.turn
);
241 // Simple: if something is visible, we can swap
242 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
243 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
244 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
248 if (V
.OnBoard(i
, j
) && this.getColor(i
, j
) == oppCol
) {
249 const oppPiece
= this.getPiece(i
, j
);
250 if (!byChameleon
|| oppPiece
== V
.ROOK
) {
251 let m
= this.getBasicMove([x
, y
], [i
, j
]);
257 p: this.getPiece(i
, j
)
261 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
262 // Add mutual destruction option:
264 start: { x: x
, y: y
},
267 // TODO: is copying necessary here?
268 vanish: JSON
.parse(JSON
.stringify(m
.vanish
))
279 getPotentialRookMoves(sq
) {
280 return super.getPotentialQueenMoves(sq
).concat(this.getRookCaptures(sq
));
283 getKnightCaptures(startSquare
, byChameleon
) {
284 // Look in every direction for captures
285 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
286 const color
= this.turn
;
287 const oppCol
= V
.GetOppCol(color
);
289 const [x
, y
] = [startSquare
[0], startSquare
[1]];
290 const piece
= this.getPiece(x
, y
); //might be a chameleon!
291 outerLoop: for (let step
of steps
) {
292 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
293 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
299 this.getColor(i
, j
) == color
||
300 (!!byChameleon
&& this.getPiece(i
, j
) != V
.KNIGHT
)
304 // last(thing), cur(thing) : stop if "cur" is our color,
305 // or beyond board limits, or if "last" isn't empty and cur neither.
306 // Otherwise, if cur is empty then add move until cur square;
307 // if cur is occupied then stop if !!byChameleon and the square not
308 // occupied by a leaper.
310 let cur
= [i
+ step
[0], j
+ step
[1]];
311 let vanished
= [new PiPo({ x: x
, y: y
, c: color
, p: piece
})];
312 while (V
.OnBoard(cur
[0], cur
[1])) {
313 if (this.board
[last
[0]][last
[1]] != V
.EMPTY
) {
314 const oppPiece
= this.getPiece(last
[0], last
[1]);
315 if (!!byChameleon
&& oppPiece
!= V
.KNIGHT
) continue outerLoop
;
318 new PiPo({ x: last
[0], y: last
[1], c: oppCol
, p: oppPiece
})
321 if (this.board
[cur
[0]][cur
[1]] != V
.EMPTY
) {
323 this.getColor(cur
[0], cur
[1]) == color
||
324 this.board
[last
[0]][last
[1]] != V
.EMPTY
326 //TODO: redundant test
332 appear: [new PiPo({ x: cur
[0], y: cur
[1], c: color
, p: piece
})],
333 vanish: JSON
.parse(JSON
.stringify(vanished
)), //TODO: required?
334 start: { x: x
, y: y
},
335 end: { x: cur
[0], y: cur
[1] }
339 last
= [last
[0] + step
[0], last
[1] + step
[1]];
340 cur
= [cur
[0] + step
[0], cur
[1] + step
[1]];
347 getPotentialKnightMoves(sq
) {
348 return super.getPotentialQueenMoves(sq
).concat(this.getKnightCaptures(sq
));
352 getPotentialBishopMoves([x
, y
]) {
353 const oppCol
= V
.GetOppCol(this.turn
);
355 .getPotentialQueenMoves([x
, y
])
356 .concat(this.getKnightCaptures([x
, y
], "asChameleon"))
357 .concat(this.getRookCaptures([x
, y
], "asChameleon"));
358 // No "king capture" because king cannot remain under check
359 this.addQueenCaptures(moves
, "asChameleon");
360 // Also add pawn captures (as a pawn):
361 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
362 adjacentSteps
.forEach(step
=> {
363 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
364 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
365 // Try to leap over (i,j):
368 this.board
[i
][j
] != V
.EMPTY
&&
369 this.board
[ii
][jj
] != V
.EMPTY
&&
370 this.getColor(ii
, jj
) == oppCol
&&
371 this.getPiece(ii
, jj
) == V
.PAWN
373 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
376 // Post-processing: merge similar moves, concatenating vanish arrays
377 let mergedMoves
= {};
379 const key
= m
.end
.x
+ V
.size
.x
* m
.end
.y
;
380 if (!mergedMoves
[key
]) mergedMoves
[key
] = m
;
382 for (let i
= 1; i
< m
.vanish
.length
; i
++)
383 mergedMoves
[key
].vanish
.push(m
.vanish
[i
]);
386 return Object
.values(mergedMoves
);
389 addQueenCaptures(moves
, byChameleon
) {
390 if (moves
.length
== 0) return;
391 const [x
, y
] = [moves
[0].start
.x
, moves
[0].start
.y
];
392 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
393 let capturingDirStart
= {};
394 const oppCol
= V
.GetOppCol(this.turn
);
395 // Useful precomputation:
396 adjacentSteps
.forEach(step
=> {
397 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
400 this.board
[i
][j
] != V
.EMPTY
&&
401 this.getColor(i
, j
) == oppCol
&&
402 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
404 capturingDirStart
[step
[0] + "_" + step
[1]] = this.getPiece(i
, j
);
409 m
.end
.x
!= x
? (m
.end
.x
- x
) / Math
.abs(m
.end
.x
- x
) : 0,
410 m
.end
.y
!= y
? (m
.end
.y
- y
) / Math
.abs(m
.end
.y
- y
) : 0
412 // TODO: this test should be done only once per direction
413 const capture
= capturingDirStart
[(-step
[0]) + "_" + (-step
[1])];
415 const [i
, j
] = [x
- step
[0], y
- step
[1]];
425 // Also test the end (advancer effect)
426 const [i
, j
] = [m
.end
.x
+ step
[0], m
.end
.y
+ step
[1]];
429 this.board
[i
][j
] != V
.EMPTY
&&
430 this.getColor(i
, j
) == oppCol
&&
431 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
437 p: this.getPiece(i
, j
),
445 // Withdrawer + advancer: "pushme-pullyu"
446 getPotentialQueenMoves(sq
) {
447 let moves
= super.getPotentialQueenMoves(sq
);
448 this.addQueenCaptures(moves
);
452 getPotentialImmobilizerMoves(sq
) {
453 // Immobilizer doesn't capture
454 return super.getPotentialQueenMoves(sq
);
457 // Does m2 un-do m1 ? (to disallow undoing swaps)
458 oppositeMoves(m1
, m2
) {
461 m2
.appear
.length
== 2 &&
462 m1
.start
.x
== m2
.start
.x
&&
463 m1
.end
.x
== m2
.end
.x
&&
464 m1
.start
.y
== m2
.start
.y
&&
470 if (moves
.length
== 0) return [];
471 const color
= this.turn
;
475 const L
= this.smoves
.length
; //at least 1: init from FEN
476 return !this.oppositeMoves(this.smoves
[L
- 1], m
);
482 // isAttacked() is OK because the immobilizer doesn't take
484 isAttackedByPawn([x
, y
], color
) {
485 // Attacked if an enemy pawn stands just behind an immediate obstacle:
486 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
487 for (let step
of adjacentSteps
) {
488 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
489 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
492 this.board
[i
][j
] != V
.EMPTY
&&
493 this.board
[ii
][jj
] != V
.EMPTY
&&
494 this.getColor(ii
, jj
) == color
&&
495 this.getPiece(ii
, jj
) == V
.PAWN
&&
496 !this.isImmobilized([ii
, jj
])
504 isAttackedByRook([x
, y
], color
) {
505 // The only way a swapper can take is by mutual destruction when the
506 // enemy piece stands just next:
507 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
508 for (let step
of adjacentSteps
) {
509 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
512 this.board
[i
][j
] != V
.EMPTY
&&
513 this.getColor(i
, j
) == color
&&
514 this.getPiece(i
, j
) == V
.ROOK
&&
515 !this.isImmobilized([i
, j
])
523 isAttackedByKnight([x
, y
], color
) {
524 // Square (x,y) must be on same line as a knight,
525 // and there must be empty square(s) behind.
526 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
527 outerLoop: for (let step
of steps
) {
528 const [i0
, j0
] = [x
+ step
[0], y
+ step
[1]];
529 if (V
.OnBoard(i0
, j0
) && this.board
[i0
][j0
] == V
.EMPTY
) {
530 // Try in opposite direction:
531 let [i
, j
] = [x
- step
[0], y
- step
[1]];
532 while (V
.OnBoard(i
, j
)) {
533 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
537 if (V
.OnBoard(i
, j
)) {
538 if (this.getColor(i
, j
) == color
) {
540 this.getPiece(i
, j
) == V
.KNIGHT
&&
541 !this.isImmobilized([i
, j
])
547 // could be captured *if there was an empty space*
548 if (this.board
[i
+ step
[0]][j
+ step
[1]] != V
.EMPTY
)
559 isAttackedByBishop([x
, y
], color
) {
560 // We cheat a little here: since this function is used exclusively for
561 // the king, it's enough to check the immediate surrounding of the square.
562 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
563 for (let step
of adjacentSteps
) {
564 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
567 this.board
[i
][j
] != V
.EMPTY
&&
568 this.getColor(i
, j
) == color
&&
569 this.getPiece(i
, j
) == V
.BISHOP
&&
570 !this.isImmobilized([i
, j
])
578 isAttackedByQueen([x
, y
], color
) {
579 // Is there a queen in view?
580 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
581 for (let step
of adjacentSteps
) {
582 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
583 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
589 this.getColor(i
, j
) == color
&&
590 this.getPiece(i
, j
) == V
.QUEEN
592 // Two cases: the queen is at 2 steps at least, or just close
593 // but maybe with enough space behind to withdraw.
594 let attacked
= false;
595 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
596 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
597 if (V
.OnBoard(ii
, jj
) && this.board
[ii
][jj
] == V
.EMPTY
)
600 else attacked
= true;
601 if (attacked
&& !this.isImmobilized([i
, j
])) return true;
607 isAttackedByKing([x
, y
], color
) {
608 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
609 for (let step
of steps
) {
610 let rx
= x
+ step
[0],
614 this.getPiece(rx
, ry
) === V
.KING
&&
615 this.getColor(rx
, ry
) == color
&&
616 !this.isImmobilized([rx
, ry
])
624 static GenRandInitFen(randomness
) {
625 if (randomness
== 0) {
627 "91/1rqnbknqm1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1MQNBKNQR1/91 w 0 -"
631 let pieces
= { w: new Array(8), b: new Array(8) };
632 // Shuffle pieces on first and last rank
633 for (let c
of ["w", "b"]) {
634 if (c
== 'b' && randomness
== 1) {
635 pieces
['b'] = pieces
['w'];
639 // Get random squares for every piece, totally freely
640 let positions
= shuffle(ArrayFun
.range(8));
641 const composition
= ['r', 'm', 'n', 'n', 'q', 'q', 'b', 'k'];
642 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
645 "91/1" + pieces
["b"].join("") +
646 "1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1" +
647 pieces
["w"].join("").toUpperCase() + "1/91 w 0 -"
652 const L
= this.smoves
.length
;
656 : ChessRules
.CoordsToSquare(this.smoves
[L
- 1].start
) +
657 ChessRules
.CoordsToSquare(this.smoves
[L
- 1].end
)
662 return super.getFen() + " " + this.getSmoveFen();
666 return super.getFenForRepeat() + "_" + this.getSmoveFen();
670 super.postPlay(move);
671 this.smoves
.push(this.getSmove(move));
675 super.postUndo(move);
679 static get VALUES() {
691 static get SEARCH_DEPTH() {
696 const initialSquare
= V
.CoordsToSquare(move.start
);
697 const finalSquare
= V
.CoordsToSquare(move.end
);
698 if (move.appear
.length
== 0) {
699 // Suicide 'S' or mutual destruction 'D':
701 initialSquare
+ (move.vanish
.length
== 1 ? "S" : "D" + finalSquare
)
704 let notation
= undefined;
705 if (move.appear
[0].p
== V
.PAWN
) {
706 // Pawn: generally ambiguous short notation, so we use full description
707 notation
= "P" + initialSquare
+ finalSquare
;
708 } else if (move.appear
[0].p
== V
.KING
)
709 notation
= "K" + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
;
710 else notation
= move.appear
[0].p
.toUpperCase() + finalSquare
;
711 // Add a capture mark (not describing what is captured...):
712 if (move.vanish
.length
> 1 && move.appear
[0].p
!= V
.KING
) notation
+= "X";