-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (107 loc) · 3.74 KB
/
app.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
const hit = document.querySelector(".hit");
const stand = document.querySelector(".stand");
const deal = document.querySelector(".deal");
const result = document.querySelector(".result");
const userCardSection = document.querySelector(".userCardSection");
const compCardSection = document.querySelector(".compCardSection");
const userScoreDOM = document.querySelector(".userScore");
const compScoreDOM = document.querySelector(".compScore");
const userSumDOM = document.querySelector(".userSum");
const compSumDOM = document.querySelector(".compSum");
const drawsDOM = document.querySelector(".draws");
const hitSound = new Audio("static/sounds/swish.m4a");
const winSound = new Audio("static/sounds/cash.mp3");
const loseSound = new Audio("static/sounds/aww.mp3");
let userScore = 0;
let compScore = 0;
let draws = 0;
let userSum = 0;
let compSum = 0;
function pickAndDisplayCard(sum, cardArea, sumArea) {
const choices = [2,3,4,5,6,7,8,9,"J","Q","K","A"]
let index = Math.floor(Math.random() * 12);
let image = document.createElement("img");
stand.removeAttribute("disabled");
// stand.classList.remove("disableBtn");
image.setAttribute("src","static/images/"+choices[index]+".jpg");
hitSound.play();
cardArea.appendChild(image);
if(index >= 8 && index <= 10) {
sum = sum + 10;
} else if(index === 11 && sum < 11) {
sum = sum + 11;
} else if(index === 11 && sum >= 11) {
sum = sum + 1;
} else {
sum = sum + index + 2;
}
sumArea.textContent = sum;
return sum;
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function compHit() {
hit.setAttribute("disabled", "");
stand.setAttribute("disabled", "");
deal.removeAttribute("disabled");
// deal.classList.remove("disableBtn");
// hit.className += " disableBtn";
// stand.className += " disableBtn";
while(compSum < 15) {
compSum = pickAndDisplayCard(compSum, compCardSection, compSumDOM);
if(compSum > 21) {
compSumDOM.textContent = "BUST!";
compareScore();
}
await sleep(800);
}
compareScore();
}
function compareScore() {
if((userSum > 21 && compSum > 21) || (userSum == compSum)) {
result.textContent = "You Drew!";
result.style.color = "grey";
draws++;
drawsDOM.textContent = draws;
} else if((userSum > 21 && compSum <= 21) || (userSum < 22 && compSum < 22 && userSum < compSum)) {
result.textContent = "You Lost!";
result.style.color = "red";
compScore++;
compScoreDOM.textContent = compScore;
loseSound.play();
} else if((userSum <= 21 && compSum > 21) || (userSum < 22 && compSum < 22 && userSum > compSum)) {
result.textContent = "You Won!";
result.style.color = "green";
userScore++;
userScoreDOM.textContent = userScore;
winSound.play();
}
}
hit.addEventListener("click", ()=> {
userSum = pickAndDisplayCard(userSum, userCardSection, userSumDOM);
if(userSum > 21) {
userSumDOM.textContent = "BUST!";
compHit();
}
});
stand.addEventListener("click", ()=> {
compHit();
});
deal.addEventListener("click", ()=> {
userSumDOM.textContent = "0";
compSumDOM.textContent = "0";
result.textContent = "Ready for Next Round?";
// userCardSection.innerHTML = "";
while (userCardSection.firstChild) {
userCardSection.removeChild(userCardSection.firstChild);
}
while (compCardSection.firstChild) {
compCardSection.removeChild(compCardSection.firstChild);
}
compSum = userSum = 0;
hit.removeAttribute("disabled");
result.style.color = "whitesmoke";
deal.setAttribute("disabled","")
// hit.classList.remove("disableBtn");
});