7198c0b1e98df5c4f25a71b7b22afc03a0103752
[vchess.git] / client / src / variants / Upsidedown.js
1 class UpsidedownRules extends ChessRules
2 {
3 static get HasFlags() { return false; }
4
5 static get HasEnpassant() { return false; }
6
7 getPotentialKingMoves(sq)
8 {
9 // No castle
10 return this.getSlideNJumpMoves(sq,
11 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
12 }
13
14 static GenRandInitFen()
15 {
16 let pieces = { "w": new Array(8), "b": new Array(8) };
17 for (let c of ["w","b"])
18 {
19 let positions = range(8);
20
21 let randIndex = random(8);
22 const kingPos = positions[randIndex];
23 positions.splice(randIndex, 1);
24
25 // At least a knight must be next to the king:
26 let knight1Pos = undefined;
27 if (kingPos == 0)
28 knight1Pos = 1;
29 else if (kingPos == V.size.y-1)
30 knight1Pos = V.size.y-2;
31 else
32 knight1Pos = kingPos + (Math.random() < 0.5 ? 1 : -1);
33 // Search for knight1Pos index in positions and remove it
34 const knight1Index = positions.indexOf(knight1Pos);
35 positions.splice(knight1Index, 1);
36
37 // King+knight1 are on two consecutive squares: one light, one dark
38 randIndex = 2 * random(3);
39 const bishop1Pos = positions[randIndex];
40 let randIndex_tmp = 2 * random(3) + 1;
41 const bishop2Pos = positions[randIndex_tmp];
42 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
43 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
44
45 randIndex = random(4);
46 const knight2Pos = positions[randIndex];
47 positions.splice(randIndex, 1);
48
49 randIndex = random(3);
50 const queenPos = positions[randIndex];
51 positions.splice(randIndex, 1);
52
53 const rook1Pos = positions[0];
54 const rook2Pos = positions[1];
55
56 pieces[c][rook1Pos] = 'r';
57 pieces[c][knight1Pos] = 'n';
58 pieces[c][bishop1Pos] = 'b';
59 pieces[c][queenPos] = 'q';
60 pieces[c][kingPos] = 'k';
61 pieces[c][bishop2Pos] = 'b';
62 pieces[c][knight2Pos] = 'n';
63 pieces[c][rook2Pos] = 'r';
64 }
65 return pieces["w"].join("").toUpperCase() +
66 "/PPPPPPPP/8/8/8/8/pppppppp/" +
67 pieces["b"].join("") +
68 " w"; //no castle, no en-passant
69 }
70 }