6af29c0555dde5d3188d8f12caebc7e46eedebfe
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 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
209 const piece
= this.getPiece(x
, y
);
211 outerLoop: for (let step
of steps
) {
214 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
215 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
216 if (oneStep
!== undefined) continue outerLoop
;
220 // Only king can take on occupied square:
221 if (piece
== V
.KING
&& V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
222 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
227 // "Cannon/grasshopper pawn"
228 getPotentialPawnMoves([x
, y
]) {
229 const oppCol
= V
.GetOppCol(this.turn
);
231 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
232 adjacentSteps
.forEach(step
=> {
233 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
234 if (V
.OnBoard(i
, j
)) {
235 if (this.board
[i
][j
] == V
.EMPTY
)
236 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
239 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
240 if (V
.OnBoard(ii
, jj
) && this.getColor(ii
, jj
) == oppCol
)
241 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
248 // NOTE: not really captures, but let's keep the name
249 getRookCaptures([x
, y
], byChameleon
) {
251 const oppCol
= V
.GetOppCol(this.turn
);
252 // Simple: if something is visible, we can swap
253 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
254 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
255 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
259 if (V
.OnBoard(i
, j
) && this.getColor(i
, j
) == oppCol
) {
260 const oppPiece
= this.getPiece(i
, j
);
261 if (!byChameleon
|| oppPiece
== V
.ROOK
) {
262 let m
= this.getBasicMove([x
, y
], [i
, j
]);
268 p: this.getPiece(i
, j
)
272 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
273 // Add mutual destruction option:
275 start: { x: x
, y: y
},
278 // TODO: is copying necessary here?
279 vanish: JSON
.parse(JSON
.stringify(m
.vanish
))
290 getPotentialRookMoves(sq
) {
291 return super.getPotentialQueenMoves(sq
).concat(this.getRookCaptures(sq
));
294 getKnightCaptures([x
, y
], byChameleon
) {
295 // Look in every direction for captures
296 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
297 const color
= this.turn
;
298 const oppCol
= V
.GetOppCol(color
);
300 const piece
= this.getPiece(x
, y
); //might be a chameleon!
301 outerLoop: for (let step
of steps
) {
302 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
303 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
309 this.getColor(i
, j
) == color
||
310 (!!byChameleon
&& this.getPiece(i
, j
) != V
.KNIGHT
)
314 // last(thing), cur(thing) : stop if "cur" is our color,
315 // or beyond board limits, or if "last" isn't empty and cur neither.
316 // Otherwise, if cur is empty then add move until cur square;
317 // if cur is occupied then stop if !!byChameleon and the square not
318 // occupied by a leaper.
320 let cur
= [i
+ step
[0], j
+ step
[1]];
321 let vanished
= [new PiPo({ x: x
, y: y
, c: color
, p: piece
})];
322 while (V
.OnBoard(cur
[0], cur
[1])) {
323 if (this.board
[last
[0]][last
[1]] != V
.EMPTY
) {
324 const oppPiece
= this.getPiece(last
[0], last
[1]);
325 if (!!byChameleon
&& oppPiece
!= V
.KNIGHT
) continue outerLoop
;
328 new PiPo({ x: last
[0], y: last
[1], c: oppCol
, p: oppPiece
})
331 if (this.board
[cur
[0]][cur
[1]] != V
.EMPTY
) {
333 this.getColor(cur
[0], cur
[1]) == color
||
334 this.board
[last
[0]][last
[1]] != V
.EMPTY
336 //TODO: redundant test
343 appear: [new PiPo({ x: cur
[0], y: cur
[1], c: color
, p: piece
})],
344 vanish: JSON
.parse(JSON
.stringify(vanished
)), //TODO: required?
345 start: { x: x
, y: y
},
346 end: { x: cur
[0], y: cur
[1] }
350 last
= [last
[0] + step
[0], last
[1] + step
[1]];
351 cur
= [cur
[0] + step
[0], cur
[1] + step
[1]];
358 getPotentialKnightMoves(sq
) {
359 return super.getPotentialQueenMoves(sq
).concat(this.getKnightCaptures(sq
));
363 getPotentialBishopMoves([x
, y
]) {
364 const oppCol
= V
.GetOppCol(this.turn
);
366 .getPotentialQueenMoves([x
, y
])
367 .concat(this.getKnightCaptures([x
, y
], "asChameleon"))
368 .concat(this.getRookCaptures([x
, y
], "asChameleon"));
369 // No "king capture" because king cannot remain under check
370 this.addQueenCaptures(moves
, "asChameleon");
371 // Also add pawn captures (as a pawn):
372 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
373 adjacentSteps
.forEach(step
=> {
374 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
375 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
376 // Try to leap over (i,j):
379 this.board
[i
][j
] != V
.EMPTY
&&
380 this.board
[ii
][jj
] != V
.EMPTY
&&
381 this.getColor(ii
, jj
) == oppCol
&&
382 this.getPiece(ii
, jj
) == V
.PAWN
384 moves
.push(this.getBasicMove([x
, y
], [ii
, jj
]));
387 // Post-processing: merge similar moves, concatenating vanish arrays
388 let mergedMoves
= {};
390 const key
= m
.end
.x
+ V
.size
.x
* m
.end
.y
;
391 if (!mergedMoves
[key
]) mergedMoves
[key
] = m
;
393 for (let i
= 1; i
< m
.vanish
.length
; i
++)
394 mergedMoves
[key
].vanish
.push(m
.vanish
[i
]);
397 return Object
.values(mergedMoves
);
400 addQueenCaptures(moves
, byChameleon
) {
401 if (moves
.length
== 0) return;
402 const [x
, y
] = [moves
[0].start
.x
, moves
[0].start
.y
];
403 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
404 let capturingDirStart
= {};
405 const oppCol
= V
.GetOppCol(this.turn
);
406 // Useful precomputation:
407 adjacentSteps
.forEach(step
=> {
408 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
411 this.board
[i
][j
] != V
.EMPTY
&&
412 this.getColor(i
, j
) == oppCol
&&
413 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
415 capturingDirStart
[step
[0] + "_" + step
[1]] = this.getPiece(i
, j
);
420 m
.end
.x
!= x
? (m
.end
.x
- x
) / Math
.abs(m
.end
.x
- x
) : 0,
421 m
.end
.y
!= y
? (m
.end
.y
- y
) / Math
.abs(m
.end
.y
- y
) : 0
423 // TODO: this test should be done only once per direction
424 const capture
= capturingDirStart
[(-step
[0]) + "_" + (-step
[1])];
426 const [i
, j
] = [x
- step
[0], y
- step
[1]];
436 // Also test the end (advancer effect)
437 const [i
, j
] = [m
.end
.x
+ step
[0], m
.end
.y
+ step
[1]];
440 this.board
[i
][j
] != V
.EMPTY
&&
441 this.getColor(i
, j
) == oppCol
&&
442 (!byChameleon
|| this.getPiece(i
, j
) == V
.QUEEN
)
448 p: this.getPiece(i
, j
),
456 // Withdrawer + advancer: "pushme-pullyu"
457 getPotentialQueenMoves(sq
) {
458 let moves
= super.getPotentialQueenMoves(sq
);
459 this.addQueenCaptures(moves
);
463 getPotentialImmobilizerMoves(sq
) {
464 // Immobilizer doesn't capture
465 return super.getPotentialQueenMoves(sq
);
468 // Does m2 un-do m1 ? (to disallow undoing swaps)
469 oppositeMoves(m1
, m2
) {
472 m2
.appear
.length
== 2 &&
473 m1
.start
.x
== m2
.start
.x
&&
474 m1
.end
.x
== m2
.end
.x
&&
475 m1
.start
.y
== m2
.start
.y
&&
481 if (moves
.length
== 0) return [];
482 const color
= this.turn
;
486 const L
= this.smoves
.length
; //at least 1: init from FEN
487 return !this.oppositeMoves(this.smoves
[L
- 1], m
);
493 // isAttacked() is OK because the immobilizer doesn't take
495 isAttackedByPawn([x
, y
], color
) {
496 // Attacked if an enemy pawn stands just behind an immediate obstacle:
497 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
498 for (let step
of adjacentSteps
) {
499 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
500 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
503 this.board
[i
][j
] != V
.EMPTY
&&
504 this.board
[ii
][jj
] != V
.EMPTY
&&
505 this.getColor(ii
, jj
) == color
&&
506 this.getPiece(ii
, jj
) == V
.PAWN
&&
507 !this.isImmobilized([ii
, jj
])
515 isAttackedByRook([x
, y
], color
) {
516 // The only way a swapper can take is by mutual destruction when the
517 // enemy piece stands just next:
518 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
519 for (let step
of adjacentSteps
) {
520 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
523 this.board
[i
][j
] != V
.EMPTY
&&
524 this.getColor(i
, j
) == color
&&
525 this.getPiece(i
, j
) == V
.ROOK
&&
526 !this.isImmobilized([i
, j
])
534 isAttackedByKnight([x
, y
], color
) {
535 // Square (x,y) must be on same line as a knight,
536 // and there must be empty square(s) behind.
537 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
538 outerLoop: for (let step
of steps
) {
539 const [i0
, j0
] = [x
+ step
[0], y
+ step
[1]];
540 if (V
.OnBoard(i0
, j0
) && this.board
[i0
][j0
] == V
.EMPTY
) {
541 // Try in opposite direction:
542 let [i
, j
] = [x
- step
[0], y
- step
[1]];
543 while (V
.OnBoard(i
, j
)) {
544 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
548 if (V
.OnBoard(i
, j
)) {
549 if (this.getColor(i
, j
) == color
) {
551 this.getPiece(i
, j
) == V
.KNIGHT
&&
552 !this.isImmobilized([i
, j
])
558 // could be captured *if there was an empty space*
559 if (this.board
[i
+ step
[0]][j
+ step
[1]] != V
.EMPTY
)
570 isAttackedByBishop([x
, y
], color
) {
571 // We cheat a little here: since this function is used exclusively for
572 // the king, it's enough to check the immediate surrounding of the square.
573 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
574 for (let step
of adjacentSteps
) {
575 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
578 this.board
[i
][j
] != V
.EMPTY
&&
579 this.getColor(i
, j
) == color
&&
580 this.getPiece(i
, j
) == V
.BISHOP
&&
581 !this.isImmobilized([i
, j
])
589 isAttackedByQueen([x
, y
], color
) {
590 // Is there a queen in view?
591 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
592 for (let step
of adjacentSteps
) {
593 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
594 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
600 this.getColor(i
, j
) == color
&&
601 this.getPiece(i
, j
) == V
.QUEEN
603 // Two cases: the queen is at 2 steps at least, or just close
604 // but maybe with enough space behind to withdraw.
605 let attacked
= false;
606 if (i
== x
+ step
[0] && j
== y
+ step
[1]) {
607 const [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
608 if (V
.OnBoard(ii
, jj
) && this.board
[ii
][jj
] == V
.EMPTY
)
611 else attacked
= true;
612 if (attacked
&& !this.isImmobilized([i
, j
])) return true;
618 isAttackedByKing([x
, y
], color
) {
619 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
620 for (let step
of steps
) {
621 let rx
= x
+ step
[0],
625 this.getPiece(rx
, ry
) === V
.KING
&&
626 this.getColor(rx
, ry
) == color
&&
627 !this.isImmobilized([rx
, ry
])
635 static GenRandInitFen(randomness
) {
636 if (randomness
== 0) {
638 "91/1rqnbknqm1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1MQNBKNQR1/91 w 0 -"
642 let pieces
= { w: new Array(8), b: new Array(8) };
643 // Shuffle pieces on first and last rank
644 for (let c
of ["w", "b"]) {
645 if (c
== 'b' && randomness
== 1) {
646 pieces
['b'] = pieces
['w'];
650 // Get random squares for every piece, totally freely
651 let positions
= shuffle(ArrayFun
.range(8));
652 const composition
= ['r', 'm', 'n', 'n', 'q', 'q', 'b', 'k'];
653 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
656 "91/1" + pieces
["b"].join("") +
657 "1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1" +
658 pieces
["w"].join("").toUpperCase() + "1/91 w 0 -"
663 const L
= this.smoves
.length
;
667 : ChessRules
.CoordsToSquare(this.smoves
[L
- 1].start
) +
668 ChessRules
.CoordsToSquare(this.smoves
[L
- 1].end
)
673 return super.getFen() + " " + this.getSmoveFen();
677 return super.getFenForRepeat() + "_" + this.getSmoveFen();
681 super.postPlay(move);
682 this.smoves
.push(this.getSmove(move));
686 super.postUndo(move);
690 static get VALUES() {
702 static get SEARCH_DEPTH() {
707 const initialSquare
= V
.CoordsToSquare(move.start
);
708 const finalSquare
= V
.CoordsToSquare(move.end
);
709 if (move.appear
.length
== 0) {
710 // Suicide 'S' or mutual destruction 'D':
712 initialSquare
+ (move.vanish
.length
== 1 ? "S" : "D" + finalSquare
)
715 let notation
= undefined;
716 if (move.appear
[0].p
== V
.PAWN
) {
717 // Pawn: generally ambiguous short notation, so we use full description
718 notation
= "P" + initialSquare
+ finalSquare
;
720 else if (move.appear
[0].p
== V
.KING
)
721 notation
= "K" + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
;
723 notation
= move.appear
[0].p
.toUpperCase() + finalSquare
;
724 // Add a capture mark (not describing what is captured...):
725 if (move.vanish
.length
> 1) notation
+= "X";