forked from dhairyagothi/100_days_100_web_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (148 loc) · 4.73 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const getRuleBox = document.querySelector(".box");
const getGameBox = document.querySelector(".game");
const getWinBox = document.querySelector(".you-win");
const getLoseBox = document.querySelector(".you-lost");
const getDrawBox = document.querySelector(".tie-up");
const getRock = document.querySelector(".stone1");
const getPaper = document.querySelector(".paper1");
const getScissor = document.querySelector(".scissor1");
const opt_U1 = document.getElementById("opt-U1");
const opt_P1 = document.getElementById("opt-P1");
const opt_U2 = document.getElementById("opt-U2");
const opt_P2 = document.getElementById("opt-P2");
const opt_U3 = document.getElementById("opt-U3");
const opt_P3 = document.getElementById("opt-P3");
const com_score = document.getElementById("comscore");
const your_score = document.getElementById("youscore");
if (
localStorage.getItem("clickcount") === null &&
localStorage.getItem("clickcount2") === null
) {
localStorage.setItem("clickcount", 0);
localStorage.setItem("clickcount2", 0);
}
function displayscore() {
if (
localStorage.clickcount === "undefined" &&
localStorage.clickcount2 === "undefined"
) {
your_score.innerHTML = 0;
com_score.innerHTML = 0;
}
your_score.innerHTML = localStorage.clickcount;
com_score.innerHTML = localStorage.clickcount2;
}
displayscore();
function popup() {
getRuleBox.style.display = "block";
}
function popdown() {
getRuleBox.style.display = "none";
}
function replay() {
getWinBox.style.display = "none";
getLoseBox.style.display = "none";
getDrawBox.style.display = "none";
getGameBox.style.display = "block";
}
function getComputerChoice() {
const choices = ["rock", "paper", "scissor"];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
function loses(userOutput, pcOutput) {
getLoseBox.style.display = "flex";
getGameBox.style.display = "none";
console.log(getLoseBox);
if (typeof Storage !== 0) {
if (localStorage.clickcount2) {
localStorage.clickcount2 = Number(localStorage.clickcount2) + 1;
} else {
localStorage.clickcount2 = 1;
}
com_score.innerHTML = localStorage.clickcount2;
} else {
com_score.innerHTML = 0;
}
const cloneU = userOutput.cloneNode(true);
while (opt_U2.firstChild) opt_U2.firstChild.remove();
opt_U2.appendChild(cloneU);
const cloneP = pcOutput.cloneNode(true);
while (opt_P2.firstChild) opt_P2.firstChild.remove();
opt_P2.appendChild(cloneP);
}
function win(userOutput, pcOutput) {
getWinBox.style.display = "flex";
getGameBox.style.display = "none";
console.log(getWinBox);
if (typeof Storage !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
your_score.innerHTML = localStorage.clickcount;
} else {
your_score.innerHTML = 0;
}
const cloneU = userOutput.cloneNode(true);
while (opt_U1.firstChild) opt_U1.firstChild.remove();
opt_U1.appendChild(cloneU);
const cloneP = pcOutput.cloneNode(true);
while (opt_P1.firstChild) opt_P1.firstChild.remove();
opt_P1.appendChild(cloneP);
}
function draw(userOutput, pcOutput) {
getDrawBox.style.display = "flex";
getGameBox.style.display = "none";
console.log(getDrawBox);
const cloneU = userOutput.cloneNode(true);
while (opt_U3.firstChild) opt_U3.firstChild.remove();
opt_U3.appendChild(cloneU);
const cloneP = pcOutput.cloneNode(true);
while (opt_P3.firstChild) opt_P3.firstChild.remove();
opt_P3.appendChild(cloneP);
}
function game(userChoice) {
const computerChoice = getComputerChoice();
var computerOutput = "";
var userOutput = "";
if (userChoice === "rock") {
userOutput = getRock;
} else if (userChoice === "paper") {
userOutput = getPaper;
} else if (userChoice === "scissor") {
userOutput = getScissor;
}
console.log(userOutput);
if (computerChoice === "rock") {
computerOutput = getRock;
} else if (computerChoice === "paper") {
computerOutput = getPaper;
} else if (computerChoice === "scissor") {
computerOutput = getScissor;
}
console.log(computerOutput);
switch (userChoice + computerChoice) {
case "paperrock":
case "rockscissor":
case "scissorpaper":
// win(userChoice, computerChoice, userOutput, computerOutput);
win(userOutput, computerOutput);
console.log("user wins");
break;
case "rockpaper":
case "scissorrock":
case "paperscissor":
// loses(userChoice, computerChoice, userOutput, computerOutput);
loses(userOutput, computerOutput);
console.log("computer wins");
break;
case "rockrock":
case "scissorscissor":
case "paperpaper":
draw(userOutput, computerOutput);
console.log("draw");
break;
}
}