Skip to content

Instantly share code, notes, and snippets.

@bootcoder
Created April 22, 2015 01:12
Show Gist options
  • Save bootcoder/880089db584b7348ba6e to your computer and use it in GitHub Desktop.
Save bootcoder/880089db584b7348ba6e to your computer and use it in GitHub Desktop.
/*jshint strict: false */
/*global $:false, undef: true */
/*jshint unused: false, undef:false */
$(document).ready(function(){
//Model
var Die = function (id) {
this.side = 0;
this.id = id;
};
Die.prototype.roll = function(){
this.side = Math.floor((Math.random()*6)+1);
};
//Controller
var Dice = function(){
this.display = new Display();
this.dice = [];
this.dieId = 0;
this.display.$add.click(this.addDie.bind(this));
this.display.$roll.click(this.rollDice.bind(this));
};
Dice.prototype =
{
addDie: function(){
this.dieId++;
var newDie = new Die(this.dieId);
this.dice.push(newDie);
this.display.append(newDie);
},
rollDice: function(){
for(var x = 0; x < this.dice.length; x++){
var singleDie = this.dice[x];
singleDie.roll();
this.display.findDie(singleDie);
}
}
};
//Display
var Display = function(){
this.$add = $('.add');
this.$roll = $('.roll');
this.$dice = $('.dice');
};
Display.prototype =
{
append: function(die){
this.$dice.append('<div class="die" id="'+die.id+'">' + die.side + '</div>');
},
findDie: function(die){
this.$dice.find('#' + die.id)[0].innerHTML = die.side;
}
};
var game = new Dice();
});
// $(document).ready(function() {
// $('#roller button.add').on('click', function() {
// console.log("WAT")
// $('.dice').append('<div class="die">0</div>');
// });
// $('#roller button.roll').on('click', function() {
// $('.die').each(function(k, die) {
// var value = Math.floor((Math.random()*6)+1);
// $(die).text(value);
// });
// });
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment