Draft Bario (unfinished)
[xogo.git] / variants / Giveaway / class.js
CommitLineData
8f57fbf2 1import ChessRules from "/base_rules.js";
f3e90e30
BA
2import {ArrayFun} from "/utils/array.js";
3import {Random} from "/utils/alea.js";
8f57fbf2
BA
4
5export 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
f31de5e4 39 genRandInitBaseFen() {
bc2bc396 40 if (this.options["mode"] == "losers")
f31de5e4 41 return super.genRandInitBaseFen();
bc2bc396 42
f31de5e4
BA
43 let fen = "";
44 if (this.options["randomness"] == 0)
dc10e429 45 fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
f31de5e4
BA
46 else {
47 let pieces = { w: new Array(8), b: new Array(8) };
48 for (let c of ["w", "b"]) {
49 if (c == 'b' && this.options["randomness"] == 1) {
50 pieces['b'] = pieces['w'];
51 break;
52 }
53 // Get random squares for every piece, totally freely
54 let positions = Random.shuffle(ArrayFun.range(8));
55 const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
56 const rem2 = positions[0] % 2;
57 if (rem2 == positions[1] % 2) {
58 // Fix bishops (on different colors)
59 for (let i=2; i<8; i++) {
60 if (positions[i] % 2 != rem2) {
61 [positions[1], positions[i]] = [positions[i], positions[1]];
62 break;
63 }
8f57fbf2
BA
64 }
65 }
f31de5e4
BA
66 for (let i = 0; i < 8; i++)
67 pieces[c][positions[i]] = composition[i];
8f57fbf2 68 }
f31de5e4
BA
69 fen = (
70 pieces["b"].join("") +
71 "/pppppppp/8/8/8/8/PPPPPPPP/" +
dc10e429 72 pieces["w"].join("").toUpperCase()
f31de5e4 73 );
8f57fbf2 74 }
f31de5e4 75 return { fen: fen, o: {} };
8f57fbf2
BA
76 }
77
78 constructor(o) {
79 o.options["capture"] = true;
80 super(o);
81 }
82
f3e90e30 83 underCheck(square, oppCol) {
8f57fbf2
BA
84 if (this.options["mode"] == "suicide")
85 return false;
f3e90e30 86 return super.underCheck(square, oppCol);
8f57fbf2
BA
87 }
88
89 getCurrentScore() {
f31de5e4
BA
90 if (this.atLeastOneMove(this.turn))
91 return "*";
8f57fbf2
BA
92 // No valid move: the side who cannot move wins
93 return (this.turn == "w" ? "1-0" : "0-1");
94 }
95
96};