Commit | Line | Data |
---|---|---|
8f57fbf2 BA |
1 | import ChessRules from "/base_rules.js"; |
2 | import { ArrayFun } from "/utils/array.js"; | |
3 | import { Random } from "/utils/alea.js"; | |
4 | ||
5 | export default class GiveawayRules extends ChessRules { | |
6 | ||
7 | static get Options() { | |
8 | return { | |
9 | select: [ | |
10 | { | |
11 | label: "Mode", | |
12 | variable: "mode", | |
13 | defaut: "suicide", | |
14 | options: [ | |
15 | {label: "Suicide", value: "suicide"}, | |
16 | {label: "Losers", value: "losers"} | |
17 | ] | |
18 | } | |
19 | ].concat(C.Options.select), | |
20 | input: C.Options.input.filter(i => i.variable == "pawnfall"), | |
21 | styles: [ | |
22 | "atomic", "cannibal", "cylinder", "dark", | |
23 | "madrasi", "rifle", "teleport", "zen" | |
24 | ] | |
25 | }; | |
26 | } | |
27 | ||
28 | get hasFlags() { | |
29 | return this.options["mode"] == "losers"; | |
30 | } | |
31 | ||
32 | get pawnPromotions() { | |
33 | let res = ['q', 'r', 'n', 'b']; | |
34 | if (this.options["mode"] == "suicide") | |
35 | res.push('k'); | |
36 | return res; | |
37 | } | |
38 | ||
39 | genRandInitFen(seed) { | |
bc2bc396 BA |
40 | if (this.options["mode"] == "losers") |
41 | return super.genRandInitFen(seed); | |
42 | ||
8f57fbf2 BA |
43 | if (this.options["randomness"] == 0) { |
44 | return ( | |
45 | 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 {"enpassant":"-"}' | |
46 | ); | |
47 | } | |
48 | ||
49 | Random.setSeed(seed); | |
50 | let pieces = { w: new Array(8), b: new Array(8) }; | |
51 | for (let c of ["w", "b"]) { | |
52 | if (c == 'b' && this.options["randomness"] == 1) { | |
53 | pieces['b'] = pieces['w']; | |
54 | break; | |
55 | } | |
56 | ||
57 | // Get random squares for every piece, totally freely | |
58 | let positions = Random.shuffle(ArrayFun.range(8)); | |
59 | const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; | |
60 | const rem2 = positions[0] % 2; | |
61 | if (rem2 == positions[1] % 2) { | |
62 | // Fix bishops (on different colors) | |
63 | for (let i=2; i<8; i++) { | |
64 | if (positions[i] % 2 != rem2) { | |
65 | [positions[1], positions[i]] = [positions[i], positions[1]]; | |
66 | break; | |
67 | } | |
68 | } | |
69 | } | |
70 | for (let i = 0; i < 8; i++) | |
71 | pieces[c][positions[i]] = composition[i]; | |
72 | } | |
73 | return ( | |
74 | pieces["b"].join("") + | |
75 | "/pppppppp/8/8/8/8/PPPPPPPP/" + | |
76 | pieces["w"].join("").toUpperCase() + | |
77 | // En-passant allowed, but no flags | |
78 | ' w 0 {"enpassant":"-"}' | |
79 | ); | |
80 | } | |
81 | ||
82 | constructor(o) { | |
83 | o.options["capture"] = true; | |
84 | super(o); | |
85 | } | |
86 | ||
87 | underCheck([x, y], oppCol) { | |
88 | if (this.options["mode"] == "suicide") | |
89 | return false; | |
90 | return super.underCheck([x, y], oppCol); | |
91 | } | |
92 | ||
93 | getCurrentScore() { | |
94 | if (this.atLeastOneMove()) return "*"; | |
95 | // No valid move: the side who cannot move wins | |
96 | return (this.turn == "w" ? "1-0" : "0-1"); | |
97 | } | |
98 | ||
99 | }; |