Created
April 21, 2015 21:57
-
-
Save bootcoder/19b89f7a7ddf7a3743d2 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() { | |
// Die model | |
var Die = function(id) { | |
this.value = Math.floor((Math.random()*6)+1); | |
this.id = id; | |
} | |
Die.prototype.roll = function() { | |
this.value = Math.floor((Math.random()*6)+1); | |
} | |
// Dice game controller | |
var DiceController = function() { | |
this.view = new View(); | |
this.dieId = 0; | |
this.dice = []; | |
this.view.$add.on('click', this.addDie.bind(this)); | |
this.view.$roll.on('click', this.rollDice.bind(this)); | |
} | |
DiceController.prototype.addDie = function() { | |
this.dieId++; | |
var newDie = new Die(this.dieId); | |
this.dice.push(newDie); | |
this.view.append(newDie); | |
} | |
DiceController.prototype.rollDice = function() { | |
for (var i = 0; i < this.dice.length; i++) { | |
var currentDie = this.dice[i]; | |
currentDie.roll(); | |
this.view.updateDieValue(currentDie); | |
} | |
} | |
// View | |
var View = function() { | |
this.$add = $('#roller button.add'); | |
this.$roll = $('#roller button.roll'); | |
this.$dice = $('.dice'); | |
} | |
View.prototype.append = function(die) { | |
this.$dice.append('<div class="die" id="' + die.id +'">' + die.value + '</div>'); | |
} | |
View.prototype.updateDieValue = function(die) { | |
this.$dice.find('#' + die.id)[0].innerHTML = die.value; | |
} | |
var game = new DiceController(); | |
// $('#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