-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (78 loc) · 2.89 KB
/
app.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
let userScore = 0;
let computerScore = 0;
const userScore_span = document.getElementById("user-score");
const computerScore_span =document.getElementById("computer-score");
const scoreBoard_div = document.querySelector(".score-board");
const result_p = document.querySelector(".result > p");
const rock_div = document.getElementById("r");
const paper_div = document.getElementById("p");
const scissors_div = document.getElementById("s");
const userSmall = 'USER'.fontsize(3).sub();
const computerSmall = 'COMPUTER'.fontsize(3).sub();
const choisesNames = {
'r' : 'ROCK',
'p' : 'PAPER',
's' : 'SCISSORS'
}
function getComputerChoise() {
const choises = ['r', 'p', 's'];
const randomNumber = Math.floor(Math.random() * 3);
return choises[randomNumber];
}
function win(userChoise, computerChoise) {
userScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result_p.innerHTML = `${choisesNames[userChoise]}${userSmall} beats ${choisesNames[computerChoise]}${computerSmall}. You win!`;
document.getElementById(userChoise).classList.add("green-glow");
setTimeout(function() {document.getElementById(userChoise).classList.remove("green-glow")}, 1000);
}
function lose(userChoise, computerChoise) {
computerScore++;
userScore_span.innerHTML = userScore;
computerSCore_span.innerHTML = computerScore;
result_p.innerHTML = `${choisesNames[computerChoise]}${computerSmall} beats ${choisesNames[userChoise]}${userSmall}. You lost!`;
document.getElementById(userChoise).classList.add("red-glow");
setTimeout(function() {document.getElementById(userChoise).classList.remove("red-glow")}, 1000);
}
function draw(userChoise, computerChoise) {
// userScore++;
// computerScore++;
// userScore_span.innerHTML = userScore;
// computerScore_span.innerHTML = computerScore;
result_p.innerHTML = `${choisesNames[computerChoise]}${computerSmall} equals ${choisesNames[userChoise]}${userSmall}. It's a draw!`;
document.getElementById(userChoise).classList.add("grey-glow");
setTimeout(function() {document.getElementById(userChoise).classList.remove("grey-glow")}, 1000);
}
function game(userChoise) {
const computerChoise = getComputerChoise();
switch (userChoise + computerChoise) {
case "rs":
case "pr":
case "sp":
win(userChoise, computerChoise);
break;
case "rp":
case "sr":
case "ps":
lose(userChoise, computerChoise);
break;
case "rr":
case "ss":
case "pp":
draw(userChoise, computerChoise);
break;
}
}
function main() {
rock_div.addEventListener("click", function(){
game("r");
})
paper_div.addEventListener("click", function(){
game("p");
})
scissors_div.addEventListener("click", function(){
game("s");
})
}
main();