-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (108 loc) · 1.9 KB
/
index.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
'use strict';
const _ = require('underscore');
function Deck() {
this.deck = [
'As',
'Ks',
'Qs',
'Js',
'Ts',
'9s',
'8s',
'7s',
'6s',
'5s',
'4s',
'3s',
'2s',
'Ah',
'Kh',
'Qh',
'Jh',
'Th',
'9h',
'8h',
'7h',
'6h',
'5h',
'4h',
'3h',
'2h',
'Ad',
'Kd',
'Qd',
'Jd',
'Td',
'9d',
'8d',
'7d',
'6d',
'5d',
'4d',
'3d',
'2d',
'Ac',
'Kc',
'Qc',
'Jc',
'Tc',
'9c',
'8c',
'7c',
'6c',
'5c',
'4c',
'3c',
'2c'
];
this.shuffledDeck = this.deck;
};
// Shuffle Deck
Deck.prototype.shuffle = function() {
let tempDeck = _.shuffle(this.deck);
this.shuffledDeck = tempDeck;
return tempDeck;
};
// Draw or Get some card out of the deck
Deck.prototype.draw = function(count) {
if (count == 0) {
return [];
} else if (count && count != 1) {
let randDeck = [];
for (let i = 0; i < count; i++) {
let index = _.random(0, this.shuffledDeck.length - 1);
let card = this.shuffledDeck[index];
this.shuffledDeck.splice(index, 1);
randDeck.push(card);
}
return randDeck;
} else {
let index = _.random(0, this.shuffledDeck.length - 1);
let card = this.shuffledDeck[index];
this.shuffledDeck.splice(index, 1);
return [card];
}
};
// Add new deck to the
Deck.prototype.add = function(count) {
let finalDeck = _.shuffle(this.deck);
this.shuffledDeck = finalDeck;
// for 1 deck
if (count == 0 || count == 1) {
return finalDeck;
} else {
// for many deck
for (let i = 0; i < count - 1; i++) {
let tempDeck = _.shuffle(this.deck);
finalDeck = finalDeck.concat(tempDeck);
}
finalDeck = _.shuffle(finalDeck)
this.shuffledDeck = finalDeck;
return this.shuffledDeck;
}
};
// Get the left out cards after each draw
Deck.prototype.left = function() {
return this.shuffledDeck;
};
module.exports = Deck;