-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (137 loc) · 3.74 KB
/
script.js
File metadata and controls
156 lines (137 loc) · 3.74 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
148
149
150
151
152
153
154
155
156
const LIGHT_BLUE = document.getElementById("celeste");
const PURPLE = document.getElementById("violeta");
const ORANGE = document.getElementById("naranja");
const GREEN = document.getElementById("verde");
const btnStartGame = document.getElementById("btnEmpezar");
const MAX_LEVEL = 10;
class Game {
constructor() {
this.start();
this.generateSequence();
setTimeout(this.siguienteNivel, 500);
}
start() {
this.siguienteNivel = this.siguienteNivel.bind(this);
this.elegirColor = this.elegirColor.bind(this);
btnStartGame.classList.add("hide");
this.nivel = 1;
this.colores = {
celeste: LIGHT_BLUE,
violeta: PURPLE,
naranja: ORANGE,
verde: GREEN,
};
}
generateSequence() {
this.secuencia = new Array(MAX_LEVEL)
.fill()
.map(() => Math.floor(Math.random() * 4));
}
siguienteNivel() {
this.subnivel = 0;
this.iluminarSecuencia();
this.agregarEventosClick();
}
transformarNumeroAColor(numero) {
switch (numero) {
case 0:
return "celeste";
case 1:
return "violeta";
case 2:
return "naranja";
case 3:
return "verde";
default:
return "verde";
}
}
transformarColorANumero(color) {
switch (color) {
case "celeste":
return 0;
case "violeta":
return 1;
case "naranja":
return 2;
case "verde":
return 3;
default:
return "verde";
}
}
iluminarSecuencia() {
for (let i = 0; i < this.nivel; i++) {
const color = this.transformarNumeroAColor(this.secuencia[i]);
setTimeout(() => this.iluminarColor(color), 1000 * i);
}
}
iluminarColor(color) {
this.colores[color].classList.add("light");
setTimeout(() => this.apagarColor(color), 350);
}
apagarColor(color) {
this.colores[color].classList.remove("light");
}
agregarEventosClick() {
this.colores.celeste.addEventListener("click", this.elegirColor);
this.colores.verde.addEventListener("click", this.elegirColor);
this.colores.violeta.addEventListener("click", this.elegirColor);
this.colores.naranja.addEventListener("click", this.elegirColor);
}
eliminarEventosClick() {
this.colores.celeste.removeEventListener("click", this.elegirColor);
this.colores.verde.removeEventListener("click", this.elegirColor);
this.colores.violeta.removeEventListener("click", this.elegirColor);
this.colores.naranja.removeEventListener("click", this.elegirColor);
}
elegirColor(ev) {
const nombreColor = ev.target.dataset.color;
const numeroColor = this.transformarColorANumero(nombreColor);
this.iluminarColor(nombreColor);
if (numeroColor === this.secuencia[this.subnivel]) {
this.subnivel++;
if (this.subnivel === this.nivel) {
this.nivel++;
this.eliminarEventosClick();
if (this.nivel === MAX_LEVEL + 1) {
this.winGame();
} else {
this.winLevel();
}
}
} else {
this.loseGame();
}
}
deleteClickEvents() {
this.colors.forEach((item) =>
item.removeEventListener("click", this.chooseColor)
);
}
winGame() {
swal("Simon Colors", "Congratulations, you win!", "success").then(() =>
this.initialize()
);
}
loseGame() {
swal(
"You lose :(",
"But you can try it again, don't give up!",
"error"
).then(() => {
this.deleteClickEvents();
this.initialize();
});
}
winLevel() {
swal("Simon Colors", "Perfect!, next level", "success").then(() =>
setTimeout(this.siguienteNivel(), 800)
);
}
}
const startGame = () => {
swal("Simon Dice", "¡The game is about to start!", "success").then(() =>
setTimeout((window.juego = new Game()))
);
};