-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
115 lines (72 loc) · 2.46 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
let userscore = 0;
let compscore = 0;
const choices = document.querySelectorAll(".choice");
let compscorepara = document.querySelector("#comp-score");
let userscorepara = document.querySelector("#user-score");
const msg = document.querySelector("#msg");
const drawgame = () => {
console.log("game was draw.");
msg.innerText = "Game Draw, Try Again ! ";
msg.style.backgroundColor = "rgb(7, 13, 49)";
}
// const userwin = () => {
// console.log("user is winner.");
// }
// const compwin = () => {
// console.log("computer is winner.");
// }
const gencompchoice = () => {
const options = ["rock", "paper", "scissor"];
const randidx = Math.floor(Math.random()*3);
return options[randidx];
}
const showwinner = (userwin, userchoice, compchoice) => {
if(userwin) {
userscore++;
userscorepara.innerText = userscore;
msg.innerText = `You Win! ${userchoice} beats ${compchoice}` ;
msg.style.backgroundColor = "green";
}
else{
compscore++;
compscorepara.innerText = compscore;
// msg.innerText = "You Loss ! ";
msg.innerText = `You Loss! ${compchoice} beats ${userchoice}` ;
msg.style.backgroundColor = "red";
}
}
const playgame = (userchoice) => {
console.log("user choice = ", userchoice);
const compchoice = gencompchoice();
console.log("computer choice = ", compchoice);
if(userchoice === compchoice){
drawgame();
}
else {
let userwin = true;
if(userchoice === "rock"){
userwin = compchoice === "paper" ? false : true;
}
else if(userchoice === "paper"){
userwin = compchoice === "scissor" ? false : true;
}
else{
userwin = compchoice === "rock" ? false : true;
}
showwinner(userwin, userchoice, compchoice);
}
// else if(userchoice === "stone" && compchoice == "paper" || userchoice === "paper" && compchoice == "scissor" || userchoice === "scissor" && compchoice == "stone"){
// compwin();
// }
// else {
// userwin();
// }
}
choices.forEach((choice) => {
// console.log(choice);
choice.addEventListener("click", () => {
const userchoice = choice.getAttribute("id");
// console.log("this", userchoice);
playgame(userchoice);
});
});