Started code review + some fixes (unfinished)
[vchess.git] / client / src / variants / Upsidedown.js
1 import { ChessRules } from "@/base_rules";
2 import { randInt } from "@/utils/alea";
3 import { ArrayFun } from "@/utils/array";
4
5 export const VariantRules = class UpsidedownRules extends ChessRules {
6 static get HasFlags() {
7 return false;
8 }
9
10 static get HasEnpassant() {
11 return false;
12 }
13
14 getPotentialKingMoves(sq) {
15 // No castle
16 return this.getSlideNJumpMoves(
17 sq,
18 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
19 "oneStep"
20 );
21 }
22
23 static GenRandInitFen() {
24 let pieces = { w: new Array(8), b: new Array(8) };
25 for (let c of ["w", "b"]) {
26 let positions = ArrayFun.range(8);
27
28 let randIndex = randInt(8);
29 const kingPos = positions[randIndex];
30 positions.splice(randIndex, 1);
31
32 // At least a knight must be next to the king:
33 let knight1Pos = undefined;
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);
37 // Search for knight1Pos index in positions and remove it
38 const knight1Index = positions.indexOf(knight1Pos);
39 positions.splice(knight1Index, 1);
40
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];
46 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
47 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
48
49 randIndex = randInt(4);
50 const knight2Pos = positions[randIndex];
51 positions.splice(randIndex, 1);
52
53 randIndex = randInt(3);
54 const queenPos = positions[randIndex];
55 positions.splice(randIndex, 1);
56
57 const rook1Pos = positions[0];
58 const rook2Pos = positions[1];
59
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";
68 }
69 return (
70 pieces["w"].join("").toUpperCase() +
71 "/PPPPPPPP/8/8/8/8/pppppppp/" +
72 pieces["b"].join("") +
73 " w 0"
74 ); //no castle, no en-passant
75 }
76 };