Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
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 | |
388e4c40 | 6 | { |
69f3d801 | 7 | static get HasFlags() { return false; } |
388e4c40 | 8 | |
69f3d801 | 9 | static get HasEnpassant() { return false; } |
f6dbe8e3 | 10 | |
388e4c40 BA |
11 | getPotentialKingMoves(sq) |
12 | { | |
13 | // No castle | |
14 | return this.getSlideNJumpMoves(sq, | |
15 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep"); | |
16 | } | |
17 | ||
18 | static GenRandInitFen() | |
19 | { | |
20 | let pieces = { "w": new Array(8), "b": new Array(8) }; | |
21 | for (let c of ["w","b"]) | |
22 | { | |
0c3fe8a6 | 23 | let positions = ArrayFun.range(8); |
388e4c40 | 24 | |
656b1878 | 25 | let randIndex = randInt(8); |
26c1e3bd BA |
26 | const kingPos = positions[randIndex]; |
27 | positions.splice(randIndex, 1); | |
28 | ||
29 | // At least a knight must be next to the king: | |
30 | let knight1Pos = undefined; | |
31 | if (kingPos == 0) | |
32 | knight1Pos = 1; | |
33 | else if (kingPos == V.size.y-1) | |
34 | knight1Pos = V.size.y-2; | |
35 | else | |
0c3fe8a6 | 36 | knight1Pos = kingPos + (Math.random() < 0.5 ? 1 : -1); |
26c1e3bd BA |
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 | |
656b1878 | 42 | randIndex = 2 * randInt(3); |
26c1e3bd | 43 | const bishop1Pos = positions[randIndex]; |
656b1878 | 44 | let randIndex_tmp = 2 * randInt(3) + 1; |
26c1e3bd | 45 | const bishop2Pos = positions[randIndex_tmp]; |
388e4c40 BA |
46 | positions.splice(Math.max(randIndex,randIndex_tmp), 1); |
47 | positions.splice(Math.min(randIndex,randIndex_tmp), 1); | |
48 | ||
656b1878 | 49 | randIndex = randInt(4); |
26c1e3bd | 50 | const knight2Pos = positions[randIndex]; |
388e4c40 BA |
51 | positions.splice(randIndex, 1); |
52 | ||
656b1878 | 53 | randIndex = randInt(3); |
26c1e3bd | 54 | const queenPos = positions[randIndex]; |
388e4c40 BA |
55 | positions.splice(randIndex, 1); |
56 | ||
26c1e3bd BA |
57 | const rook1Pos = positions[0]; |
58 | const rook2Pos = positions[1]; | |
388e4c40 BA |
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 | } | |
f6dbe8e3 | 69 | return pieces["w"].join("").toUpperCase() + |
388e4c40 | 70 | "/PPPPPPPP/8/8/8/8/pppppppp/" + |
f6dbe8e3 | 71 | pieces["b"].join("") + |
0c3fe8a6 | 72 | " w 0"; //no castle, no en-passant |
388e4c40 BA |
73 | } |
74 | } |