-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckOfCards.js
91 lines (80 loc) · 2.16 KB
/
DeckOfCards.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
class Deck {
var ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var suits = ["♠","♥", "♦", "♣"];
function createDeck() {
var deck = []
suits.map(function(suit) {
ranks.map(function(rank,i) {
deck.push(suit + rank)
})
})
return deck
}
var fullDeck = createDeck()
var shuffledDeck = fullDeck
function shuffleCards(deck) {
shuffDeck = []
temp = []
function random(){
return Math.floor(Math.random() * 52)
}
deck.map(function(card) {
var p = random();
if (temp.indexOf(p) < 0) {
shuffDeck.push(deck[p])
temp.push(p)
} else {
while(temp.indexOf(p) > -1) {
p = random()
if(temp.indexOf(p) < 0) {
shuffDeck.push(deck[p])
break;
}
}
}
})
return shuffDeck
}
function dealCards(number, faceDown, position){
var dealtCards = []
var selectedCards = shuffledDeck.splice(0,number)
if (number > shuffledDeck.length) {
var notEnoughCards = shuffledDeck.splice(0,number)
notEnoughCards.map((card) => {
dealtCards.push({card: card, faceDown: faceDown, position: position})
})
dealtCards.push({card: "No More Cards"})
} else {
selectedCards.map((card) => {
dealtCards.push({card: card, faceDown: faceDown, position: position})
})
}
return dealtCards
}
function cutDeck(position) {
var deckCut = []
var deckCut = shuffledDeck.splice(position, 51)
deckCut.push(...shuffledDeck)
shuffledDeck = deckCut
}
}
/*
document.createElement('body');
var shuffle = document.createElement('button');
document.body.appendChild(shuffle);
shuffle.onclick = function() {
shuffledDeck = shuffleCards(fullDeck);
}
shuffle.textContent = "shuffle deck"
var pickCard = document.createElement('button');
document.body.appendChild(pickCard);
pickCard.textContent = "draw card";
pickCard.onclick = function() {
dealCards(5, true, 3)
}
var deckCutter = document.createElement('button');
document.body.appendChild(deckCutter);
deckCutter.textContent = "cut deck";
deckCutter.onclick = function() {
cutDeck(22)
}*/