-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (149 loc) · 5.85 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const choices=["rock","paper","scissors","lizard","spock"] //all choices
//get points value from session storage so that even if user reloads the page points are intact
const points=parseInt(sessionStorage.getItem("points"))
if(points>0){
document.getElementById("points").textContent=points
}
//border colors according to the icons
const borderColor={
"rock":"hsl(349, 71%, 52%)",
"paper":"hsl(230, 89%, 62%)",
"scissors":"hsl(39, 89%, 49%)",
"lizard":"hsl(261, 73%, 60%)",
"spock":"hsl(189, 59%, 53%)"
}
//box-shadow color WRT icons
const shadowColor={
"rock":"hsl(349, 70%, 56%)",
"paper":" hsl(230, 89%, 65%)",
"scissors":"hsl(40, 84%, 53%)",
"lizard":"hsl(261, 72%, 63%)",
"spock":"hsl(189, 58%, 57%)"
}
//computer selected icon
const getRandomChoice=()=>{
const computerChoice=choices[Math.floor(Math.random()*choices.length)]
const computer=document.getElementById("computer")
computer.src=`./images/icon-${computerChoice}.svg`
document.getElementById("computer-div").style.display="block"
document.getElementById("computer-div").style.borderColor=`${borderColor[computerChoice]}`
if (window.matchMedia("(max-width:500px)").matches){
document.getElementById("computer-div").style.boxShadow=`3px 6px ${shadowColor[computerChoice]}`
}else{
document.getElementById("computer-div").style.boxShadow=`5px 10px ${shadowColor[computerChoice]}`
}
return computerChoice;
}
//when the user clicks on the icon
const OnClickChoice=(choice)=>{
document.getElementById("player").src=`./images/icon-${choice}.svg`; //setting icon image wrt icon
document.getElementById("player-div").style.borderColor=`${borderColor[choice]}`
if(window.matchMedia("(max-width:500px)").matches){
document.getElementById("player-div").style.boxShadow=`3px 6px ${shadowColor[choice]}`
}else{
document.getElementById("player-div").style.boxShadow=`5px 10px ${shadowColor[choice]}`
}
document.getElementById("icon-container").style.display="none";
document.getElementById("chosen-container").style.display="flex";
document.getElementById("result-container").style.display="flex"
document.getElementById("rules").style.marginTop="20%";
if(window.matchMedia("(min-width:1000px)").matches){
document.getElementById("rules").style.marginTop="2%";
}
}
const onWin=()=>{
let curr=parseInt(document.getElementById("points").textContent);
document.getElementById("points").textContent=curr+1
document.getElementById("result-heading").textContent="YOU WIN";
sessionStorage.setItem("points",`${document.getElementById("points").textContent}`)
}
const onLose=()=>{
let curr=parseInt(document.getElementById("points").textContent);
document.getElementById("points").innerText=curr-1
document.getElementById("result-heading").textContent="YOU LOSE";
sessionStorage.setItem("points",`${document.getElementById("points").textContent}`)
}
//Player Choices
const rockClick=()=>{
OnClickChoice("rock");
setTimeout(()=>{
const computerChoice=getRandomChoice()
if (computerChoice==="scissors" || computerChoice==="lizard"){
onWin();
}else if(computerChoice==="paper" || computerChoice==="spock"){
onLose();
}else{
document.getElementById("result-heading").textContent=""
}
},0);
}
const paperClick=()=>{
OnClickChoice("paper");
setTimeout(()=>{
const computerChoice=getRandomChoice();
if(computerChoice==="rock" || computerChoice==="spock"){
onWin();
}else if (computerChoice==="scissors" || computerChoice==="lizard"){
onLose();
}else{
document.getElementById("result-heading").textContent=""
}
},0)
}
const scissorsClick=()=>{
OnClickChoice("scissors");
setTimeout(()=>{
const computerChoice=getRandomChoice();
if(computerChoice==="paper" || computerChoice==="lizard"){
onWin();
}else if(computerChoice==="rock" || computerChoice==="spock"){
onLose();
}else{
document.getElementById("result-heading").textContent=""
}
},0)
}
const lizardClick=()=>{
OnClickChoice("lizard");
setTimeout(()=>{
const computerChoice=getRandomChoice();
if(computerChoice==="spock" || computerChoice==="paper"){
onWin();
}else if(computerChoice==="rock" || computerChoice==="scissors"){
onLose();
}else{
document.getElementById("result-heading").textContent=""
}
})
}
const spockClick=()=>{
OnClickChoice("spock");
setTimeout(()=>{
const computerChoice=getRandomChoice();
if(computerChoice==="scissors" || computerChoice==="rock"){
onWin();
}else if(computerChoice==="lizard" || computerChoice=="paper"){
onLose();
}else{
document.getElementById("result-heading").textContent=""
}
})
}
//PLAY AGAIN
const play=()=>{
document.getElementById("result-container").style.display="none";
document.getElementById("chosen-container").style.display="none";
document.getElementById("icon-container").style.display="block";
if(window.matchMedia("(max-width:500px)").matches){
document.getElementById("rules").style.marginTop="120%";
}else if(window.matchMedia("(min-width:1000px)").matches){
document.getElementById("rules").style.marginTop="2%";
}else{
document.getElementById("rules").style.marginTop="30%";
}
}
const restart=()=>{
document.getElementById("points").textContent="0";
sessionStorage.setItem("points","0");
play();
}