-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeck.js
More file actions
50 lines (44 loc) · 1.43 KB
/
deck.js
File metadata and controls
50 lines (44 loc) · 1.43 KB
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
exports.Deck = (function() {
var Deck = function () {
this.cards = [];
this.suits = ['s','c', 'h', 'd'];
this.rank = ['A','2','3','4','5','6','7','8','9','10','J','Q','K','W','X'];
};
Deck.prototype = {
// reset the deck to its full, ordered state
renew: function() {
var i, j, m;
m = this.suits.length * this.rank.length;
this.cards = new Array( m );
for (i = 0; i < this.suits.length; i++) {
for (j = 0; j < this.rank.length; j++) {
this.cards[i * this.rank.length + j] = new Card(this.rank[j], this.suits[i]);
}
}
},
shuffle: function(times) {
var i, j, k;
var temp;
for (i = 0; i < times; i++) {
for (j = 0; j < this.cards.length; j++) {
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
}
},
deal: function(number) {
if (this.cards.length >= number) {
return this.cards.splice(0, number);
} else {
//overdrawn
}
}
};
return Deck;
}());
var Card = function(rank, suit) {
this.rank = rank;
this.suit = suit;
};