8c3eed4699dc0c6e9f890c084ba490d4987b13d4
[xogo.git] / variants / Dice / class.js
1 import ChessRules from "/base_rules.js";
2 import {Random} from "/utils/alea.js";
3
4 export default class DiceRules extends ChessRules {
5
6 static get Options() {
7 let res = C.Options;
8 res.select["defaut"] = 2;
9 return {
10 select: res.select,
11 input: [
12 {
13 label: "Biased alea",
14 variable: "biased",
15 type: "checkbox",
16 defaut: true
17 },
18 {
19 label: "Falling pawn",
20 variable: "pawnfall",
21 type: "checkbox",
22 defaut: false
23 }
24 ],
25 styles: [
26 "atomic",
27 "capture",
28 "crazyhouse",
29 "cylinder",
30 "madrasi",
31 "recycle",
32 "rifle",
33 "zen"
34 ]
35 };
36 }
37
38 getPartFen(o) {
39 let toplay = '';
40 if (o.init) {
41 let canMove = (this.options["biased"]
42 ? Array(8).fill('p').concat(Array(2).fill('n'))
43 : ['p', 'n']);
44 toplay = canMove[Random.randInt(canMove.length)];
45 }
46 return Object.assign(
47 { toplay: (o.init ? toplay : this.getRandomPiece(this.turn)) },
48 super.getPartFen(o)
49 );
50 }
51
52 constructor(o) {
53 super(o);
54 this.afterPlay = (move_s, newTurn, ops) => {
55 // Movestack contains only one move:
56 move_s[0].toplay = this.getRandomPiece(this.turn);
57 super.displayMessage(this.message, move_s[0].toplay);
58 o.afterPlay(move_s, newTurn, ops);
59 };
60 }
61
62 setOtherVariables(fenParsed) {
63 super.setOtherVariables(fenParsed);
64 this.toplay = fenParsed.toplay;
65 this.message = document.createElement("div");
66 C.AddClass_es(this.message, "piece-text");
67 this.message.innerHTML = this.toplay;
68 let container = document.getElementById(this.containerId);
69 container.appendChild(this.message);
70 }
71
72 getRandomPiece(color) {
73 // Find pieces which can move and roll a (biased) dice
74 let canMove = [];
75 for (let i=0; i<8; i++) {
76 for (let j=0; j<8; j++) {
77 if (this.board[i][j] != "" && this.getColor(i, j) == color) {
78 const piece = this.getPiece(i, j);
79 if (this.findDestSquares([i, j], {one: true}))
80 canMove.push(piece);
81 }
82 }
83 }
84 if (!this.options["biased"])
85 canMove = [...new Set(canMove)];
86 return canMove[Random.randInt(canMove.length)];
87 }
88
89 postProcessPotentialMoves(moves) {
90 return super.postProcessPotentialMoves(moves).filter(m => {
91 return (
92 (m.appear.length >= 1 && m.appear[0].p == this.toplay) ||
93 (m.vanish.length >= 1 && m.vanish[0].p == this.toplay)
94 );
95 });
96 }
97
98 filterValid(moves) {
99 return moves;
100 }
101
102 playReceivedMove(moves, callback) {
103 this.toplay = moves[0].toplay; //only one move
104 super.displayMessage(this.message, this.toplay);
105 super.playReceivedMove(moves, callback);
106 }
107
108 };