-
Notifications
You must be signed in to change notification settings - Fork 1
/
Deck.js
57 lines (47 loc) · 1.41 KB
/
Deck.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
var Card = require('./Card.js').Card,
_ = require('underscore');
this.Deck = function() {
this.cards = [];
function init() {
for (var shape = 0; shape < 3; shape += 1) {
for (var color = 0; color < 3; color += 1) {
for (var shading = 0; shading < 3; shading += 1) {
for (var number = 0; number < 3; number += 1) {
this.cards.push(new Card(shape, color, shading, number));
}
}
}
}
this.shuffle();
}
this.isEmpty = function() {
return this.cards.length === 0;
};
this.numCards = function() {
return this.cards.length;
};
this.addCard = function(card) {
this.cards.push(card);
};
this.drawCard = function() {
var numCards = this.cards.length,
i;
if (numCards > 0) {
i = Math.floor(Math.random() * numCards);
return this.cards.splice(i,1)[0];
}
return null;
};
this.drawSpecificCard = function(card) {
for (var i = 0, n = this.cards.length; i < n; i += 1) {
if (Card.equals(this.cards[i], card)) {
return this.cards.splice(i,1)[0];
}
}
return null;
}
this.shuffle = function() {
this.cards = _.shuffle(this.cards);
};
init.apply(this, arguments);
};