/
deck.js
283 lines (260 loc) · 7.82 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @module deck
*/
'use strict';
var isArray = require( './lib/is-array' );
// Get a random integer index within the provided array
function randomIndex( arr ) {
return Math.floor( Math.random() * arr.length );
}
// Predicate function to filter out undefined values
function isNotUndefined( val ) {
return typeof val !== 'undefined';
}
// Shuffle an array in place, returning that array
function shuffle( arr ) {
// Fisher–Yates implementation adapted from http://bost.ocks.org/mike/shuffle/
var remaining = arr.length;
var tmp;
var idx;
// While there remain elements to shuffle…
while ( remaining ) {
// Pick a remaining element...
idx = Math.floor( Math.random() * remaining-- );
// And swap it with the current element.
tmp = arr[ remaining ];
arr[ remaining ] = arr[ idx ];
arr[ idx ] = tmp;
}
}
/**
* @class Deck
*/
function Deck( arr ) {
if ( isArray( arr ) ) {
this.cards( arr );
}
}
/**
* Populate the deck with an array of cards, wiping out any cards that had
* previously been added to the deck
*
* @chainable
* @param {Array} cardArray An array of cards to use for the deck
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.cards = function( cardArray ) {
if ( ! isArray( cardArray ) ) { return this; }
// Replace the deck with the new cards
this._stack = cardArray;
return this;
};
/**
* Randomize the order of cards within the deck
*
* @chainable
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.shuffle = function() {
shuffle( this._stack );
return this;
};
/**
* Get the number of cards currently contained within the deck
*
* @return {Number} The number of cards left in the deck
*/
Deck.prototype.remaining = function() {
return this._stack.length;
};
/**
* Draw a card or cards, removing the drawn cards from the deck
*
* @param {Number} [count] The number of cards to draw
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.draw = function( count ) {
count || ( count = 1 );
var drawnCards = this._stack.splice( 0, count );
if ( ! drawnCards.length ) { return; }
return count === 1 ? drawnCards[ 0 ] : drawnCards;
};
/**
* Draw a card or cards from the bottom of the deck, removing the drawn cards
* from the deck
*
* @param {Number} [count] The number of cards to draw
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.drawFromBottom = function( count ) {
count || ( count = 1 );
var drawnCards = this._stack.splice( -count, count ).reverse();
if ( ! drawnCards.length ) { return; }
return count === 1 ? drawnCards[ 0 ] : drawnCards;
};
/**
* Draw a card or cards matching a condition defined in a provided predicate
* function, removing the drawn cards from the deck
*
* @param {Function} predicate A function to use to evaluate whether a given
* card in the deck should be drawn
* @param {Number} [count] The number of cards to draw
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.drawWhere = function( predicate, count ) {
if ( typeof predicate !== 'function' ) {
return;
}
count || ( count = 1 );
var drawnCards = this._stack.filter( predicate ).slice( 0, count );
for ( var i = 0; i < drawnCards.length; i++ ) {
// Remove from the stack
this._stack.splice( this._stack.indexOf( drawnCards[ i ] ), 1 );
}
if ( ! drawnCards.length ) { return; }
return count === 1 ? drawnCards[ 0 ] : drawnCards;
};
/**
* Draw a card or cards from random positions in the deck, removing the drawn
* cards from the deck
*
* @param {Number} [count] The number of cards to draw
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.drawRandom = function( count ) {
if ( ! this._stack.length ) { return; }
count || ( count = 1 );
if ( count === 1 ) {
return this._stack.splice( randomIndex( this._stack ), 1 )[ 0 ];
}
var drawnCards = [];
for ( var i = 0; i < count; i++ ) {
drawnCards.push( this._stack.splice( randomIndex( this._stack ), 1 )[ 0 ] );
}
drawnCards = drawnCards.filter( isNotUndefined );
return drawnCards;
};
/**
* Insert a card or cards at the bottom of the deck in order
*
* @chainable
* @param {Object|Array} cards The card object or array of card objects to insert
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.addToBottom = function( cards ) {
if ( ! isArray( cards ) ) {
// Handle individual card objects
return this.addToBottom( [ cards ] );
}
this._stack.push.apply( this._stack, cards );
return this;
};
/**
* Insert a card or cards at the bottom of the deck in random order
*
* @chainable
* @param {Object|Array} cards The card object or array of card objects to insert
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.shuffleToBottom = function( cards ) {
if ( ! isArray( cards ) ) {
// Handle individual card objects
return this.shuffleToBottom( [ cards ] );
}
shuffle( cards );
return this.addToBottom( cards );
};
/**
* Insert a card or cards at the top of the deck in order
*
* @chainable
* @param {Object|Array} cards The card object or array of card objects to insert
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.addToTop = function( cards ) {
if ( ! isArray( cards ) ) {
// Handle individual card objects
return this.addToTop( [ cards ] );
}
this._stack.unshift.apply( this._stack, cards );
return this;
};
/**
* Insert a card or cards at the top of the deck in random order
*
* @chainable
* @param {Object|Array} cards The card object or array of card objects to insert
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.shuffleToTop = function( cards ) {
if ( ! isArray( cards ) ) {
// Handle individual card objects
return this.shuffleToTop( [ cards ] );
}
shuffle( cards );
return this.addToTop( cards );
};
/**
* Insert a card or cards into the deck at random positions
*
* @chainable
* @param {Object|Array} cards The card object or array of card objects to insert
* @return {Deck} The deck instance (for chaining)
*/
Deck.prototype.addRandom = function( cards ) {
if ( ! isArray( cards ) ) {
// Handle individual card objects
return this.addRandom( [ cards ] );
}
var stack = this._stack;
cards.forEach( function( card ) {
stack.splice( randomIndex( stack ), 0, card );
} );
return this;
};
/**
* Look at a card or cards on the bottom of the deck, without removing them
* from the deck
*
* @param {Number} count The number of cards to retrieve
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.top = function( count ) {
if ( ! this._stack.length ) { return; }
count || ( count = 1 );
var returnedCards = this._stack.slice( 0, count );
return count === 1 ? returnedCards[ 0 ] : returnedCards;
};
/**
* Look at a card or cards on the top of the deck, without removing them from
* the deck
*
* @param {Number} count The number of cards to retrieve
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.bottom = function( count ) {
if ( ! this._stack.length ) { return; }
count || ( count = 1 );
var returnedCards = this._stack.slice( -count ).reverse();
return count === 1 ? returnedCards[ 0 ] : returnedCards;
};
/**
* Look at a random card or cards, without removing them from the deck
*
* @param {Number} count The number of cards to retrieve
* @return {Object|Array} A single card or an array of cards
*/
Deck.prototype.random = function( count ) {
if ( ! this._stack.length ) { return; }
count || ( count = 1 );
var idx;
if ( count === 1 ) {
idx = randomIndex( this._stack );
return this._stack.slice( idx, idx + 1 )[ 0 ];
}
var cards = [].concat( this._stack );
shuffle( cards );
cards.length = count;
return cards.filter( isNotUndefined );
};
module.exports = Deck;