-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
184 lines (164 loc) · 5.25 KB
/
script.js
File metadata and controls
184 lines (164 loc) · 5.25 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {startConfetti, stopConfetti, removeConfetti} from './confetti.js'
const playerScoreEl = document.getElementById('playerScore');
const playerChoiceEl = document.getElementById('playerChoice');
const playerRock = document.getElementById('playerRock');
const playerPaper = document.getElementById('playerPaper');
const playerScissors = document.getElementById('playerScissors');
const playerLizard = document.getElementById('playerLizard');
const playerSpock = document.getElementById('playerSpock');
const computerScoreEl = document.getElementById('computerScore');
const computerChoiceEl = document.getElementById('computerChoice');
const computerRock = document.getElementById('computerRock');
const computerPaper = document.getElementById('computerPaper');
const computerScissors = document.getElementById('computerScissors');
const computerLizard = document.getElementById('computerLizard');
const computerSpock = document.getElementById('computerSpock');
const resultText = document.getElementById('resultText');
const allGameIcon = document.querySelectorAll('.far');
let computerChoice = '';
let playerScoreNumber = 0;
let computerScoreNumber = 0;
const hitSound = new Audio('sounds/swish.m4a');
const lossSound = new Audio('sounds/aww.mp3');
const winSound = new Audio('sounds/cash.mp3');
const choices = {
rock: { name: 'Rock', defeats: ['scissors', 'lizard'] },
paper: { name: 'Paper', defeats: ['rock', 'spock'] },
scissors: { name: 'Scissors', defeats: ['paper', 'lizard'] },
lizard: { name: 'Lizard', defeats: ['paper', 'spock'] },
spock: { name: 'Spock', defeats: ['scissors', 'rock'] },
};
//Reset all 'selected' icons
function resetSelected(){
allGameIcon.forEach(( icon ) => {
icon.classList.remove('selected');
});
stopConfetti();
removeConfetti();
}
// Reset Score & playerChoice/computerChoice
function resetAll(){
playerScoreNumber = 0;
computerScoreNumber = 0;
playerScoreEl.textContent = playerScoreNumber;
computerScoreEl.textContent = computerScoreNumber;
playerChoiceEl.textContent = "";
computerChoiceEl.textContent = "";
resultText.textContent = '';
resetSelected();
}
window.resetAll = resetAll;
// Check result, increase score and update resultText
function updateScore(playerChoice){
if(playerChoice === computerChoice){
resultText.textContent = "It's a tie."
} else {
const choice = choices[playerChoice];
if(choice.defeats.indexOf(computerChoice) > -1){
resultText.textContent = "You won.";
startConfetti();
winSound.play();
playerScoreNumber++;
playerScoreEl.textContent = playerScoreNumber;
} else {
resultText.textContent = "You lost.";
lossSound.play();
computerScoreNumber++;
computerScoreEl.textContent = computerScoreNumber;
}
}
}
function computerRandomChoice(){
const computerChoiceNumber = Math.random();
if(computerChoiceNumber < 0.2){
computerChoice = 'rock';
} else if (computerChoiceNumber <= 0.4){
computerChoice = 'paper';
} else if(computerChoiceNumber <= 0.6){
computerChoice = 'scissors';
} else if(computerChoiceNumber <= 0.8){
computerChoice = 'lizard'
} else {
computerChoice = 'spock'
}
}
// Add 'selected' styling & computerChoice
function displayChomputerChoice() {
switch(computerChoice){
case 'rock' : {
computerRock.classList.add('selected');
computerChoiceEl.textContent = " --- rock";
break;
}
case 'paper' : {
computerPaper.classList.add('selected');
computerChoiceEl.textContent = ' --- paper';
break;
}
case 'scissors' : {
computerScissors.classList.add('selected');
computerChoiceEl.textContent = ' --- scissors';
break;
}
case 'lizard': {
computerLizard.classList.add('selected');
computerChoiceEl.textContent = ' --- lizard';
break;
}
case 'spock':{
computerSpock.classList.add('selected');
computerChoiceEl.textContent = ' --- spock';
break;
}
default:
break;
}
}
function checkResult(playerChoice){
resetSelected();
computerRandomChoice();
displayChomputerChoice();
updateScore(playerChoice);
}
// Passing player selection value and style icons
function select(playerChoice) {
checkResult(playerChoice)
// Add 'selected' styling and update playerChoice
switch(playerChoice){
case 'rock' : {
playerRock.classList.add('selected');
playerChoiceEl.textContent = " --- rock";
hitSound.play();
break;
}
case 'paper' : {
playerPaper.classList.add('selected');
playerChoiceEl.textContent = ' --- paper';
hitSound.play();
break;
}
case 'scissors' : {
playerScissors.classList.add('selected');
playerChoiceEl.textContent = ' --- scissors';
hitSound.play();
break;
}
case 'lizard': {
playerLizard.classList.add('selected');
playerChoiceEl.textContent = ' --- lizard';
hitSound.play();
break;
}
case 'spock':{
playerSpock.classList.add('selected');
playerChoiceEl.textContent = ' --- spock';
hitSound.play();
break;
}
default:
break;
}
}
window.select = select;
// On start up, set initial value
resetAll();