forked from GenXclub/RockPaperScissor-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (100 loc) · 3.06 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
let USERSCORE = 0;
let COMPSCORE = 0;
// Retriving Scores Text
const user_score = document.querySelector('.user-score');
const comp_score = document.querySelector('.comp-score');
// Reriving Options
const rock_option = document.querySelector('.rock-option');
const paper_option = document.querySelector('.paper-option');
const scissor_option = document.querySelector('.scissor-option');
// Retriving Result Text
const result_text = document.getElementById('result-text');
function game(user_option){
comp_option = computer()
option = user_option + comp_option;
switch(option){
case "rs":
case "pr":
case "sp": user_win(user_option,comp_option);
break;
case "rr":
case "pp":
case "ss": draw(user_option,comp_option);
break;
case "rp":
case "ps":
case "sr": comp_win(user_option,comp_option);
break;
}
function CapitalizeWords(letter){
switch(letter){
case 'r': return "ROCK"
case 'p': return "PAPER"
case 's': return "SCISSOR"
}
}
function ColorBorder(option,class_name){
switch(option){
case "r": rock_option.classList.add(class_name);
break;
case "p": paper_option.classList.add(class_name);
break;
case "s": scissor_option.classList.add(class_name);
}
}
function RemoveColorBorder(option,class_name){
switch(option){
case "r": rock_option.classList.remove(class_name);
break;
case "p": paper_option.classList.remove(class_name);
break;
case "s": scissor_option.classList.remove(class_name);
}
}
function user_win(Uo,Co){
CUo = CapitalizeWords(Uo)
CCo = CapitalizeWords(Co)
user_score.innerHTML = USERSCORE+=1
result_text.innerHTML = `${CUo} beats ${CCo}, You Win !!`
ColorBorder(Uo,'green-border')
setTimeout(function(){
RemoveColorBorder(Uo,"green-border")
},700)
}
function draw(Uo,Co){
CUo = CapitalizeWords(Uo)
CCo = CapitalizeWords(Co)
result_text.innerHTML = "Draw!!"
ColorBorder(Uo,'yellow-border')
setTimeout(function(){
RemoveColorBorder(Uo,"yellow-border")
},700)
}
function comp_win(Uo,Co){
CUo = CapitalizeWords(Uo)
CCo = CapitalizeWords(Co)
comp_score.innerHTML = COMPSCORE+=1
result_text.innerHTML = `${CCo} beats ${CUo}, You Lose !!`
ColorBorder(Uo,'red-border')
setTimeout(function(){
RemoveColorBorder(Uo,"red-border")
},700)
}
}
function computer(){
rps_array = ['r','p','s'];
random_value = Math.floor(Math.random() * 3)
return rps_array[random_value]
}
function main(){
rock_option.addEventListener('click',function(){
game("r")
})
paper_option.addEventListener('click',function(){
game("p")
})
scissor_option.addEventListener('click',function(){
game("s")
})
}
main();