Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Die(sides) {
- if (sides == undefined) {
- this.sides = 6;
- } else {
- this.sides = sides;
- }
- }
- Die.prototype.roll = function() {
- if (this.held === 1) {
- return this.latestRoll;
- } else {
- this.latestRoll = Math.floor((Math.random() * this.sides) + 1);
- return this.latestRoll;
- }
- };
- Die.prototype.toggleHold = function() {
- if (this.held === 1) {
- this.held = 0;
- } else {
- this.held = 1;
- }
- return this.held;
- };
- var singleDie = new Die();
- var dicebag = {
- dice: [], //array with all dice ever
- addDie: function(sides) { //adds die to end
- this.dice.push(new Die(sides));
- return this.dice;
- },
- removeDie: function(index) { //removes die at index
- this.dice.pop(this.dice[index]);
- return this.dice;
- },
- rollAll: function() { //rolls all dice
- var result = [];
- for (var i = 0; i < this.dice.length; i++) {
- result.push(this.dice[i].roll());
- }
- this.latestResult = result;
- return result;
- },
- sumLastRes: function() {
- var sum = 0;
- for (var i = 0; i < this.latestResult.length; i++) {
- sum += this.latestResult[i];
- }
- return sum;
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement