-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
147 lines (123 loc) · 5.65 KB
/
script.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
135
136
137
138
139
140
141
142
143
144
145
146
147
const startGameBtn = document.querySelector("#startGameBtn");
const startDialog = document.querySelector("#startDialog");
const choices = document.querySelectorAll("#userChoices > #choices > div");
const resultText = document.querySelector("#resultText");
const gameHeading = document.querySelector("#gameHeading");
const gameWindow = document.querySelector("#gameWindow");
const compChoiceCircle = document.querySelectorAll("#compChoices > #choices > div");
const playAgainGameBtn = document.querySelector("#playAgainGameBtn");
const endDialog = document.querySelector("#endDialog");
const endDialogHeading = document.querySelector("#endDialogHeading");
const endDialogSubHeading = document.querySelector("#endDialogSubHeading");
const clickSound = new Audio('assets/click.mp3');
const winSound = new Audio('assets/win.mp3');
const loseSound = new Audio('assets/lose.mp3');
const tieSound = new Audio('assets/tie.mp3');
const gameWinSound = new Audio('assets/gameWin.mp3');
const gameLoseSound = new Audio('assets/gameLose.mp3');
let userScoreText = document.querySelector("#userScore");
let compScoreText = document.querySelector("#compScore");
let userScore = 0;
let compScore = 0;
//initial splash screen, for user to start game
startDialog.addEventListener("click",e => {
if(e.target.matches("button")) {
clickSound.play()
startDialog.style.display = "none"
gameWindow.classList.toggle("hiddenClass")
gameStart();
};
})
function gameStart(){
//adds event listener to each option and assign the value to userInput
const playRound = choices.forEach((element) => {
element.addEventListener("click",(e)=>{
let computerChoice = Math.floor(Math.random() * 3) + 1
const choiceContainer = element.innerText.trim()
const userChoice = choiceContainer.substr(2,choiceContainer.length).trim();
//assigns value to random number from comp
switch (computerChoice){
case 1:
computerChoice = "ROCK";
break;
case 2:
computerChoice = "PAPER";
break;
case 3:
computerChoice = "SCISSOR";
break;
default:
console.log(`${computerChoice} computerChoice error`)
}
//compares userChoice with computerChoice
gameHeading.innerText = "Choose Your Next Move";
if (userChoice == computerChoice){
resultText.innerHTML = `<span style=color:purple>TIE!</span> you both chose ${userChoice}`
tieSound.play();
}
if (userChoice == "ROCK" && computerChoice == "SCISSOR"){
resultText.innerHTML = `<span style=color:green>YOU WON,</span> ${userChoice} beats ${computerChoice}`;
userScoreText.innerText = `Score: ${userScore+=1}`
winSound.play();
}
if (userChoice == "PAPER" && computerChoice == "ROCK") {
resultText.innerHTML = `<span style=color:green>YOU WON,</span> ${userChoice} beats ${computerChoice}`;
userScoreText.innerText = `Score: ${userScore += 1}`;
winSound.play()
}
if (userChoice == "SCISSOR" && computerChoice == "PAPER") {
resultText.innerHTML = `<span style=color:green>YOU WON,</span> ${userChoice} beats ${computerChoice}`;
userScoreText.innerText = `Score: ${userScore += 1}`;
winSound.play()
}
if (userChoice == "ROCK" && computerChoice == "PAPER") {
resultText.innerHTML = `<span style=color:red>YOU LOST,</span> ${computerChoice} beats ${userChoice}`;
compScoreText.innerText = `Score: ${compScore += 1}`;
loseSound.play()
}
if (userChoice == "PAPER" && computerChoice == "SCISSOR") {
resultText.innerHTML = `<span style=color:red>YOU LOST,</span> ${computerChoice} beats ${userChoice}`;
compScoreText.innerText = `Score: ${compScore += 1}`;
loseSound.play()
}
if (userChoice == "SCISSOR" && computerChoice == "ROCK") {
resultText.innerHTML = `<span style=color:red>YOU LOST,</span> ${computerChoice} beats ${userChoice}`;
compScoreText.innerText = `Score: ${compScore += 1}`;
loseSound.play()
}
//game over condition
if (userScore >= 5 || compScore >= 5) {
gameWindow.classList.toggle("hiddenClass")
endDialog.style.display = "flex"
}
if (userScore >= 5) {
gameWinSound.play();
endDialogHeading.innerText = "You Won"
endDialogSubHeading.innerText = `Your Score: ${userScore}`
}
if (compScore >= 5) {
gameLoseSound.play();
endDialogHeading.innerText = "You Lost"
endDialogSubHeading.innerText = `Computer won ${compScore} rounds against you`
}
});
})
};
function resetScore(){
userScore = 0
compScore = 0
compScoreText.innerText = `Score: 0`
userScoreText.innerText = `Score: 0`
gameHeading.innerText = "Choose Your Move";
resultText.innerText = ""
element.replaceWith(element.cloneNode(true));
}
endDialog.addEventListener("click",e => {
if(e.target.matches("button")) {
clickSound.play()
endDialog.style.display = "none";
gameWindow.classList.toggle("hiddenClass");
resetScore();
gameStart();
};
})