Thursday 24 November 2016

Zombie Dice Code

This is the code after the Cup and Dice were classified and generated. The code works perfectly at this stage.

// This is the code for my first zombie game
// This is the code for the Dice class of objects

class Dice {
 
  // constructor method will create new Dice objects
 
  constructor(colour,faces) {
    this.colour = colour;
    this.faces = faces;
  }
 
  rollDie() {
    // generate a random number between 1 and 6
    var randomNumber = Math.floor((Math.random() * 6) + 1);
    // pick a face using the random number we generated
    var face = this.faces[randomNumber - 1];
    return face;
  }
 
  // helper method for getting face value
 
  face() {
    var face = this.rollDie();
    return face;
  }
}

class Cup {
 
  //constructor method to create a new cup
 
  constructor(r,y,g) {
    this.red = r;
    this.yellow = y;
    this.green = g;
    this.dice = new Array();
  }
 
  // create methods to generate new dice
 
  newRedDie() {
    var newDie = new Dice("red",["shotgun","shotgun","shotgun","feet","feet","brains"]);
    return newDie;
  }
  newYellowDie() {
    var newDie = new Dice ("yellow",["shotgun","shotgun","feet","feet","brains","brains"]);
    return newDie;
  }
  newGreenDie() {
    var newDie = new Dice ("green",["shotgun",,"feet","feet","brains","brains","brains"]);
    return newDie;
  }
setupCup() {
    // loop through the value of this.red to make all the red dice
  for (var i = 0; i < this.red; i++) {
    var newRedDie = this.newRedDie();
    this.dice.push(newRedDie);
    }
  for (var i = 0; i < this.yellow; i++) {
    var newYellowDie = this.newYellowDie();
    this.dice.push(newYellowDie);
    }
  for (var i = 0; i < this.green; i++) {
    var newGreenDie = this.newGreenDie();
    this.dice.push(newGreenDie);
    }
  }
}
// This is where code will run
var myCup = new Cup(3,4,6);
myCup.setupCup();

document.write(myCup.dice[0].colour);

No comments:

Post a Comment