-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.js
71 lines (60 loc) · 1.48 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Die
function Die(numSides) {
this.numSides = numSides;
this.value = 0;
}
Die.prototype.calcValue = function() {
this.value = Math.floor((Math.random()*this.numSides)+1);
}
//Board
function Board(view) {
this.view = view;
}
Board.prototype.drawDie = function() {
this.view.find('.dice').append('<div class="die">0</div>');
};
Board.prototype.drawAllDie = function(cup) {
this.view.find('.die').each(function(k, die) {
$(die).text(cup[k].value);
});
};
Board.prototype.setEventListenerForAdd = function (game) {
this.view.on('click', 'button.add', function() {
game.addDieToCup();
});
}
Board.prototype.setEventListenerForRoll = function (game) {
this.view.on('click', 'button.roll', function() {
game.rollAllDie();
});
}
//Game
function Game(view, numSides) {
this.cup = [];
this.board = new Board(view);
this.numSides = numSides;
// this.view = view;
}
Game.prototype.start = function() {
this.board.setEventListenerForAdd(this);
this.board.setEventListenerForRoll(this);
};
Game.prototype.addDieToCup = function() {
var die = new Die(this.numSides);
this.cup.push(die);
this.board.drawDie();
};
Game.prototype.rollAllDie = function() {
var self = this;
this.cup.forEach(function(die,k) {
die.calcValue();
});
self.board.drawAllDie(this.cup);
};
$(document).ready(function() {
numSides = 6;
var game = new Game($('#roller'), numSides);
game.start();
var otherGame = new Game($('#other_roller'), 12);
otherGame.start();
});