Created
April 22, 2015 01:10
-
-
Save bootcoder/14e8338976ad070e9bcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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