-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.js
67 lines (57 loc) · 1.61 KB
/
cards.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
var card = (function() {
function card(value, suit) {
this.value = value;
this.suit = suit;
}
return card;
}());
var deck = (function() {
var cards = [];
var numCardsLeft = 0;
function deck() {
this.getNumLeft = function() {
return numCardsLeft;
};
this.get = function() {
return cards;
}
this.create = function() {
cards = [];
var suitArray = ["spade", "club", "heart", "diamond"];
var valueArray = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"];
suitArray.forEach(function(suit) {
valueArray.forEach(function(value) {
cards.push(new card(value, suit));
numCardsLeft++;
});
});
this.shuffle = function() {
var currentIndex = cards.length, temporaryValue, randomIndex;
while(0 !== currentIndex) {
//get a random value to switch
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
//perform the switch
temporaryValue = cards[currentIndex];
cards[currentIndex] = cards[randomIndex];
cards[randomIndex] = temporaryValue;
}
}
this.cut = function() {
for(cardsCtr = 0; cardsCtr < Math.floor(cards.length/2); cardsCtr++) {
cards.push(cards.shift());
}
}
this.deal = function() {
numCardsLeft--;
return cards.shift();
}
this.dealRandom = function() {
numCardsLeft--;
randomIndex = Math.floor(Math.random() * cards.length);
return cards.splice(randomIndex, 1);
}
};
}
return deck;
}());