-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
203 lines (167 loc) · 5.42 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const resultEl = document.querySelector('.result')
resultEl.innerText = ""
let computerCHoice
let getPlayerchoice
luck = Math.random()
const rockBtn = document.querySelector('.chrock')
const paperBtn = document.querySelector('.chpaper')
const scisorsBtn = document.querySelector('.chscisors')
rockBtn.addEventListener('click',setrock)
function setrock() {
getPlayerchoice ='rock'
}
paperBtn.addEventListener('click',setpaper)
function setpaper() {
getPlayerchoice ='paper'
}
scisorsBtn.addEventListener('click',setscisors)
function setscisors() {
getPlayerchoice ='scisors'
}
const urchoice = document.querySelector('.urchoice')
const cmpchoice = document.querySelector('.cmpchoice')
const action = document.querySelector(".action")
function oneGame(playerchoice){
setTimeout(() => {
cmpchoice.setAttribute('src',`./images/enemy/rock.svg`)
urchoice.setAttribute('src',`./images/you/rock.svg`)
}, 100);
action.setAttribute('class','action nochoice')
setTimeout(() => {
action.setAttribute('class','action ')
}, 3200);
let luck = Math.random()
if(luck<0.33){
computerCHoice = "rock"
}
else{
if (luck <0.66){
computerCHoice = "paper"
}
else {
computerCHoice = "scisors"
}
}
console.log("you chose : ",playerchoice)
console.log("computer chose",computerCHoice)
urchoice.setAttribute('class','urchoice animate')
cmpchoice.setAttribute('class','cmpchoice animate')
setTimeout(function animateChoice() {
cmpchoice.setAttribute('src',`./images/enemy/${computerCHoice}.svg`)
urchoice.setAttribute('src',`./images/you/${playerchoice}.svg`)
urchoice.setAttribute('class','urchoice ')
cmpchoice.setAttribute('class','cmpchoice ')}
,2200)
resultEl.innerHTML =""
if (computerCHoice === "rock"){
if (playerchoice === "rock") {
return "draw"
}
if (playerchoice === "paper") {
setTimeout(function animatewin(){
urchoice.setAttribute('class','urchoice win')},
2800)
playerScore++
console.log("win")
return "win"
}
if (playerchoice === "scisors") {
console.log("lose")
setTimeout(function animatelose(){
cmpchoice.setAttribute('class','cmpchoice win')},
2800)
computerScore++
return "lose"
}
}
if (computerCHoice === "scisors"){
if (playerchoice === "scisors") {
return "draw"
}
if (playerchoice === "rock") {
setTimeout(function animatewin(){
urchoice.setAttribute('class','urchoice win')},
2800)
playerScore++
console.log("win")
return "win"
}
if (playerchoice === "paper") {
setTimeout(function animatelose(){
cmpchoice.setAttribute('class','cmpchoice win')},
2800)
computerScore++
return "lose"
}
}
if (computerCHoice === "paper"){
if (playerchoice === "paper") {
return "draw"
}
if (playerchoice === "scisors") {
setTimeout(function animatewin(){
urchoice.setAttribute('class','urchoice win')},
2800)
console.log("win")
playerScore++
return "win"
}
if (playerchoice === "rock") {
setTimeout(function animatelose(){
cmpchoice.setAttribute('class','cmpchoice win')},
2800)
console.log("lose")
computerScore++
return "lose"
}
}
}
let i = 1
let playerScore = 0
let computerScore = 0
const roundNb = document.querySelector(".gameNumber")
function result() { return oneGame(getComputerCHoice(),
getPlayerchoice())}
const play = document.querySelector('.play')
const score = document.querySelector('.score')
const finalResult = document.querySelector('.finalResult')
function playOneGame(){
console.log("round",i)
result = oneGame(getPlayerchoice)
console.log(result)
setTimeout(() => {
resultEl.innerText =result
}, 2800);
setTimeout(() => {
score.innerText = `${playerScore}-${computerScore}`
}, 3000);
if(playerScore == 3 || computerScore == 3) {
setTimeout(() => {
if(playerScore == 3) {
finalResult.innerText = "Congatulations ! you've beaten the other cat"
}
else {
finalResult.innerText = "How bad ! the other cat beat you :("
}
gameState.setAttribute('class','gameOver')
}, 3500);
}
i++
}
const rePlayBtn = document.querySelector('.rePlayBtn')
function startgame() {
gameState.setAttribute('class','inGame')
}
function restart() {
history.go(0)
}
const startEL = document.querySelector('.playBtn')
const gameState = document.querySelector('div')
function game(){
startEL.addEventListener('click',startgame)
rockBtn.addEventListener('click',playOneGame)
scisorsBtn.addEventListener('click',playOneGame)
paperBtn.addEventListener('click',playOneGame)
rePlayBtn.addEventListener('click',restart)
}
game()