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