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