-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (69 loc) · 2.94 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
let body = document.querySelector("body");
const user_board = document.querySelector(".user");
const computer_board = document.querySelector(".computer");
const message_box = document.querySelector(".message")
let user_score = 0;
let computer_score = 0;
// const Rock = document.querySelector("#Rock")
// const Paper = document.querySelector("#Paper")
// const Scissors = document.querySelector("#Scissors")
const choices = ["Rock", "Paper", "Scissors"]
const generateComputerResponse = () => {
const index = ((Math.random() * 10).toFixed(0)) % 3
return choices[index]
}
const result = (winner, userResponse, computerResponse) => {
switch (winner) {
case "user":
// display message
message_box.innerHTML = `User Wins !! 😍😍<br/> Computer responce - ${computerResponse} `
document.querySelector(`#${userResponse}`).classList.add('won')
user_board.innerHTML = ++user_score
break
case "computer":
message_box.innerHTML = `Computer Wins !! 😢😢<br/> Computer responce - ${computerResponse} ` //beats ${userResponse}
computer_board.innerHTML = ++computer_score
break
case "Tie":
message_box.innerHTML = `Tie !! 😁😁<br/> Computer responce - ${computerResponse} `
break
default:
window.alert("something went wrong")
}
}
const playGame = (userResponse) => {
const computerResponse = generateComputerResponse()
if (userResponse === computerResponse) console.log('Tie!!')
switch (userResponse) {
case "Rock":
switch (computerResponse) {
case "Paper":
return result("computer", userResponse, computerResponse)
case "Scissors":
return result("user", userResponse, computerResponse)
case "Rock":
return result("Tie", userResponse, computerResponse)
}
case "Paper":
switch (computerResponse) {
case "Rock":
return result("user", userResponse, computerResponse)
case "Scissors":
return result("computer", userResponse, computerResponse)
case "Paper":
return result("Tie", userResponse, computerResponse)
}
case "Scissors":
switch (computerResponse) {
case "Rock":
return result("computer", userResponse, computerResponse)
case "Paper":
return result("user", userResponse, computerResponse)
case "Scissors":
return result("Tie", userResponse, computerResponse)
}
}
}
Rock.addEventListener('click', (e) => playGame(e.target.id))
Paper.addEventListener('click', (e) => playGame(e.target.id))
Scissors.addEventListener('click', (e) => playGame(e.target.id))