Commit | Line | Data |
---|---|---|
d01282a5 BA |
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); | |
7fbcb53d BA |
57 | super.displayMessage( |
58 | this.message, "To play: " + move_s[0].toplay.toUpperCase()); | |
d01282a5 BA |
59 | o.afterPlay(move_s, newTurn, ops); |
60 | }; | |
61 | } | |
62 | ||
63 | setOtherVariables(fenParsed) { | |
64 | super.setOtherVariables(fenParsed); | |
65 | this.toplay = fenParsed.toplay; | |
66 | this.message = document.createElement("div"); | |
67 | C.AddClass_es(this.message, "piece-text"); | |
7fbcb53d | 68 | this.message.innerHTML = "To play: " + this.toplay.toUpperCase(); |
d01282a5 BA |
69 | let container = document.getElementById(this.containerId); |
70 | container.appendChild(this.message); | |
71 | } | |
72 | ||
73 | getRandomPiece(color) { | |
74 | // Find pieces which can move and roll a (biased) dice | |
75 | let canMove = []; | |
76 | for (let i=0; i<8; i++) { | |
77 | for (let j=0; j<8; j++) { | |
78 | if (this.board[i][j] != "" && this.getColor(i, j) == color) { | |
79 | const piece = this.getPiece(i, j); | |
80 | if (this.findDestSquares([i, j], {one: true})) | |
81 | canMove.push(piece); | |
82 | } | |
83 | } | |
84 | } | |
85 | if (!this.options["biased"]) | |
86 | canMove = [...new Set(canMove)]; | |
87 | return canMove[Random.randInt(canMove.length)]; | |
88 | } | |
89 | ||
90 | postProcessPotentialMoves(moves) { | |
91 | return super.postProcessPotentialMoves(moves).filter(m => { | |
92 | return ( | |
93 | (m.appear.length >= 1 && m.appear[0].p == this.toplay) || | |
94 | (m.vanish.length >= 1 && m.vanish[0].p == this.toplay) | |
95 | ); | |
96 | }); | |
97 | } | |
98 | ||
99 | filterValid(moves) { | |
100 | return moves; | |
101 | } | |
102 | ||
103 | playReceivedMove(moves, callback) { | |
104 | this.toplay = moves[0].toplay; //only one move | |
7fbcb53d BA |
105 | super.displayMessage( |
106 | this.message, "To play: " + this.toplay.toUpperCase()); | |
d01282a5 BA |
107 | super.playReceivedMove(moves, callback); |
108 | } | |
109 | ||
110 | }; |