-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (123 loc) · 3.52 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
//refactorizar
document.getElementById("game").hidden = true
let cards = []
let sum = 0
let hasBlackjack = false
let isAlive = false
let message = ''
let messageEl = document.getElementById("message-el")
let cardsEl = document.getElementById('cards-el')
let sumEl = document.getElementById("sum-el")
let playerEl = document.getElementById('player-el')
let player = {
name: '',
money: 0
}
const getRandomCard = () => {
return Math.floor(Math.random() * 13) + 1
}
document.addEventListener('DOMContentLoaded', ()=>{
document.getElementById('submit-btn').addEventListener('click', addPlayer)
})
const addPlayer = (ev) => {
ev.preventDefault()
let x = document.getElementById('input').value;
if (!/^[A-zÀ-ú]*$/g.test(x)) {
swal("Oh, no!", "You've to fill with your name", "warning")
return false;
}
else if (x === "") {
swal("Oh, no!", "You've to fill with your name", "warning")
return false;
}else{
swal('Welcome!', ' ', 'success', {
buttons: false,
timer: 1500,
})
}
player.name = document.getElementById('input').value,
player.money = 200
document.getElementById('form').hidden = true
document.getElementById("game").hidden = false
playerEl.textContent = player.name + ': $' + player.money
}
const startGame = () => {
document.getElementById('start-btn').disabled = true
isAlive = true
let firstCard = getRandomCard()
let secondCard = getRandomCard()
sum = firstCard + secondCard
cards = [firstCard, secondCard]
renderGame()
}
const renderGame = () => {
cardsEl.textContent = 'Cards: '
for (let i = 0; i < cards.length; i++) {
cardsEl.textContent += ' ' + cards[i]
}
sumEl.textContent = 'Sum: '
sumEl.textContent += sum
if (sum <= 20) {
message = 'Do you want to draw a new card?'
} else if (sum === 21) {
message = "You've got Blackjack!"
hasBlackjack = true
player.money += 25
document.getElementById('reset-btn').hidden = false
document.getElementById('playagain-btn').hidden = false
} else {
message = "You're out of the game"
isAlive = false
player.money -= 25
document.getElementById('reset-btn').hidden = false
document.getElementById('playagain-btn').hidden = false
}
if(player.money === 0){
swal('Game Over', ' ', 'error', {button : false, timer: 4000})
document.getElementById('form').hidden = false
document.getElementById("game").hidden = true
player.name = ''
player.money = 0
}
messageEl.textContent = message;
playerEl.textContent = player.name + ': $' + player.money
}
const newCard = () => {
if(isAlive === true && hasBlackjack === false){
let card = getRandomCard()
sum += card
cards.push(card)
renderGame()
}
}
const playAgain = () => {
hasBlackjack = false
cardsEl.textContent = 'Cards: '
cards = []
sumEl.textContent = 'Sum: '
sum = 0
document.getElementById('start-btn').disabled = false
document.getElementById('newcard-btn').disabled = false
document.getElementById('playagain-btn').hidden = true
document.getElementById('reset-btn').hidden = true
messageEl.textContent = 'Want to play a round?'
}
const reset = () => {
swal({
title: "Are you sure?",
text: "This will restart the entire game!",
icon: "error",
buttons: ['Cancel', 'Delete'],
dangerMode: true,
closeOnClickOutside: false
})
.then((willDelete) => {
if (willDelete) {
document.getElementById('form').hidden = false
document.getElementById("game").hidden = true
player.name = ''
player.money = 0
} else {
}
});
}