Skip to content

Instantly share code, notes, and snippets.

@bootcoder
Created April 22, 2015 01:10
Show Gist options
  • Save bootcoder/14e8338976ad070e9bcf to your computer and use it in GitHub Desktop.
Save bootcoder/14e8338976ad070e9bcf to your computer and use it in GitHub Desktop.
$(document).ready(function() {
var controller = new Controller;
$('#roller button.add').on('click', function() {
controller.add();
});
$('#roller button.roll').on('click', function() {
controller.roll();
});
});
var Controller = function() {
this.dice = [];
this.view = new View(this.dice);
this.add = function() {
this.dice.push(new Die);
this.view.reRender();
};
this.roll = function() {
for (i in this.dice) {
this.dice[i].roll();
};
this.view.reRender();
};
}
var View = function(dice) {
this.dice = dice;
this.reRender = function() {
$('.dice').text('');
for (i in this.dice) {
$('.dice').append('<div class="die">'+this.dice[i].side+'</div>');
};
};
};
var Die = function() {
this.side = 0;
this.roll = function() {
return this.side = Math.floor((Math.random()*6)+1);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment