-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.html
More file actions
83 lines (75 loc) · 2.15 KB
/
Copy pathNumberGame.html
File metadata and controls
83 lines (75 loc) · 2.15 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Game</title>
<style>
body {
background-color: black;
color: white;
display: flex;
justify-content: center;
align-items: center;
height: 700px;
flex-direction: column;
}
.playbtn {
background-color: red;
margin: 10px;
padding: 12px;
cursor: pointer;
}
.playground {
font-size: 24px;
margin: 5px;
}
</style>
</head>
<body>
<div class="playground userscore">User Score: 0</div>
<button class="playbtn">Play</button>
<div class="playground computerscore">Computer Score: 0</div>
<script>
const playbtn = document.querySelector(".playbtn");
const userscore = document.querySelector(".userscore");
const computerscore = document.querySelector(".computerscore");
let playground = {
user: 0,
computer: 0,
};
let target = 20;
let min = 1;
let max = 3;
function randomNumber() {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function checkWinner() {
if (playground.user >= target) {
setTimeout(() => alert("User Wins! 🎉"), 200);
resetGame();
} else if (playground.computer >= target) {
setTimeout(() => alert("Computer Wins! 🤖"), 200);
resetGame();
}
}
function resetGame() {
playground.user = 0;
playground.computer = 0;
userscore.innerHTML = "User Score: 0";
computerscore.innerHTML = "Computer Score: 0";
}
function computerTurn() {
playground.computer += randomNumber();
computerscore.innerHTML = "Computer Score: " + playground.computer;
checkWinner();
}
playbtn.addEventListener("click", () => {
playground.user += randomNumber();
userscore.innerHTML = "User Score: " + playground.user;
checkWinner();
setTimeout(computerTurn, 500);
});
</script>
</body>
</html>