-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
120 lines (106 loc) · 3.66 KB
/
main.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
// Game Options for both player or CPU
const choices = ['rock', 'paper', 'scissors'];
// Responses passed to Emojies to display it on screen
const rules = {
rock: '✊',
paper: '🖐️',
scissors: '✌️',
};
// Counters initiation
let plyCounter = 0;
let cpuCounter = 0;
// Set background music to false on start
let isMusicOn = false;
// Set instances of all elements involved on the game
const player = document.getElementById('player');
const computer = document.getElementById('computer');
const result = document.getElementById('result');
const playerScore = document.getElementById('playerScore');
const computerScore = document.getElementById('computerScore');
const plyDisplay = document.getElementById('plyDisplay');
const cpuDisplay = document.getElementById('cpuDisplay');
// Toast section
const toastLiveExample = document.getElementById('liveToast');
const toast = new bootstrap.Toast(toastLiveExample);
// Function to get a random choice from the choice array
function getRndChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}
// Main function to be executed on every click event of the 3 options
function play(playerChoice) {
clickSound.play();
// Get the random choice for CPU
const computerChoice = getRndChoice();
// Set both player and CPU with their chosen options
plyDisplay.src = `animations/ply_${playerChoice}.gif`;
cpuDisplay.src = `animations/ply_${computerChoice}.gif`;
// Game Validations
if (playerChoice === computerChoice) {
result.innerText = 'It is a draw! 😌';
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
result.innerText = 'You win! 😄';
winSound.play();
plyCounter += 1; // Increase player score
playerScore.innerText = `Player: ${plyCounter}`; // Update score on screen
} else {
result.innerText = 'You lose! 😢';
loseSound.play();
cpuCounter += 1; // Increase CPU score
computerScore.innerText = `Computer: ${cpuCounter}`; // Update score on screen
}
// Save the current scores on the local Storage key
localStorage.setItem(
'prevScore',
JSON.stringify({
plyCounter: plyCounter,
cpuCounter: cpuCounter,
})
);
}
// This function is to control background music with a toggle button
function bgMusic() {
const bgMusicAudioTag = document.getElementById('musicSelector');
if (isMusicOn === true) {
bkMusic.pause();
bkMusic.currentTime = 0;
isMusicOn = false;
bgMusicAudioTag.innerText = 'Turn Music ON 🔈';
// Set class attributes to simulate toggling
bgMusicAudioTag.setAttribute(
'class',
'w-50 text-muted text-end poppins-extralight'
);
} else {
bkMusic.play();
isMusicOn = true;
bgMusicAudioTag.innerText = 'Turn Music OFF 🔊';
bgMusicAudioTag.setAttribute(
'class',
'w-50 text-end poppins-extralight'
);
}
}
function retrieveMatchScores() {
// Retrieve the content of 'prevScore' from localStorage
const prevScore = JSON.parse(localStorage.getItem('prevScore'));
plyCounter = prevScore.plyCounter;
cpuCounter = prevScore.cpuCounter;
playerScore.innerText = `Player: ${plyCounter}`;
computerScore.innerText = `Computer: ${cpuCounter}`;
}
// Check if 'prevScore' exists in localStorage
if (localStorage.getItem('prevScore')) {
// Confirm if user wants to resume previous match scores
toast.show();
} else {
// Initialize 'prevScore' with plyCounter and cpuCounter set to 0
const prevScore = {
plyCounter: 0,
cpuCounter: 0,
};
localStorage.setItem('prevScore', JSON.stringify(prevScore));
}