-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (158 loc) · 5.26 KB
/
index.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
const timeLeftDisplay = document.querySelector('#time-left')
const resultDisplay = document.querySelector('#result')
const startPauseButton = document.querySelector('#start-pause-button')
const squares = document.querySelectorAll('.grid div')
const logsLeft = document.querySelectorAll('.log-left')
const logsRight = document.querySelectorAll('.log-right')
const carsLeft = document.querySelectorAll('.car-left')
const carsRight = document.querySelectorAll('.car-right')
let currentIndex = 76;
let width = 9;
let timerId;
let outcomeTimerId;
let currentTime = 20;
const moveFrog = (e) => {
squares[currentIndex].classList.remove('frog')
switch(e.key) {
case 'ArrowLeft' :
if (currentIndex % width !== 0) currentIndex -= 1
break
case 'ArrowRight' :
if (currentIndex % width < width - 1) currentIndex += 1
break
case 'ArrowUp' :
if (currentIndex - width >=0 ) currentIndex -= width
break
case 'ArrowDown' :
if (currentIndex + width < width * width) currentIndex += width
break
}
squares[currentIndex].classList.add('frog')
}
const autoMoveElements = () => {
currentTime--
timeLeftDisplay.textContent = currentTime
logsLeft.forEach(logLeft => moveLogLeft(logLeft))
logsRight.forEach(logRight => moveLogRight(logRight))
carsLeft.forEach(carLeft => moveCarLeft(carLeft))
carsRight.forEach(carRight => moveCarRight(carRight))
}
const moveLogLeft = (logLeft) => {
switch(true) {
case logLeft.classList.contains('l1') :
logLeft.classList.remove('l1')
logLeft.classList.add('l2')
break
case logLeft.classList.contains('l2') :
logLeft.classList.remove('l2')
logLeft.classList.add('l3')
break
case logLeft.classList.contains('l3') :
logLeft.classList.remove('l3')
logLeft.classList.add('l4')
break
case logLeft.classList.contains('l4') :
logLeft.classList.remove('l4')
logLeft.classList.add('l5')
break
case logLeft.classList.contains('l5') :
logLeft.classList.remove('l5')
logLeft.classList.add('l1')
break
}
}
const moveLogRight = (logRight) => {
switch(true) {
case logRight.classList.contains('l1') :
logRight.classList.remove('l1')
logRight.classList.add('l5')
break
case logRight.classList.contains('l2') :
logRight.classList.remove('l2')
logRight.classList.add('l1')
break
case logRight.classList.contains('l3') :
logRight.classList.remove('l3')
logRight.classList.add('l2')
break
case logRight.classList.contains('l4') :
logRight.classList.remove('l4')
logRight.classList.add('l3')
break
case logRight.classList.contains('l5') :
logRight.classList.remove('l5')
logRight.classList.add('l4')
break
}
}
function moveCarLeft(carLeft) {
switch(true) {
case carLeft.classList.contains('c1') :
carLeft.classList.remove('c1')
carLeft.classList.add('c2')
break
case carLeft.classList.contains('c2') :
carLeft.classList.remove('c2')
carLeft.classList.add('c3')
break
case carLeft.classList.contains('c3') :
carLeft.classList.remove('c3')
carLeft.classList.add('c1')
break
}
}
const moveCarRight = (carRight) => {
switch(true) {
case carRight.classList.contains('c1') :
carRight.classList.remove('c1')
carRight.classList.add('c3')
break
case carRight.classList.contains('c2') :
carRight.classList.remove('c2')
carRight.classList.add('c1')
break
case carRight.classList.contains('c3') :
carRight.classList.remove('c3')
carRight.classList.add('c2')
break
}
}
const checkOutComes = () => {
lose()
win()
}
const lose = () => {
if(
squares[currentIndex].classList.contains('c1')
|| squares[currentIndex].classList.contains('l4')
|| squares[currentIndex].classList.contains('l5')
|| currentTime <= 0
) {
resultDisplay.textContent = 'You lose!'
clearInterval(timerId)
clearInterval(outcomeTimerId)
squares[currentIndex].classList.remove('frog')
document.removeEventListener('keyup', moveFrog)
}
}
const win = () => {
if(squares[currentIndex].classList.contains('ending-block')) {
resultDisplay.textContent = 'You win!'
clearInterval(timerId)
clearInterval(outcomeTimerId)
document.removeEventListener('keyup', moveFrog)
}
}
startPauseButton.addEventListener('click', () => {
if(timerId) {
clearInterval(timerId)
clearInterval(outcomeTimerId)
timerId = null
outcomeTimerId = null
document.removeEventListener('keyup', moveFrog)
} else {
timerId = setInterval(autoMoveElements, 1000)
outcomeTimerId = setInterval(checkOutComes, 50)
document.addEventListener('keyup', moveFrog)
}
})