-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (103 loc) · 4.05 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
"use strict";
// ---------------------- Set global variables ------------------------
let choice = ["rock", "paper", "scissors"];
// let gameRound = 0;
let playerScore = 0;
let computerScore = 0;
// // -------------- Getting the computer choice from an array ---------------------
function getComputerChoice() {
return choice[Math.floor(Math.random() * choice.length)];
};
getComputerChoice();
let computerSelection = getComputerChoice();
// // ------- Getting the player's choice from a button / Play sound on click ------------
let playerSelection = () => {
const weapon = new Audio("sounds/weaponSound.mp3");
const rock = document.getElementById("rock").onclick = () => {
playerSelection = choice[0];
weapon.currentTime = 0;
playRound();
weapon.play();
};
const paper = document.getElementById("paper").onclick = () => {
playerSelection = choice[1];
weapon.currentTime = 0;
playRound();
weapon.play();
};
const scissors = document.getElementById("scissors").onclick = () => {
playerSelection = choice[2];
weapon.currentTime = 0;
playRound();
weapon.play();
};
return;
};
playerSelection();
// // -------------- Play the game, change text ---------------------
function playRound() {
computerSelection = getComputerChoice();
const winner = ["You won the game!", "You lost the game!"];
if (playerSelection === computerSelection) {
// return "Tie";
document.getElementById('result').innerHTML = `Tie! you both choose: ${playerSelection.toUpperCase()}`;
document.getElementById('result').style.color = "#000";
gameOverSounds();
}
else if ((playerSelection === choice[0] && computerSelection === choice[2]) || (playerSelection === choice[1] && computerSelection === choice[0]) || (playerSelection === choice[2] && computerSelection === choice[1])) {
playerScore++;
document.getElementById('result').innerHTML = "You won. Good job!!";
document.getElementById('result').style.color = "#23C552";
document.getElementById('playerScore').innerHTML = playerScore;
gameOverSounds();
stopGame();
if (playerScore == 3) {
document.getElementById("bannerAnounce").innerHTML = winner[0];
document.getElementById("bannerAnounce").style.backgroundColor = "#23C552";
}
}
else if ((playerSelection === choice[0] && computerSelection === choice[1]) || (playerSelection === choice[1] && computerSelection === choice[2]) || (playerSelection === choice[2] && computerSelection === choice[0])) {
computerScore++;
document.getElementById('result').innerHTML = "Computer won!";
document.getElementById('result').style.color = "#F84F31";
document.getElementById('cpuScore').innerHTML = computerScore;
gameOverSounds();
stopGame();
if (computerScore == 3) {
document.getElementById("bannerAnounce").innerHTML = winner[1]
document.getElementById("bannerAnounce").style.backgroundColor = "#F84F31";
}
}
// gameRound++;
}
playRound();
// // -------------- Play sounds at the end of the game ---------------------
function gameOverSounds() {
const playerWon = new Audio("sounds/playerWon.mp3");
const computerWon = new Audio("sounds/computerWon.mp3");
if (playerScore == 3) {
playerWon.play();
}
else if (computerScore == 3) {
computerWon.play();
}
}
gameOverSounds();
// // -------------- Stop the game / Disable buttons-weapons ---------------------
function stopGame() {
const main = document.querySelector(".main");
if ((playerScore == 3) || (computerScore == 3)) {
document.getElementById('result').style.color = "#fccb06";
document.getElementById('result').innerHTML = "Game over! Press reset to play again.";
document.getElementById("rock").disabled = true;
document.getElementById("paper").disabled = true;
document.getElementById("scissors").disabled = true;
main.classList.add("game-done");
}
}
stopGame();
// -------------- Restart the game // Refresh the browser ---------------------
const resetButton = document.getElementById("buttonReset");
resetButton.onclick = () => {
window.location.reload();
}