-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardDraw.js
71 lines (67 loc) · 2.82 KB
/
cardDraw.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
const deck = [
{ suit: 'Hearts', type: '2', value: 2 },
{ suit: 'Hearts', type: '3', value: 3 },
{ suit: 'Hearts', type: '4', value: 4 },
{ suit: 'Hearts', type: '5', value: 5 },
{ suit: 'Hearts', type: '6', value: 6 },
{ suit: 'Hearts', type: '7', value: 7 },
{ suit: 'Hearts', type: '8', value: 8 },
{ suit: 'Hearts', type: '9', value: 9 },
{ suit: 'Hearts', type: '10', value: 10 },
{ suit: 'Hearts', type: 'J', value: 10 },
{ suit: 'Hearts', type: 'Q', value: 10 },
{ suit: 'Hearts', type: 'K', value: 10 },
{ suit: 'Hearts', type: 'A', value: 1 },
{ suit: 'Diamonds', type: '2', value: 2 },
{ suit: 'Diamonds', type: '3', value: 3 },
{ suit: 'Diamonds', type: '4', value: 4 },
{ suit: 'Diamonds', type: '5', value: 5 },
{ suit: 'Diamonds', type: '6', value: 6 },
{ suit: 'Diamonds', type: '7', value: 7 },
{ suit: 'Diamonds', type: '8', value: 8 },
{ suit: 'Diamonds', type: '9', value: 9 },
{ suit: 'Diamonds', type: '10', value: 10 },
{ suit: 'Diamonds', type: 'J', value: 10 },
{ suit: 'Diamonds', type: 'Q', value: 10 },
{ suit: 'Diamonds', type: 'K', value: 10 },
{ suit: 'Diamonds', type: 'A', value: 1 },
{ suit: 'Clubs', type: '2', value: 2 },
{ suit: 'Clubs', type: '3', value: 3 },
{ suit: 'Clubs', type: '4', value: 4 },
{ suit: 'Clubs', type: '5', value: 5 },
{ suit: 'Clubs', type: '6', value: 6 },
{ suit: 'Clubs', type: '7', value: 7 },
{ suit: 'Clubs', type: '8', value: 8 },
{ suit: 'Clubs', type: '9', value: 9 },
{ suit: 'Clubs', type: '10', value: 10 },
{ suit: 'Clubs', type: 'J', value: 10 },
{ suit: 'Clubs', type: 'Q', value: 10 },
{ suit: 'Clubs', type: 'K', value: 10 },
{ suit: 'Clubs', type: 'A', value: 1 },
{ suit: 'Spades', type: '2', value: 2 },
{ suit: 'Spades', type: '3', value: 3 },
{ suit: 'Spades', type: '4', value: 4 },
{ suit: 'Spades', type: '5', value: 5 },
{ suit: 'Spades', type: '6', value: 6 },
{ suit: 'Spades', type: '7', value: 7 },
{ suit: 'Spades', type: '8', value: 8 },
{ suit: 'Spades', type: '9', value: 9 },
{ suit: 'Spades', type: '10', value: 10 },
{ suit: 'Spades', type: 'J', value: 10 },
{ suit: 'Spades', type: 'Q', value: 10 },
{ suit: 'Spades', type: 'K', value: 10 },
{ suit: 'Spades', type: 'A', value: 1 }
];
export function cardDraw() {
if (deck.length === 0) {
throw new Error("All cards have been drawn"); // Throws error if deck is empty
}
const index = Math.floor(Math.random() * deck.length);
const card = deck.splice(index, 1)[0]; // Removes the card from the deck
return card;
}
export function cardDrawReplacement() {
const index = Math.floor(Math.random() * deck.length);
const card = deck[index]; // Does not remove the card from the deck
return card;
}