68910e74c700aab7e70667fb4997ffada6082c55
1 import { ChessRules
} from "@/base_rules";
2 import { CoregalRules
} from "@/variants/Coregal";
4 export class TwokingsRules
extends CoregalRules
{
5 static get PawnSpecs() {
9 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.KING
]) }
13 static IsGoodPosition(position
) {
14 if (position
.length
== 0) return false;
15 const rows
= position
.split("/");
16 if (rows
.length
!= V
.size
.x
) return false;
17 let kings
= { "w": 0, "b": 0 };
18 for (let row
of rows
) {
20 for (let i
= 0; i
< row
.length
; i
++) {
21 if (['K','k'].includes(row
[i
])) kings
[row
[i
]]++;
22 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
24 const num
= parseInt(row
[i
]);
25 if (isNaN(num
)) return false;
29 if (sumElts
!= V
.size
.y
) return false;
31 // Two kings (at least) per side should be present:
32 if (Object
.values(kings
).some(v
=> v
< 2)) return false;
36 // Not scanning king positions. In this variant, scan the board everytime.
39 getCheckSquares(color
) {
41 const oppCol
= V
.GetOppCol(color
);
42 for (let i
=0; i
<V
.size
.x
; i
++) {
43 for (let j
=0; j
<V
.size
.y
; j
++) {
45 this.getColor(i
, j
) == color
&&
46 this.getPiece(i
, j
) == V
.KING
&&
47 this.isAttacked([i
, j
], oppCol
)
56 static GenRandInitFen(randomness
) {
58 return "rnqkkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNQKKBNR w 0 adehadeh -";
60 const replaceBishop
= (fen
, first
, ch1
, ch2
) => {
61 // Remove and re-add final part:
62 const suffix
= fen
.substr(-15);
63 fen
= fen
.slice(0, -15);
64 if (first
) fen
= fen
.replace(ch1
, ch2
);
67 fen
.split("").reverse().join("")
69 .split("").reverse().join("")
74 const sameIndexReplace
= (fen
) => {
75 const first
= (Math
.random() < 0.5);
77 replaceBishop(fen
, first
, 'B', 'Q'),
85 CoregalRules
.GenRandInitFen(randomness
)
86 .replace("q", "k").replace("Q", "K");
87 // Now replace a bishop by the queen,
88 // so that bishops are of different colors:
89 if (randomness
== 1) return sameIndexReplace(fen
);
90 const wOdd
= fen
.indexOf('B') % 2;
91 const bOdd
= fen
.indexOf('b') % 2;
92 // Since there are 7 slashes, different oddities means symmetric
93 if (wOdd
!= bOdd
) return sameIndexReplace(fen
);
94 const wFirst
= (Math
.random() < 0.5);
96 replaceBishop(fen
, wFirst
, 'B', 'Q'),
104 const oppCol
= V
.GetOppCol(color
);
105 for (let i
=0; i
<V
.size
.x
; i
++) {
106 for (let j
=0; j
<V
.size
.y
; j
++) {
108 this.getColor(i
, j
) == color
&&
109 this.getPiece(i
, j
) == V
.KING
&&
110 this.isAttacked([i
, j
], oppCol
)
120 const piece
= move.vanish
[0].p
;
121 super.updateCastleFlags(move, piece
, "twoKings");