-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.js
98 lines (79 loc) · 1.88 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(function () {
this.Deck = function (options) {
var settings = {
decks: 1,
jokers: 0
}
var stack = [];
var usedCards = [];
// set settings passed by the user
if (options) {
// loop attributes in object 'options'
for (var attribute in options) {
if (options.hasOwnProperty(attribute)) {
settings[attribute] = options[attribute];
}
}
}
for (var i = 0; i < settings.decks; i++) {
buildDeck();
}
addJokers(settings.jokers);
function buildDeck() {
for (var i = 0; i < 52; i++) {
var rank = i % 13 + 1;
var suit;
if (i < 13) {
suit = "Spades";
} else if (i < 26) {
suit = "Hearts";
} else if (i < 39) {
suit = "Diamonds";
} else {
suit = "Clubs";
}
var card = new Card(rank, suit);
stack.push(card);
}
}
function addJokers(n) {
for (var i = 0; i < n; i++) {
var joker = new Card(0, "Joker");
stack.push(joker);
}
}
function stringify(array) {
var string = "";
array.forEach(function(card) {
string += card.literal() + "\n";
});
return string;
}
function currentStack() {
return stringify(stack);
}
function listUsedCards() {
return stringify(used);
}
function deal(num) {
var hand = [];
for (var i = 0; i < num; i++) {
var card = stack.pop();
usedCards.push(card);
hand.push(card);
}
return hand;
}
function shuffle() {
var o = stack;
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
};
return {
stack: stack,
currentStack: currentStack,
usedCards: listUsedCards,
shuffle: shuffle,
deal: deal
}
}
})();