-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (59 loc) · 2.21 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
let firstPlayerScore = 0
let secondPlayerScore = 0
let player1Chance = true
// All the elements from the id
const messageEl = document.getElementById("message")
const player1Scoreboard = document.getElementById("player1Scoreboard")
const player2Scoreboard = document.getElementById("player2Scoreboard")
const player1Dice = document.getElementById("player1Dice")
const player2Dice = document.getElementById("player2Dice")
const rollBtn = document.getElementById("rollDice")
const resetBtn = document.getElementById("resetGame")
rollBtn.addEventListener("click", gameStart)
function gameStart() {
rollBtn.disabled = true
const randomNumber = Math.floor(Math.random()*6) + 1
messageEl.textContent = "COMPUTER'S CHANCE"
player1Dice.textContent = randomNumber
firstPlayerScore += randomNumber
player1Scoreboard.textContent = firstPlayerScore
player1Dice.classList.remove("active")
player2Dice.classList.add("active")
if(firstPlayerScore >= 20) {
messageEl.textContent = "YOU WON!🥳"
rollBtn.style.display = "none"
resetBtn.style.display = "block"
return
}
setTimeout(() => computerGame(), 2000);
}
function computerGame() {
const randomNumber = Math.floor(Math.random()*6) + 1
messageEl.textContent = "YOUR CHANCE"
player2Dice.textContent = randomNumber
secondPlayerScore += randomNumber
player2Scoreboard.textContent = secondPlayerScore
player2Dice.classList.remove("active")
player1Dice.classList.add("active")
if(secondPlayerScore >= 20) {
messageEl.textContent = "COMPUTER WON!🎉"
rollBtn.style.display = "none"
resetBtn.style.display = "block"
}
rollBtn.disabled = false
}
resetBtn.addEventListener("click", resetGame)
function resetGame() {
messageEl.textContent = "YOUR CHANCE"
firstPlayerScore = 0
secondPlayerScore = 0
player1Scoreboard.textContent = firstPlayerScore
player2Scoreboard.textContent = secondPlayerScore
player1Dice.textContent = "-"
player2Dice.textContent = "-"
rollBtn.style.display = "block"
resetBtn.style.display = "none"
player2Dice.classList.remove("active")
player1Dice.classList.add("active")
rollBtn.disabled = false
}