-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (125 loc) · 4.03 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
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
let cards = []
const array = []
let playerSum = 0
let dealerCardsShown = []
let dealerCardHidden = 0
let dealerSum = 0
let game = {
stand: false,
active: false,
bet: 0
}
let message = ""
let messageEl = document.getElementById("message-el")
let cardsEl = document.getElementById("cards-el")
let player = {
name: "Sarah",
chips: 100
}
let playerEl = document.getElementById("player-el")
let chipsEl = document.getElementById("chips-el")
let betEl = document.getElementById("bet-el")
let dealerCardsEl = document.getElementById("dealercards-el")
function getRandomInt() {
let randomInt = Math.floor(Math.random() * 13) + 1
if (randomInt === 11) {
return 1
} else if (randomInt > 11) {
return 10
} else { return randomInt }
}
function sumCards(array) {
const total = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
return total;
console.log(total)
}
function startGame() {
if (game.active === true) {
message = "Click HIT to draw a new card."
messageEl.textContent = message
} else {
game.active = true
game.bet = 10
betEl.textContent = "Bet: " + game.bet
player.chips -= game.bet
playerEl.textContent = "Player: " + player.name
chipsEl.textContent = "Chips: " + player.chips
dealerRevealed = false
dealerCardsShown = []
dealerCardsShown.push(getRandomInt())
dealerCardHidden = getRandomInt()
dealerCardsEl.textContent = "Dealer Cards: " + dealerCardsShown + ", ?" //+ dealerCardsShown + ", ?"
game.stand = false
let firstCard = getRandomInt()
let secondCard = getRandomInt()
cards = [firstCard, secondCard]
playerSum = sumCards(cards)
cardsEl.textContent = "Cards: " + cards + " (" + playerSum + ")"
checkSum()
}
}
function checkSum() {
if (game.stand === true) {
endGame()
} else if (playerSum < 21) {
message = "Do you want to draw a new card?"
messageEl.textContent = message
} else if (playerSum > 21) {
game.active = false
message = "You're out of the game!"
messageEl.textContent = message
} else {
dealerReveal()
endGame()
}
}
function endGame() {
game.active = false
if (playerSum < dealerSum && dealerSum <= 21) {
message = "Lose! Click Start Game to play again."
} else if (playerSum < 21 && playerSum > dealerSum || playerSum < 21 && dealerSum > 21) {
message = "Win! Click Start Game to play again."
player.chips += game.bet * 2
} else if (playerSum <= 21 && dealerSum === playerSum) {
message = "Draw! Click Start Game to play again."
player.chips += game.bet
} else if (playerSum === 21 && dealerSum != 21) {
message = "BlackJack! Click Start Game to play again."
player.chips += game.bet * 3
}
chipsEl.textContent = "Chips: " + player.chips
messageEl.textContent = message
}
function hit() {
if (game.active === false) {
message = "Click Start Game to Continue"
messageEl.textContent = message
} else {
cards.push(getRandomInt())
playerSum = sumCards(cards)
cardsEl.textContent = "Cards: " + cards + " (" + playerSum + ")"
checkSum()
}
}
function dealerReveal() {
dealerRevealed = true
dealerCardsShown.push(dealerCardHidden)
for (dealerSum = sumCards(dealerCardsShown); dealerSum <= 16;) {
dealerDraw()
}
dealerCardsEl.textContent = "Dealer Cards: " + dealerCardsShown + " (" + dealerSum + ")"
}
function dealerDraw() {
dealerCardsShown.push(getRandomInt())
dealerSum = sumCards(dealerCardsShown)
}
function stand() {
if (game.active === false) {
message = "Click Start Game to Continue"
messageEl.textContent = message
} else {
game.stand = true
dealerReveal()
checkSum()
}
}