-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (105 loc) · 3.09 KB
/
index.js
File metadata and controls
116 lines (105 loc) · 3.09 KB
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
let player = {
name: "Peter",
chips: 140
}
let deck = []
let playerCards = []
let sum = 0
let hasBlackJack = false
let isAlive = false
let message = ""
let messageEl = document.getElementById("message-el")
let sumEl = document.getElementById("sum-el")
let playerCardsEl = document.getElementById("cards-el")
let playerEL = document.getElementById("player-el")
playerEL.textContent = player.name + ": $" + player.chips
// gera um array de com 52 objetos do tipo carta
function generateDeck() {
const naipesIcons = {
'C': "bi bi-heart-fill",
'O': "bi bi-diamond-fill",
'E': "bi bi-suit-spade-fill",
'P': "bi bi-tree-fill"
}
const naipes = ['C', 'O', 'E', 'P']
const numeros = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
let deck = []
for (let naipe of naipes) {
for (let numero of numeros) {
deck.push(carta = {
naipe: naipe,
strValue: numero,
value: numero,
name: numero + naipe,
icone: naipesIcons[naipe]
})
}
}
shuffleArray(deck)
return deck
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function getRandomCard() {
card = deck.pop()
if (card.value === 'J' || card.value === 'Q' || card.value === 'K') {
card.value = 10
} else if (card.value === 'A') {
card.value = 10
} else {
card.value = parseInt(card.value)
}
return card
}
function startGame() {
playerCardsEl.innerHTML = ""
deck = generateDeck()
isAlive = true
hasBlackJack = false
let firstCard = getRandomCard()
let secondCard = getRandomCard()
playerCards = [firstCard, secondCard]
sum = firstCard.value + secondCard.value
renderGame()
}
function renderGame() {
let tempCards = ""
for (let i = 0; i < playerCards.length; i++) {
tempCards += `<li class="${playerCards[i].naipe}">
<div class="card_content top">
<p>${playerCards[i].strValue}</p>
<i class="${playerCards[i].icone}"></i>
</div>
<div class="card_content bottom">
<p>${playerCards[i].strValue}</p>
<i class="${playerCards[i].icone}"></i>
</div>
</li>`
}
playerCardsEl.innerHTML = tempCards
sumEl.textContent = "Sum: " + sum
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
}
messageEl.textContent = message
}
function newCard() {
if (isAlive && !hasBlackJack) {
let card = getRandomCard()
sum += card.value
playerCards.push(card)
renderGame()
}
}