This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathsimon.js
More file actions
147 lines (147 loc) · 3.9 KB
/
simon.js
File metadata and controls
147 lines (147 loc) · 3.9 KB
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
new Vue({
el: "#app",
data: {
colors: ["red", "green", "yellow", "blue"],
currentSequence: [],
colorStatus: {
red: false,
green: false,
yellow: false,
blue: false
},
colorSymbols: {
red: "❤️",
green: "💚",
yellow: "💛",
blue: "💙"
},
userClicks: [],
currentScore: 0,
difficulty: 1000
},
created() {
this.generateSequence();
},
watch: {
winner: function(isWinner) {
if (isWinner) {
this.changeTheme();
}
}
},
computed: {
logoTitleRed: function() {
return this.colorStatus.red ? "logo-tile--largered" : "logo-tile--red";
},
logoTitleGreen: function() {
return this.colorStatus.green
? "logo-tile--largegreen"
: "logo-tile--green";
},
logoTitleYellow: function() {
return this.colorStatus.yellow
? "logo-tile--largeyellow"
: "logo-tile--yellow";
},
logoTitleBlue: function() {
return this.colorStatus.blue ? "logo-tile--largeblue" : "logo-tile--blue";
},
winner: function() {
return this.currentScore >= 10 ? true : false;
}
},
methods: {
generateSequence: function() {
this.currentSequence.push(
this.colors[this.getRandomInt(this.colors.length)]
);
if (this.currentScore > 0) {
this.play();
} else {
console.log(
`🕵️♂️🕵️♀️ PSSST! There is a game inside this webpage...\nClick the ${
this.colorSymbols[this.currentSequence[0]]
} tile on the Microsoft logo to play!`
);
}
},
clickColor: function(color) {
this.userClicks.push(color);
if (this.checkCorrect()) {
this.userClicks = [];
if (this.difficulty > 100) {
this.difficulty -= 100;
}
this.currentScore++;
console.log(
`Simon says..."correct!" 🎉 Your score is`,
this.currentScore
);
this.lightUp();
this.generateSequence();
}
},
getRandomInt: function(max) {
return Math.floor(Math.random() * Math.floor(max));
},
timer: function(ms) {
return new Promise(res => setTimeout(res, ms));
},
checkCorrect: function() {
let matching = true;
let sequence =
this.userClicks[this.userClicks.length - 1] ===
this.currentSequence[this.userClicks.length - 1];
if (!sequence) {
this.reset();
}
for (let i = 0; i < this.currentSequence.length; i++) {
if (this.userClicks[i] !== this.currentSequence[i]) {
matching = false;
break;
}
}
return matching;
},
reset: function() {
console.log(`😢 Game Over! Refresh to play again.`);
this.currentSequence = [];
this.userClicks = [];
this.currentScore = 0;
this.difficulty = 1000;
this.generateSequence();
},
play: async function() {
await this.timer(2000);
for (var i = 0; i < this.currentSequence.length; i++) {
this.colorStatus[this.currentSequence[i]] = true;
await this.timer(this.difficulty);
this.colorStatus[this.currentSequence[i]] = false;
await this.timer(this.difficulty / 2);
}
},
lightUp: async function() {
let t = 150;
this.colorStatus.red = true;
await this.timer(t);
this.colorStatus.red = false;
this.colorStatus.green = true;
await this.timer(t);
this.colorStatus.green = false;
this.colorStatus.yellow = true;
await this.timer(t);
this.colorStatus.yellow = false;
this.colorStatus.blue = true;
await this.timer(t);
this.colorStatus.blue = false;
this.colorStatus.red = true;
await this.timer(t);
this.colorStatus.red = false;
},
changeTheme: function() {
if (theme && theme.changeTo) {
theme.changeTo("msdos");
}
}
}
});