-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRock,Paper,scissors.js
85 lines (81 loc) · 2.79 KB
/
Rock,Paper,scissors.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
let userchoice = 0;
let compscore = 0;
const choice = document.querySelectorAll(".box");
const msg = document.querySelector(".msg");
let userpara=document.querySelector(".user");
let comppara=document.querySelector(".comp");
const computerchoice = () => {
const options = ["Rock", "Paper", "Scissors"];
const randidx = Math.floor(Math.random() * 3);
return options[randidx];
}
const playgame = (userid) => {
console.log("User Choice=", userid);
const compch = computerchoice();
console.log("Computer Choice=", compch);
if (userid == compch) {
console.log("Match draw");
msg.innerText="GAME IS DRAW";
msg.style.backgroundColor="Black";
}
else {
let userwin = true;
if (userid === "Paper") {
if (compch === "Scissors") {
userwin = false;
compchoice++;
comppara.innerText=compchoice;
console.log("Computer has won");
msg.innerText="COMPUTER WON";
msg.style.backgroundColor="Red";
} else {
userwin = true;
userchoice++;
userpara.innerText=userchoice;
console.log("Player has won");
msg.innerText="PLAYER WON";
msg.style.backgroundColor="Green";
}
}
if (userid === "Rock") {
if (compch === "Paper") {
userwin = false;
compchoice++;
comppara.innerText=compchoice;
console.log("Computer has won");
msg.innerText="COMPUTER WON";
msg.style.backgroundColor="Red";
} else {
userwin = true;
userchoice++;
userpara.innerText=userchoice;
console.log("Player has won");
msg.innerText="PLAYER WON";
msg.style.backgroundColor="Green";
}
}
if (userid === "Scissors") {
if (compch === "Rock") {
userwin = false;
compchoice++;
comppara.innerText=compchoice;
console.log("Computer has won");
msg.innerText="COMPUTER WON";
msg.style.backgroundColor="Red";
} else {
userwin = true;
userchoice++;
userpara.innerText=userchoice;
console.log("Player has won");
msg.innerText="PLAYER WON";
msg.style.backgroundColor="Green";
}
}
}
}
choice.forEach((box) => {
box.addEventListener("click", () => {
const userid = box.getAttribute("id");
playgame(userid);
});
});