-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (122 loc) · 4.26 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const game = () => {
let playerscore = 0;
let computerscore = 0;
const begin = () => {
const lets_begin = document.querySelector(".begin");
const introppart = document.querySelector(".intro");
const matchpart = document.querySelector(".match");
const score = document.querySelector(".score");
lets_begin.addEventListener("click",()=>{
introppart.classList.add("fadeout");
matchpart.classList.add("fadein");
score.classList.add("fadein");
});
}
const match = () => {
const options = document.querySelectorAll(".options button");
const playerhand = document.querySelector(".playerhand");
const computerhand = document.querySelector(".computerhand");
const hands = document.querySelectorAll(".hands img");
hands.forEach(hand => {
hand.addEventListener("animationend", function(){
this.style.animation = "";
});
})
//computer hand selection
const computeroptions = ["rock", "paper", "scissors"];
options.forEach(option => {
option.addEventListener("click", function () {
playerhand.src = `./rock.png`;
computerhand.src = `./rock.png`;
const computernumber = Math.floor(Math.random()*3);
const computerchoice = computeroptions[computernumber];
setTimeout(()=>{
comparehands(this.textContent,computerchoice);
changescore();
playerhand.src = `./${this.textContent}.png`;
computerhand.src = `./${computerchoice}.png`;
},2000);
playerhand.style.animation = "shakeplayer 2s ease";
computerhand.style.animation = "shakecomputer 2s ease";
});
});
};
const changescore = () => {
const result = document.querySelector(".result");
if(playerscore==10 || computerscore==10)
{
const winner = document.querySelector(".winner");
const hands = document.querySelector(".hands");
const options = document.querySelector(".options");
hands.classList.add("fadeout");
winner.classList.add("fadeout");
options.classList.add("fadeout");
if(playerscore==10)
{
result.textContent = "You Won!!";
}
else
{
result.textContent = "You Lose!!";
}
}
document.querySelector(".player_score p").innerHTML = `${playerscore}`;
document.querySelector(".computer_score p").innerHTML = `${computerscore}`;
}
const comparehands = (playerchoice,computerchoice)=>{
const winner = document.querySelector(".winner");
if(playerchoice===computerchoice)
{
winner.textContent = "Its a tie!!"
return;
}
if(playerchoice==='rock')
{
if(computerchoice==='scissors')
{
winner.textContent="You win!!";
playerscore++;
return;
}
else
{
winner.textContent="You Lose!!";
computerscore++;
return;
}
}
if(playerchoice==='paper')
{
if(computerchoice==='rock')
{
winner.textContent="You win!!";
playerscore++;
return;
}
else
{
winner.textContent="You Lose!!";
computerscore++;
return;
}
}
if(playerchoice==='scissors')
{
if(computerchoice==='paper')
{
winner.textContent="You win!!";
playerscore++;
return;
}
else
{
winner.textContent="You Lose!!";
computerscore++;
return;
}
}
}
begin();
match();
}
game();