-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (128 loc) · 3.09 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
const movements = [];
const buttons = [];
const useMovement = [];
let isUserTurn = false;
function init() {
buttons.push({
key: 'red',
element: document.getElementById('red')
});
buttons.push({
key: 'blue',
element: document.getElementById('blue')
});
buttons.push({
key: 'green',
element: document.getElementById('green')
});
buttons.push({
key: 'yellow',
element: document.getElementById('yellow')
});
for (let i = 0; i < buttons.length; i++) {
buttons[i].element.onclick = function() {
if (isUserTurn) {
animateButton(buttons[i].key);
addUserMovement(buttons[i].key);
if (!checkStatus()) {
console.log('you loose');
looseAnimation().then(() => {
reset();
});
} else {
if (useMovement.length === movements.length) {
// next level
useMovement.length = 0;
isUserTurn = false;
setTimeout(() => {
animateAllPreviousSteps().then(() => {
addMovement().then(() => {
isUserTurn = true;
});
});
}, 2000);
}
}
} else {
console.log('wait for your turn');
}
};
}
// initial movement on init
addMovement().then(() => {
isUserTurn = true;
});
}
function addUserMovement(move) {
useMovement.push(move);
}
function reset() {
movements.length = 0;
useMovement.length = 0;
for (var i = 0; i < buttons.length; i++) {
animateButton(buttons[i].key)
}
setTimeout(() => {
addMovement();
}, 2000);
}
function addMovement() {
const next = Math.floor(Math.random() * buttons.length);
movements.push(buttons[next].key);
return animateButton(buttons[next].key);
}
function checkStatus() {
let valid = true;
if(useMovement[useMovement.length - 1] !== movements[useMovement.length - 1]) {
valid = false;
}
return valid;
}
function animateButton(key) {
return new Promise((resolve) => {
let buttonReference;
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].key === key) {
buttonReference = buttons[i];
break;
}
}
buttonReference.element.classList.add('active');
setTimeout(() => {
buttonReference.element.classList.remove('active');
setTimeout(() => {
resolve();
}, 200);
}, 500);
});
}
function animateAllPreviousSteps([current, ...tail] = movements) {
return animateButton(current).then(() => {
if (tail.length) {
return animateAllPreviousSteps(tail);
} else {
return Promise.resolve();
}
});
}
function looseAnimation() {
return new Promise((resolve) => {
for (var i = 0; i < buttons.length; i++) {
animateButton(buttons[i].key);
}
setTimeout(() => {
for (var i = 0; i < buttons.length; i++) {
animateButton(buttons[i].key);
}
}, 500);
setTimeout(() => {
for (var i = 0; i < buttons.length; i++) {
animateButton(buttons[i].key);
}
}, 2000);
setTimeout(() => {
resolve();
}, 5000);
})
}
init();