-
Notifications
You must be signed in to change notification settings - Fork 1
/
CardDeck.js
217 lines (184 loc) · 5.28 KB
/
CardDeck.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var CardDeck = function (settings) {
// constructor
// defaults
var defaults = {
decks: 1,
handSize: 5,
ranks: "2,3,4,5,6,7,8,9,10,Jack,Queen,King,Ace".split(","),
suits: "Clubs,Diamonds,Hearts,Spades".split(","),
jokers: ["Black","Red"],
shuffle: true
};
this.theDeck = [];
this.settings = this.extend({}, defaults, settings);
return this.initialize();
};
CardDeck.prototype.initialize = function () {
this.populateDeck();
if (this.settings.shuffle) {
this.shuffle();
} else {
this.sort();
}
return this;
};
CardDeck.prototype.populateDeck = function() {
this.theDeck = [];
for (var i = 0; i < this.settings.decks; i++) {
var deck = this.createDeck(i);
this.theDeck = this.theDeck.concat(deck);
}
};
CardDeck.prototype.createDeck = function (deckID) {
deckID = deckID || 0;
var i;
var deck = [];
var decklength = this.settings.ranks.length * this.settings.suits.length;
for (i = 0; i < decklength; i += 1) {
var rank = i % this.settings.ranks.length;
var suit = i % this.settings.suits.length;
var card = new CardDeck.Card(rank, suit, this, deckID);
deck.push(card);
}
for (i = 0; i < this.settings.jokers.length; i++) {
deck.push(new CardDeck.Card(i, this.settings.suits.length, this, deckID));
}
return deck;
};
CardDeck.prototype.shuffle = function() {
// based on http://bost.ocks.org/mike/shuffle/
var m = this.theDeck.length;
var t, i;
// While there remain elements to shuffle
while (m) {
// Pick a remaining element
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = this.theDeck[m];
this.theDeck[m] = this.theDeck[i];
this.theDeck[i] = t;
}
return this;
};
CardDeck.prototype.sort = function () {
this.theDeck.sort(function deckSort(a, b) {
if (a.suitIndex !== b.suitIndex) {
return a.suitIndex - b.suitIndex; // > is T/F, T = 1, F = 0
} else {
// suits do match, so sort by rank
return a.rankIndex - b.rankIndex;
}
});
};
CardDeck.prototype.dealHand = function (size) {
size = size || this.settings.handSize;
var hand = new CardDeck.Hand(this);
for (var i = 0; i < size; i += 1) {
hand.addCard( this.theDeck.pop() );
}
return hand;
};
CardDeck.prototype.extend = function () {
for(var i=1; i<arguments.length; i++) {
for(var key in arguments[i]) {
if(arguments[i].hasOwnProperty(key)) {
arguments[0][key] = arguments[i][key];
}
}
}
return arguments[0];
};
CardDeck.Card = function (rankIndex, suitIndex, deck, deckID) {
var joker = suitIndex == deck.settings.suits.length;
this.rankIndex = rankIndex;
this.rank = (joker ? deck.settings.jokers[rankIndex] : deck.settings.ranks[rankIndex]) || "Blank";
this.suitIndex = suitIndex;
this.suit = deck.settings.suits[suitIndex] || (joker ? "Joker" : "");
this.deckID = deckID;
};
CardDeck.Card.prototype.toString = function() {
return this.rank + " " + this.suit;
};
CardDeck.Hand = function (deck) {
this.deck = deck;
this.theHand = [];
};
CardDeck.Hand.prototype.toString = function () {
return this.theHand.toString();
};
CardDeck.Hand.prototype.addCard = function (card) {
this.theHand.push(card);
};
CardDeck.Hand.prototype.isOfAKind = function (exactCount, excludeRankIndexes) {
// returns highest rankIndex when it finds exactCount;
excludeRankIndexes = excludeRankIndexes || [];
var i;
var tally = {};
for (i = 0; i < this.deck.settings.ranks.length; i += 1) {
tally[i] = 0;
}
for (i = 0; i < this.theHand.length; i += 1) {
var theCard = this.theHand[i];
if (excludeRankIndexes.indexOf(theCard.rankIndex)) {
tally[ this.deck.settings.ranks[ theCard.rankIndex ] ] += 1;
}
}
for (i = this.deck.settings.ranks.length-1; i >= 0 ; i -= 1) {
if (tally[ this.deck.settings.ranks[i] ] === exactCount) {
return i;
}
}
return false;
};
CardDeck.Hand.prototype.isAPair = function () {
return this.isOfAKind(2);
}
CardDeck.Hand.prototype.isAFlush = function () {
// all five cards must share the same suit.
var firstCard = this.theHand[0]
var firstSuit = firstCard.suitIndex;
for (var i = 1; i < this.theHand.length; i += 1) {
var card = this.theHand[i];
if (card.suitIndex !== firstSuit) return false;
}
return true;
}
CardDeck.Hand.prototype.isAFullHouse = function () {
if (this.theHand.length !== 5) throw new Error("isAFullHouse currently only supports hands with 5 cards.");
var three = this.isOfAKind(3);
if (three) {
return this.isOfAKind(2, [three]);
}
return false;
}
CardDeck.Hand.prototype.isAStraight = function () {
if (this.theHand.length !== 5) throw new Error("isAStraight currently only supports hands with 5 cards.");
var count = [];
for (var i = 0; i < this.deck.settings.ranks.length; i += 1) {
count[i] = 0;
}
// count suits
for (var i = 0; i < this.theHand.length; i += 1) {
var rank = this.theHand[i].rankIndex;
if ( count[rank] > 0) {
// found a pair, can't be a straight
return false;
}
count[rank] += 1;
}
for (var i = this.deck.settings.ranks.length; i >= 4; i -= 1) {
if (count[i] === 1 && count[i-1] === 1 && count[i-2] === 1 && count[i-3] === 1 && count[i-4] === 1) {
return i;
}
}
return false;
}
CardDeck.Hand.prototype.isAStraightFlush = function () {
if (this.isAFlush()) {
return this.isAStraight();
}
return false;
}
CardDeck.Hand.prototype.isARoyalFlush = function () {
return this.isAStraightFlush() === this.deck.settings.ranks.length-1;
}