-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (78 loc) · 2.33 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
// Define player object with name and chips properties
let player = {
name: "Per",
chips: 200
}
// Initialize variables for cards, sum, and game status
let cards = []
let sum = 0
let hasBlackJack = false
let isAlive = false
let message = ""
// Get DOM elements
let messageEl = document.getElementById("message-el")
let sumEl = document.getElementById("sum-el")
let cardsEl = document.getElementById("cards-el")
let playerEl = document.getElementById("player-el")
// Display player's name and chips
playerEl.textContent = player.name + ": $" + player.chips
// Function to get a random card value (1-10)
function getRandomCard() {
let randomNumber = Math.floor(Math.random() * 13) + 1
if (randomNumber > 10) {
return 10
} else if (randomNumber === 1) {
return 11
} else {
return randomNumber
}
}
// Function to start the game
function startGame() {
// Set game status to alive
isAlive = true
// Get two random cards
let firstCard = getRandomCard()
let secondCard = getRandomCard()
// Initialize cards array and sum
cards = [firstCard, secondCard]
sum = firstCard + secondCard
// Render the game
renderGame()
}
// Function to render the game state
function renderGame() {
// Clear cards element
cardsEl.textContent = "Cards: "
// Display each card value in the cards element
for (let i = 0; i < cards.length; i++) {
cardsEl.textContent += cards[i] + " "
}
// Display the current sum
sumEl.textContent = "Sum: " + sum
// Set the message based on the sum value
if (sum <= 20) {
message = "Do you want to draw a new card?"
} else if (sum === 21) {
message = "You've got Blackjack!"
hasBlackJack = true
} else {
message = "You're out of the game!"
isAlive = false
}
// Display the message
messageEl.textContent = message
}
// Function to draw a new card
function newCard() {
// Check if the game is still alive and not already has a Blackjack
if (isAlive === true && hasBlackJack === false) {
// Get a new random card
let card = getRandomCard()
// Add the card value to the sum and cards array
sum += card
cards.push(card)
// Render the updated game state
renderGame()
}
}