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