1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
3 import { ArrayFun
} from "@/utils/array";
5 export class BicolourRules
extends ChessRules
{
6 static get HasFlags() {
10 canTake([x1
, y1
], [x2
, y2
]) {
12 this.getPiece(x1
, y1
) == V
.KING
|| super.canTake([x1
, y1
], [x2
, y2
])
16 static GenRandInitFen(randomness
) {
18 return "rqbnkbnr/pppppppp/8/8/8/8/PPPPPPPP/RQBNKBNR w 0 -";
20 // Place pieces at random but the king cannot be next to a rook or queen.
21 // => One bishop and one knight should surround the king.
22 let pieces
= { w: new Array(8), b: new Array(8) };
24 for (let c
of ["w", "b"]) {
25 if (c
== 'b' && randomness
== 1) {
26 pieces
['b'] = pieces
['w'];
30 let positions
= ArrayFun
.range(8);
32 const kingPos
= randInt(8);
33 let toRemove
= [kingPos
];
34 let knight1Pos
= undefined;
35 let bishop1Pos
= undefined;
37 if (Math
.random() < 0.5) knight1Pos
= 1;
41 else if (kingPos
== V
.size
.y
- 1) {
42 if (Math
.random() < 0.5) knight1Pos
= V
.size
.y
- 2;
43 else bishop1Pos
= V
.size
.y
- 2;
44 toRemove
.push(V
.size
.y
- 2);
47 knight1Pos
= kingPos
+ (Math
.random() < 0.5 ? 1 : -1);
48 bishop1Pos
= kingPos
+ (knight1Pos
< kingPos
? 1 : -1);
49 toRemove
.push(knight1Pos
, bishop1Pos
);
51 const firstPieces
= [kingPos
, knight1Pos
, bishop1Pos
]
52 .filter(elt
=> elt
!== undefined);
54 .sort((a
, b
) => b
- a
)
55 .forEach(elt
=> positions
.splice(elt
, 1));
57 let randIndex
= undefined;
58 if (bishop1Pos
=== undefined) {
59 const posWithIdx
= positions
.map((e
,i
) => { return { e: e
, i: i
}; });
60 let availableSquares
= posWithIdx
.filter(p
=> p
.e
% 2 == 0);
61 randIndex
= randInt(availableSquares
.length
);
62 bishop1Pos
= availableSquares
[randIndex
].e
;
63 positions
.splice(availableSquares
[randIndex
].i
, 1);
65 const posWithIdx
= positions
.map((e
,i
) => { return { e: e
, i: i
}; });
66 const rem1B
= bishop1Pos
% 2;
67 let availableSquares
= posWithIdx
.filter(p
=> p
.e
% 2 == 1 - rem1B
);
68 randIndex
= randInt(availableSquares
.length
);
69 const bishop2Pos
= availableSquares
[randIndex
].e
;
70 positions
.splice(availableSquares
[randIndex
].i
, 1);
72 if (knight1Pos
=== undefined) {
73 randIndex
= randInt(5);
74 knight1Pos
= positions
[randIndex
];
75 positions
.splice(randIndex
, 1);
77 randIndex
= randInt(4);
78 const knight2Pos
= positions
[randIndex
];
79 positions
.splice(randIndex
, 1);
81 randIndex
= randInt(3);
82 const queenPos
= positions
[randIndex
];
83 positions
.splice(randIndex
, 1);
85 const rook1Pos
= positions
[0];
86 const rook2Pos
= positions
[1];
88 pieces
[c
][rook1Pos
] = "r";
89 pieces
[c
][knight1Pos
] = "n";
90 pieces
[c
][bishop1Pos
] = "b";
91 pieces
[c
][queenPos
] = "q";
92 pieces
[c
][kingPos
] = "k";
93 pieces
[c
][bishop2Pos
] = "b";
94 pieces
[c
][knight2Pos
] = "n";
95 pieces
[c
][rook2Pos
] = "r";
98 pieces
["b"].join("") +
99 "/pppppppp/8/8/8/8/PPPPPPPP/" +
100 pieces
["w"].join("").toUpperCase() +
107 this.isAttacked(this.kingPos
[color
], 'w') ||
108 this.isAttacked(this.kingPos
[color
], 'b')