Fix Koopa promotions after stunning pieces
[vchess.git] / client / src / variants / Upsidedown.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2import { randInt } from "@/utils/alea";
3import { ArrayFun } from "@/utils/array";
4
32f6285e 5export class UpsidedownRules extends ChessRules {
6808d7a1
BA
6 static get HasFlags() {
7 return false;
8 }
388e4c40 9
c583ef1c
BA
10 static get HasEnpassant() {
11 return false;
dac39588 12 }
388e4c40 13
7ba4a5bc 14 static GenRandInitFen(randomness) {
7ba4a5bc
BA
15 if (randomness == 0)
16 return "RNBQKBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbqkbnr w 0";
17
6808d7a1
BA
18 let pieces = { w: new Array(8), b: new Array(8) };
19 for (let c of ["w", "b"]) {
7ba4a5bc
BA
20 if (c == 'b' && randomness == 1) {
21 pieces['b'] = pieces['w'];
22 break;
23 }
24
dac39588 25 let positions = ArrayFun.range(8);
388e4c40 26
dac39588
BA
27 let randIndex = randInt(8);
28 const kingPos = positions[randIndex];
29 positions.splice(randIndex, 1);
26c1e3bd 30
dac39588
BA
31 // At least a knight must be next to the king:
32 let knight1Pos = undefined;
6808d7a1
BA
33 if (kingPos == 0) knight1Pos = 1;
34 else if (kingPos == V.size.y - 1) knight1Pos = V.size.y - 2;
35 else knight1Pos = kingPos + (Math.random() < 0.5 ? 1 : -1);
dac39588
BA
36 // Search for knight1Pos index in positions and remove it
37 const knight1Index = positions.indexOf(knight1Pos);
38 positions.splice(knight1Index, 1);
26c1e3bd 39
dac39588
BA
40 // King+knight1 are on two consecutive squares: one light, one dark
41 randIndex = 2 * randInt(3);
42 const bishop1Pos = positions[randIndex];
43 let randIndex_tmp = 2 * randInt(3) + 1;
44 const bishop2Pos = positions[randIndex_tmp];
6808d7a1
BA
45 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
46 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
388e4c40 47
dac39588
BA
48 randIndex = randInt(4);
49 const knight2Pos = positions[randIndex];
50 positions.splice(randIndex, 1);
388e4c40 51
dac39588
BA
52 randIndex = randInt(3);
53 const queenPos = positions[randIndex];
54 positions.splice(randIndex, 1);
388e4c40 55
dac39588
BA
56 const rook1Pos = positions[0];
57 const rook2Pos = positions[1];
388e4c40 58
6808d7a1
BA
59 pieces[c][rook1Pos] = "r";
60 pieces[c][knight1Pos] = "n";
61 pieces[c][bishop1Pos] = "b";
62 pieces[c][queenPos] = "q";
63 pieces[c][kingPos] = "k";
64 pieces[c][bishop2Pos] = "b";
65 pieces[c][knight2Pos] = "n";
66 pieces[c][rook2Pos] = "r";
dac39588 67 }
6808d7a1
BA
68 return (
69 pieces["w"].join("").toUpperCase() +
dac39588
BA
70 "/PPPPPPPP/8/8/8/8/pppppppp/" +
71 pieces["b"].join("") +
0d5335de 72 // No castle, no en-passant:
6808d7a1 73 " w 0"
0d5335de 74 );
dac39588 75 }
b83a675a
BA
76
77 static get SEARCH_DEPTH() {
78 return 2;
79 }
6808d7a1 80};