-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (120 loc) · 4.8 KB
/
script.js
File metadata and controls
151 lines (120 loc) · 4.8 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
// Dark-Light Mode
document.addEventListener("DOMContentLoaded", function() {
// Get the toggle input element
const toggleInput = document.querySelector('.mode-toggle input');
// Check if the theme preference is stored in local storage
const storedTheme = localStorage.getItem('theme');
// Set the initial theme based on the stored preference (if available)
if (storedTheme) {
document.body.classList.toggle('dark-mode', storedTheme === 'dark');
toggleInput.checked = storedTheme === 'dark';
} else {
// Default to dark mode if no preference is stored
document.body.classList.add('dark-mode');
toggleInput.checked = true;
}
// Add an event listener to the toggle input
toggleInput.addEventListener('change', function() {
// Toggle the dark mode class on the body
document.body.classList.toggle('dark-mode', this.checked);
// Store the theme preference in local storage
localStorage.setItem('theme', this.checked ? 'dark' : 'light');
});
});
//disabling Right-click
document.addEventListener('contextmenu', event => event.preventDefault());
let user_score = 0;
let cpu_score = 0;
/*caching the dom variables to use it later by calling only variable names
(We don't have to use Document.getElementById repeatedly)*/
const user_scoreSpan = document.getElementById("userScore");
const cpu_scoreSpan = document.getElementById("cpuScore");
const score_boardDiv = document.querySelector(".scoreBoard");
const resultDiv_p = document.querySelector(".result > p");
const rockDiv = document.getElementById("r");
const paperDiv = document.getElementById("p");
const scissorDiv = document.getElementById("s");
let ResetButton = document.querySelector(".resetBtn");
/*Setting up the Cpu's random choices by using Math Function */
function getCpuChoice(){
const choices = ['r','p','s'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
// converting letter to words for displaying properly inside the result
function convertToWord(move) {
if (move === "r") return "Rock";
if (move === "p") return "Paper";
return "Scissor";
}
// adding a condition for scoreboard and result
function win(userChoice, cpuChoice){
user_score++;
user_scoreSpan.innerHTML = user_score;
cpu_scoreSpan.innerHTML = cpu_score;
resultDiv_p.innerHTML = `${convertToWord(userChoice)} beats ${convertToWord(cpuChoice)}. <span style="color:green;">You Win!</span>`;
document.getElementById(userChoice).classList.add('greenGlow');
setTimeout(function() { document.getElementById(userChoice).classList.remove('greenGlow') }, 1000);
}
function lose(userChoice, cpuChoice) {
cpu_score++;
user_scoreSpan.innerHTML = user_score;
cpu_scoreSpan.innerHTML = cpu_score;
resultDiv_p.innerHTML = `${convertToWord(cpuChoice)} beats ${convertToWord(userChoice)}. <span style="color:red;">You Lose!</span>`;
document.getElementById(userChoice).classList.add('redGlow');
setTimeout(function() { document.getElementById(userChoice).classList.remove('redGlow') }, 1000);
}
function draw(userChoice, cpuChoice) {
resultDiv_p.innerHTML = `${convertToWord(userChoice)} is equal to ${convertToWord(cpuChoice)}. <span style="color:grey;">It's a Draw</span>`;
document.getElementById(userChoice).classList.add('greyGlow');
setTimeout(function() { document.getElementById(userChoice).classList.remove('greyGlow') }, 1000);
}
/* Rock < Paper < Scissor < (loop) */
function game(userChoice) {
const cpuChoice = getCpuChoice();
switch (userChoice + cpuChoice) {
case "rs": /* User Win Case */
case "pr":
case "sp":
win(userChoice, cpuChoice);
break;
case "rp": /* User Lose Case */
case "ps":
case "sr":
lose(userChoice, cpuChoice);
break;
case "rr": /* Draw Case */
case "pp":
case "ss":
draw(userChoice, cpuChoice);
break;
}
}
function main() {
rockDiv.addEventListener('click', function(){
game("r");
})
paperDiv.addEventListener('click', function(){
game("p");
})
scissorDiv.addEventListener('click', function(){
game("s");
})
}
main();
// Reset Button
function resetGame() {
user_score = 0;
cpu_score = 0;
user_scoreSpan.innerHTML = user_score;
cpu_scoreSpan.innerHTML = cpu_score;
resultDiv_p.innerHTML = "Make Your Move.";
}
ResetButton.addEventListener('click', resetGame);
// Rules Button
document.querySelector(".rulesBtn").addEventListener('click', function(){
document.getElementById("infoBox").style.display = "block";
});
document.querySelector(".close-btn").addEventListener('click', function(){
document.getElementById("infoBox").style.display = "none";
});