-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (68 loc) · 1.43 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
Vue.config.devtools = true;
const rand = () => Math.floor(Math.random() * 4);
const audio = (i) => new Audio(`./sounds/${i}.ogg`);
const app = new Vue({
el: '#root',
data() {
return {
running: false,
score: 0,
speeds: [1500, 1000, 400],
speed: 1000,
prevScore: -1,
playerTurn: false,
seq: [],
playerSeqInd: 0,
shown: -1,
};
},
methods: {
start() {
if (this.running || this.score === 10000) return this.stop();
this.running = true;
this.prevScore = -1;
this.seq = [];
this.simonTurn();
},
simonTurn() {
this.playerSeqInd = 0;
this.seq.push(rand());
let i = 0;
const show = () =>
setTimeout(() => {
if (!this.running) return;
if (i === this.seq.length) {
this.playerTurn = true;
this.shown = -1;
} else {
this.shown = this.seq[i++];
audio(this.shown).play();
pause();
show();
}
}, this.speed);
const pause = () =>
setTimeout(() => {
this.shown = -1;
}, this.speed - 100);
show();
},
playerPick(picked) {
if (!this.playerTurn) return;
audio(picked).play();
if (this.seq[this.playerSeqInd++] !== picked) return this.stop();
if (this.playerSeqInd === this.seq.length) {
this.score++;
this.playerTurn = false;
this.simonTurn();
}
},
stop() {
this.running = false;
this.prevScore = this.score;
this.score = 0;
this.shown = -1;
this.playerTurn = false;
},
},
});