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
{
7 static get HasFlags() {
11 static get HasEnpassant() {
16 return ChessRules
.PIECES
.concat([V
.IMMOBILIZER
]);
30 //'m' for Immobilizer (I is too similar to 1)
32 return b
; //usual piece
36 // The only "choice" case is between a swap and a mutual destruction:
37 // show empty square in case of mutual destruction.
38 if (m
.appear
.length
== 0) return "Rococo/empty";
39 return m
.appear
[0].c
+ m
.appear
[0].p
;
42 setOtherVariables(fen
) {
43 // No castling, but checks, so keep track of kings
44 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
45 const fenParts
= fen
.split(" ");
46 const position
= fenParts
[0].split("/");
47 for (let i
= 0; i
< position
.length
; i
++) {
49 for (let j
= 0; j
< position
[i
].length
; j
++) {
50 switch (position
[i
].charAt(j
)) {
52 this.kingPos
["b"] = [i
, k
];
55 this.kingPos
["w"] = [i
, k
];
58 const num
= parseInt(position
[i
].charAt(j
), 10);
59 if (!isNaN(num
)) k
+= num
- 1;
65 // Local stack of swaps:
67 const smove
= V
.ParseFen(fen
).smove
;
68 if (smove
== "-") this.smoves
.push(null);
71 start: ChessRules
.SquareToCoords(smove
.substr(0, 2)),
72 end: ChessRules
.SquareToCoords(smove
.substr(2))
77 static ParseFen(fen
) {
79 ChessRules
.ParseFen(fen
),
80 { smove: fen
.split(" ")[3] }
84 static IsGoodFen(fen
) {
85 if (!ChessRules
.IsGoodFen(fen
)) return false;
86 const fenParts
= fen
.split(" ");
87 if (fenParts
.length
!= 4) return false;
88 if (fenParts
[3] != "-" && !fenParts
[3].match(/^([a-h][1-8]){2}$/))
94 if (move.appear
.length
== 2)
95 return { start: move.start
, end: move.end
};
100 // Add the "capturing edge"
101 return { x: 10, y: 10 };
104 static get IMMOBILIZER() {
107 // Although other pieces keep their names here for coding simplicity,
108 // keep in mind that:
109 // - a "rook" is a swapper, exchanging positions and "capturing" by
110 // mutual destruction only.
111 // - a "knight" is a long-leaper, capturing as in draughts
112 // - a "bishop" is a chameleon, capturing as its prey
113 // - a "queen" is a withdrawer+advancer, capturing by moving away from
114 // pieces or advancing in front of them.
116 // Is piece on square (x,y) immobilized?
117 isImmobilized([x
, y
]) {
118 const piece
= this.getPiece(x
, y
);
119 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
120 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
121 for (let step
of adjacentSteps
) {
122 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
125 this.board
[i
][j
] != V
.EMPTY
&&
126 this.getColor(i
, j
) == oppCol
128 const oppPiece
= this.getPiece(i
, j
);
129 if (oppPiece
== V
.IMMOBILIZER
) return [i
, j
];
130 // Only immobilizers are immobilized by chameleons:
131 if (oppPiece
== V
.BISHOP
&& piece
== V
.IMMOBILIZER
) return [i
, j
];
137 static OnEdge(x
, y
) {
138 return x
== 0 || y
== 0 || x
== V
.size
.x
- 1 || y
== V
.size
.y
- 1;
141 getPotentialMovesFrom([x
, y
]) {
142 // Pre-check: is thing on this square immobilized?
143 const imSq
= this.isImmobilized([x
, y
]);
144 const piece
= this.getPiece(x
, y
);
146 if (piece
== V
.KING
) return [];
147 // Only option is suicide, if I'm not a king:
150 start: { x: x
, y: y
},
151 end: { x: imSq
[0], y: imSq
[1] },
157 c: this.getColor(x
, y
),
158 p: this.getPiece(x
, y
)
167 moves
= this.getPotentialImmobilizerMoves([x
, y
]);
170 moves
= super.getPotentialMovesFrom([x
, y
]);
172 // Post-processing: prune redundant non-minimal capturing moves,
173 // and non-capturing moves ending on the edge:
175 // Useful precomputation
176 m
.dist
= Math
.abs(m
.end
.x
- m
.start
.x
) + Math
.abs(m
.end
.y
- m
.start
.y
);
178 return moves
.filter(m
=> {
179 if (!V
.OnEdge(m
.end
.x
, m
.end
.y
)) return true;
181 if (m
.vanish
.length
== 1) return false;
182 // Capture or swap: only captures get filtered
183 if (m
.appear
.length
== 2) return true;
184 // Can we find other moves with a shorter path to achieve the same
185 // capture? Apply to queens and knights.
190 mv
.vanish
.length
== m
.vanish
.length
&&
191 mv
.vanish
.every(v
=> {
192 return m
.vanish
.some(vv
=> {
194 vv
.x
== v
.x
&& vv
.y
== v
.y
&& vv
.c
== v
.c
&& vv
.p
== v
.p
205 // NOTE: not removing "dist" field; shouldn't matter much...
208 canTake([x1
, y1
], [x2
, y2
]) {
210 this.getPiece(x1
, y1
) == V
.KING
&&
211 this.getColor(x1
, y1
) != this.getColor(x2
, y2
)
215 // "Cannon/grasshopper pawn"
216 getPotentialPawnMoves([x
, y
]) {
217 const oppCol
= V
.GetOppCol(this.turn
);
219 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
220 adjacentSteps
.forEach(step
=> {
221 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
222 if (V
.OnBoard(i
, j
)) {
223 if (this.board
[i
][j
] == V
.EMPTY
)
224 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
227 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
228 if (V
.OnBoard(ii
, jj
) && this.getColor(ii
, jj
) == oppCol
)
229 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
236 // NOTE: not really captures, but let's keep the name
237 getRookCaptures([x
, y
], byChameleon
) {
239 const oppCol
= V
.GetOppCol(this.turn
);
240 // Simple: if something is visible, we can swap
241 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
242 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
243 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
247 if (V
.OnBoard(i
, j
) && this.getColor(i
, j
) == oppCol
) {
248 const oppPiece
= this.getPiece(i
, j
);
249 if (!byChameleon
|| oppPiece
== V
.ROOK
) {
250 let m
= this.getBasicMove([x
, y
], [i
, j
]);
256 p: this.getPiece(i
, j
)
260 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
261 // Add mutual destruction option:
263 start: { x: x
, y: y
},
266 // TODO: is copying necessary here?
267 vanish: JSON
.parse(JSON
.stringify(m
.vanish
))
278 getPotentialRookMoves(sq
) {
279 return super.getPotentialQueenMoves(sq
).concat(this.getRookCaptures(sq
));
282 getKnightCaptures([x
, y
], byChameleon
) {
283 // Look in every direction for captures
284 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
285 const color
= this.turn
;
286 const oppCol
= V
.GetOppCol(color
);
288 const piece
= this.getPiece(x
, y
); //might be a chameleon!
289 outerLoop: for (let step
of steps
) {
290 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
291 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
297 this.getColor(i
, j
) == color
||
298 (!!byChameleon
&& this.getPiece(i
, j
) != V
.KNIGHT
)
302 // last(thing), cur(thing) : stop if "cur" is our color,
303 // or beyond board limits, or if "last" isn't empty and cur neither.
304 // Otherwise, if cur is empty then add move until cur square;
305 // if cur is occupied then stop if !!byChameleon and the square not
306 // occupied by a leaper.
308 let cur
= [i
+ step
[0], j
+ step
[1]];
309 let vanished
= [new PiPo({ x: x
, y: y
, c: color
, p: piece
})];
310 while (V
.OnBoard(cur
[0], cur
[1])) {
311 if (this.board
[last
[0]][last
[1]] != V
.EMPTY
) {
312 const oppPiece
= this.getPiece(last
[0], last
[1]);
313 if (!!byChameleon
&& oppPiece
!= V
.KNIGHT
) continue outerLoop
;
316 new PiPo({ x: last
[0], y: last
[1], c: oppCol
, p: oppPiece
})
319 if (this.board
[cur
[0]][cur
[1]] != V
.EMPTY
) {
321 this.getColor(cur
[0], cur
[1]) == color
||
322 this.board
[last
[0]][last
[1]] != V
.EMPTY
324 //TODO: redundant test
331 appear: [new PiPo({ x: cur
[0], y: cur
[1], c: color
, p: piece
})],
332 vanish: JSON
.parse(JSON
.stringify(vanished
)), //TODO: required?
333 start: { x: x
, y: y
},
334 end: { x: cur
[0], y: cur
[1] }
338 last
= [last
[0] + step
[0], last
[1] + step
[1]];
339 cur
= [cur
[0] + step
[0], cur
[1] + step
[1]];
346 getPotentialKnightMoves(sq
) {
347 return super.getPotentialQueenMoves(sq
).concat(this.getKnightCaptures(sq
));
351 getPotentialBishopMoves([x
, y
]) {
352 const oppCol
= V
.GetOppCol(this.turn
);
354 .getPotentialQueenMoves([x
, y
])
355 .concat(this.getKnightCaptures([x
, y
], "asChameleon"))
356 .concat(this.getRookCaptures([x
, y
], "asChameleon"));
357 // No "king capture" because king cannot remain under check
358 this.addQueenCaptures(moves
, "asChameleon");
359 // Also add pawn captures (as a pawn):
360 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
361 adjacentSteps
.forEach(step
=> {
362 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
363 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
364 // Try to leap over (i,j):
367 this.board
[i
][j
] != V
.EMPTY
&&
368 this.board
[ii
][jj
] != V
.EMPTY
&&
369 this.getColor(ii
, jj
) == oppCol
&&
370 this.getPiece(ii
, jj
) == V
.PAWN
372 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
375 // Post-processing: merge similar moves, concatenating vanish arrays
376 let mergedMoves
= {};
378 const key
= m
.end
.x
+ V
.size
.x
* m
.end
.y
;
379 if (!mergedMoves
[key
]) mergedMoves
[key
] = m
;
381 for (let i
= 1; i
< m
.vanish
.length
; i
++)
382 mergedMoves
[key
].vanish
.push(m
.vanish
[i
]);
385 return Object
.values(mergedMoves
);
388 addQueenCaptures(moves
, byChameleon
) {
389 if (moves
.length
== 0) return;
390 const [x
, y
] = [moves
[0].start
.x
, moves
[0].start
.y
];
391 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
392 let capturingDirStart
= {};
393 const oppCol
= V
.GetOppCol(this.turn
);
394 // Useful precomputation:
395 adjacentSteps
.forEach(step
=> {
396 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
399 this.board
[i
][j
] != V
.EMPTY
&&
400 this.getColor(i
, j
) == oppCol
&&
401 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
403 capturingDirStart
[step
[0] + "_" + step
[1]] = this.getPiece(i
, j
);
408 m
.end
.x
!= x
? (m
.end
.x
- x
) / Math
.abs(m
.end
.x
- x
) : 0,
409 m
.end
.y
!= y
? (m
.end
.y
- y
) / Math
.abs(m
.end
.y
- y
) : 0
411 // TODO: this test should be done only once per direction
412 const capture
= capturingDirStart
[(-step
[0]) + "_" + (-step
[1])];
414 const [i
, j
] = [x
- step
[0], y
- step
[1]];
424 // Also test the end (advancer effect)
425 const [i
, j
] = [m
.end
.x
+ step
[0], m
.end
.y
+ step
[1]];
428 this.board
[i
][j
] != V
.EMPTY
&&
429 this.getColor(i
, j
) == oppCol
&&
430 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
436 p: this.getPiece(i
, j
),
444 // Withdrawer + advancer: "pushme-pullyu"
445 getPotentialQueenMoves(sq
) {
446 let moves
= super.getPotentialQueenMoves(sq
);
447 this.addQueenCaptures(moves
);
451 getPotentialImmobilizerMoves(sq
) {
452 // Immobilizer doesn't capture
453 return super.getPotentialQueenMoves(sq
);
456 // Does m2 un-do m1 ? (to disallow undoing swaps)
457 oppositeMoves(m1
, m2
) {
460 m2
.appear
.length
== 2 &&
461 m1
.start
.x
== m2
.start
.x
&&
462 m1
.end
.x
== m2
.end
.x
&&
463 m1
.start
.y
== m2
.start
.y
&&
469 if (moves
.length
== 0) return [];
470 const color
= this.turn
;
474 const L
= this.smoves
.length
; //at least 1: init from FEN
475 return !this.oppositeMoves(this.smoves
[L
- 1], m
);
481 // isAttacked() is OK because the immobilizer doesn't take
483 isAttackedByPawn([x
, y
], color
) {
484 // Attacked if an enemy pawn stands just behind an immediate obstacle:
485 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
486 for (let step
of adjacentSteps
) {
487 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
488 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
491 this.board
[i
][j
] != V
.EMPTY
&&
492 this.board
[ii
][jj
] != V
.EMPTY
&&
493 this.getColor(ii
, jj
) == color
&&
494 this.getPiece(ii
, jj
) == V
.PAWN
&&
495 !this.isImmobilized([ii
, jj
])
503 isAttackedByRook([x
, y
], color
) {
504 // The only way a swapper can take is by mutual destruction when the
505 // enemy piece stands just next:
506 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
507 for (let step
of adjacentSteps
) {
508 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
511 this.board
[i
][j
] != V
.EMPTY
&&
512 this.getColor(i
, j
) == color
&&
513 this.getPiece(i
, j
) == V
.ROOK
&&
514 !this.isImmobilized([i
, j
])
522 isAttackedByKnight([x
, y
], color
) {
523 // Square (x,y) must be on same line as a knight,
524 // and there must be empty square(s) behind.
525 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
526 outerLoop: for (let step
of steps
) {
527 const [i0
, j0
] = [x
+ step
[0], y
+ step
[1]];
528 if (V
.OnBoard(i0
, j0
) && this.board
[i0
][j0
] == V
.EMPTY
) {
529 // Try in opposite direction:
530 let [i
, j
] = [x
- step
[0], y
- step
[1]];
531 while (V
.OnBoard(i
, j
)) {
532 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
536 if (V
.OnBoard(i
, j
)) {
537 if (this.getColor(i
, j
) == color
) {
539 this.getPiece(i
, j
) == V
.KNIGHT
&&
540 !this.isImmobilized([i
, j
])
546 // could be captured *if there was an empty space*
547 if (this.board
[i
+ step
[0]][j
+ step
[1]] != V
.EMPTY
)
558 isAttackedByBishop([x
, y
], color
) {
559 // We cheat a little here: since this function is used exclusively for
560 // the king, it's enough to check the immediate surrounding of the square.
561 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
562 for (let step
of adjacentSteps
) {
563 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
566 this.board
[i
][j
] != V
.EMPTY
&&
567 this.getColor(i
, j
) == color
&&
568 this.getPiece(i
, j
) == V
.BISHOP
&&
569 !this.isImmobilized([i
, j
])
577 isAttackedByQueen([x
, y
], color
) {
578 // Is there a queen in view?
579 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
580 for (let step
of adjacentSteps
) {
581 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
582 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
588 this.getColor(i
, j
) == color
&&
589 this.getPiece(i
, j
) == V
.QUEEN
591 // Two cases: the queen is at 2 steps at least, or just close
592 // but maybe with enough space behind to withdraw.
593 let attacked
= false;
594 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
595 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
596 if (V
.OnBoard(ii
, jj
) && this.board
[ii
][jj
] == V
.EMPTY
)
599 else attacked
= true;
600 if (attacked
&& !this.isImmobilized([i
, j
])) return true;
606 isAttackedByKing([x
, y
], color
) {
607 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
608 for (let step
of steps
) {
609 let rx
= x
+ step
[0],
613 this.getPiece(rx
, ry
) === V
.KING
&&
614 this.getColor(rx
, ry
) == color
&&
615 !this.isImmobilized([rx
, ry
])
623 static GenRandInitFen(options
) {
624 if (options
.randomness
== 0) {
626 "91/1rqnbknqm1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1MQNBKNQR1/91 w 0 -"
630 let pieces
= { w: new Array(8), b: new Array(8) };
631 // Shuffle pieces on first and last rank
632 for (let c
of ["w", "b"]) {
633 if (c
== 'b' && options
.randomness
== 1) {
634 pieces
['b'] = pieces
['w'];
638 // Get random squares for every piece, totally freely
639 let positions
= shuffle(ArrayFun
.range(8));
640 const composition
= ['r', 'm', 'n', 'n', 'q', 'q', 'b', 'k'];
641 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
644 "91/1" + pieces
["b"].join("") +
645 "1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1" +
646 pieces
["w"].join("").toUpperCase() + "1/91 w 0 -"
651 const L
= this.smoves
.length
;
655 : ChessRules
.CoordsToSquare(this.smoves
[L
- 1].start
) +
656 ChessRules
.CoordsToSquare(this.smoves
[L
- 1].end
)
661 return super.getFen() + " " + this.getSmoveFen();
665 return super.getFenForRepeat() + "_" + this.getSmoveFen();
669 super.postPlay(move);
670 this.smoves
.push(this.getSmove(move));
674 super.postUndo(move);
678 static get VALUES() {
690 static get SEARCH_DEPTH() {
695 const initialSquare
= V
.CoordsToSquare(move.start
);
696 const finalSquare
= V
.CoordsToSquare(move.end
);
697 if (move.appear
.length
== 0) {
698 // Suicide 'S' or mutual destruction 'D':
700 initialSquare
+ (move.vanish
.length
== 1 ? "S" : "D" + finalSquare
)
703 let notation
= undefined;
704 if (move.appear
[0].p
== V
.PAWN
) {
705 // Pawn: generally ambiguous short notation, so we use full description
706 notation
= "P" + initialSquare
+ finalSquare
;
708 else if (move.appear
[0].p
== V
.KING
)
709 notation
= "K" + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
;
711 notation
= move.appear
[0].p
.toUpperCase() + finalSquare
;
712 // Add a capture mark (not describing what is captured...):
713 if (move.vanish
.length
> 1) notation
+= "X";