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