X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=variants%2FDice%2Fclass.js;fp=variants%2FDice%2Fclass.js;h=8c3eed4699dc0c6e9f890c084ba490d4987b13d4;hp=0000000000000000000000000000000000000000;hb=d01282a527e60af95f2a71deee1fbac9c0dd26be;hpb=5212758164eaa08e382b7bc281f87999c8af352b diff --git a/variants/Dice/class.js b/variants/Dice/class.js new file mode 100644 index 0000000..8c3eed4 --- /dev/null +++ b/variants/Dice/class.js @@ -0,0 +1,108 @@ +import ChessRules from "/base_rules.js"; +import {Random} from "/utils/alea.js"; + +export default class DiceRules extends ChessRules { + + static get Options() { + let res = C.Options; + res.select["defaut"] = 2; + return { + select: res.select, + input: [ + { + label: "Biased alea", + variable: "biased", + type: "checkbox", + defaut: true + }, + { + label: "Falling pawn", + variable: "pawnfall", + type: "checkbox", + defaut: false + } + ], + styles: [ + "atomic", + "capture", + "crazyhouse", + "cylinder", + "madrasi", + "recycle", + "rifle", + "zen" + ] + }; + } + + getPartFen(o) { + let toplay = ''; + if (o.init) { + let canMove = (this.options["biased"] + ? Array(8).fill('p').concat(Array(2).fill('n')) + : ['p', 'n']); + toplay = canMove[Random.randInt(canMove.length)]; + } + return Object.assign( + { toplay: (o.init ? toplay : this.getRandomPiece(this.turn)) }, + super.getPartFen(o) + ); + } + + constructor(o) { + super(o); + this.afterPlay = (move_s, newTurn, ops) => { + // Movestack contains only one move: + move_s[0].toplay = this.getRandomPiece(this.turn); + super.displayMessage(this.message, move_s[0].toplay); + o.afterPlay(move_s, newTurn, ops); + }; + } + + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); + this.toplay = fenParsed.toplay; + this.message = document.createElement("div"); + C.AddClass_es(this.message, "piece-text"); + this.message.innerHTML = this.toplay; + let container = document.getElementById(this.containerId); + container.appendChild(this.message); + } + + getRandomPiece(color) { + // Find pieces which can move and roll a (biased) dice + let canMove = []; + for (let i=0; i<8; i++) { + for (let j=0; j<8; j++) { + if (this.board[i][j] != "" && this.getColor(i, j) == color) { + const piece = this.getPiece(i, j); + if (this.findDestSquares([i, j], {one: true})) + canMove.push(piece); + } + } + } + if (!this.options["biased"]) + canMove = [...new Set(canMove)]; + return canMove[Random.randInt(canMove.length)]; + } + + postProcessPotentialMoves(moves) { + return super.postProcessPotentialMoves(moves).filter(m => { + return ( + (m.appear.length >= 1 && m.appear[0].p == this.toplay) || + (m.vanish.length >= 1 && m.vanish[0].p == this.toplay) + ); + }); + } + + filterValid(moves) { + return moves; + } + + playReceivedMove(moves, callback) { + this.toplay = moves[0].toplay; //only one move + super.displayMessage(this.message, this.toplay); + super.playReceivedMove(moves, callback); + } + +};