New variant idea
[xogo.git] / variants / Dice / class.js
CommitLineData
d01282a5
BA
1import ChessRules from "/base_rules.js";
2import {Random} from "/utils/alea.js";
3
4export 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);
04d93b7b
BA
57 this.toplay = move_s[0].toplay;
58 this.displayMessage(move_s[0].toplay,
59 C.GetOppTurn(move_s[0].appear[0].c));
d01282a5
BA
60 o.afterPlay(move_s, newTurn, ops);
61 };
62 }
63
04d93b7b 64 displayMessage(piece, color) {
03883a0d
BA
65 if (color == 'b') {
66 const blackPieceToCode = {
67 'k': 'l',
68 'p': 'o',
69 'n': 'm',
70 'b': 'v',
71 'q': 'w',
72 'r': 't'
73 };
74 piece = blackPieceToCode[piece];
75 }
04d93b7b
BA
76 super.displayMessage(this.message,
77 '<span>to play:</span> ' +
03883a0d 78 '<span class="symb">' + piece + '</span>'
04d93b7b
BA
79 );
80 }
81
d01282a5
BA
82 setOtherVariables(fenParsed) {
83 super.setOtherVariables(fenParsed);
84 this.toplay = fenParsed.toplay;
85 this.message = document.createElement("div");
86 C.AddClass_es(this.message, "piece-text");
d01282a5
BA
87 let container = document.getElementById(this.containerId);
88 container.appendChild(this.message);
04d93b7b 89 this.displayMessage(this.toplay, fenParsed.turn);
d01282a5
BA
90 }
91
92 getRandomPiece(color) {
93 // Find pieces which can move and roll a (biased) dice
94 let canMove = [];
95 for (let i=0; i<8; i++) {
96 for (let j=0; j<8; j++) {
97 if (this.board[i][j] != "" && this.getColor(i, j) == color) {
98 const piece = this.getPiece(i, j);
99 if (this.findDestSquares([i, j], {one: true}))
100 canMove.push(piece);
101 }
102 }
103 }
104 if (!this.options["biased"])
105 canMove = [...new Set(canMove)];
106 return canMove[Random.randInt(canMove.length)];
107 }
108
109 postProcessPotentialMoves(moves) {
110 return super.postProcessPotentialMoves(moves).filter(m => {
111 return (
112 (m.appear.length >= 1 && m.appear[0].p == this.toplay) ||
113 (m.vanish.length >= 1 && m.vanish[0].p == this.toplay)
114 );
115 });
116 }
117
118 filterValid(moves) {
119 return moves;
120 }
121
122 playReceivedMove(moves, callback) {
123 this.toplay = moves[0].toplay; //only one move
04d93b7b 124 this.displayMessage(this.toplay, C.GetOppTurn(moves[0].appear[0].c));
d01282a5
BA
125 super.playReceivedMove(moves, callback);
126 }
127
128};