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