-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (139 loc) · 4.38 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
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
const cells = document.getElementsByClassName("num-cell");
const wrapper = document.getElementById("wrapper");
const timer = document.getElementById("timer");
const maxTimerSize = parseInt(getComputedStyle(timer).width);
let timerWidth = maxTimerSize;
let timerID = 1;
let timeMs = 100;
let difficulty = 1;
let total = 0;
let objetivo = 0;
let nivel = 1;
let puntos = 0;
let amountOfTiles = 2;
cellsIndex = [1, 2, 3, 4, 5, 6, 7, 8, 9]
function selectedListener() {
// Esta función se encarga de delegar los eventos a las celdas
wrapper.addEventListener("click", (e) => {const clickedCell = e.target.parentElement
const cellIsActive = clickedCell.classList.contains("active")
const cellIsSelected = clickedCell.classList.contains("selected")
if (cellIsActive) {
clickedCell.classList.toggle("selected");
if (cellIsSelected) {
total -= parseInt(e.target.innerHTML);
} else {
total += parseInt(e.target.innerHTML);
}
}
if (checkWin(objetivo)) {
newProblem()
}
})
}
function checkWin(num) {
// chequea si la suma es igual al total
return total === num
}
async function resetTiles() {
// Aqui hago una selección aleatoria de algunas celdas para activarla
// TODO: cambiar el número de celdas seleccionadas modificando el slice
// Itero por las celdas para desactivarlas si es que están activadas
for (const cell of cells) {
cell.classList.remove("selected")
cell.classList.replace("active", "inactive")
};
}
function hideWrapper() {
wrapper.classList.toggle("hidden");
}
function activateTiles(arrayOfIndex, cells) {
for (const index of arrayOfIndex) {
cells[index - 1].classList.replace("inactive", "active")
}
}
function selectRandomTilesIndex(n = 9) {
const randomTilesIndex = cellsIndex.sort(
function () { return Math.random() - 0.5; }
);
return randomTilesIndex.slice(0, n)
}
function setCellNums(arrayOfValues, arrayOfCellsIndex) {
let i = 0;
for (index of arrayOfCellsIndex) {
cells[index - 1].firstElementChild.innerHTML = arrayOfValues[i];
i++
}
}
function setObjective(num) {
const numObjetivo = document.getElementById("num-objetivo");
numObjetivo.innerHTML = num;
}
function newProblem() {
wrapper.classList.toggle("hidden");
puntos++;
nextLevel();
wrapper.classList.toggle("hidden");
}
function nextLevel() {
amountOfTiles = Math.floor(puntos / 5 + 2);
timeMs = Math.max(2, Math.floor((-0.02 * puntos ** 2 + 10))) * 10;
newGame(amountOfTiles)
}
function randomSign() {
return (Math.random() > 0.5) ? -1 : 1
}
// Crea un array aleatorio que será usado para generar la respuesta correcta
// Agregar un slice a partir del cuál será la respuesta
// TODO: cambiar el largo según parámetros
function randomNumGenerator(n = 3) {
difficulty = Math.floor((puntos ** 2) * 0.05);
const randomArray = Array.from(
{ length: n },
() => randomSign() * Math.floor(((Math.random() - 0.5)) * 10) + difficulty
);
() => Math.floor(((Math.random() - 0.5)) * 10)
return randomArray
}
// set the width from a given percentage
function timerPercentage(percentage) {
timerWidth -= Math.floor((percentage * maxTimerSize) / 100);
timer.style.width = `${timerWidth}px`;
}
function animateTimer() {
if (timerWidth > 10) {
timerPercentage(1)
} else {
clearInterval(timerID)
gameOver()
}
}
function setTimer(time) {
return setInterval(animateTimer, time)
}
async function newGame(tiles) {
// Generamos los números que estarán en el tablero
hideWrapper();
clearInterval(timerID)
const cellsToActivate = selectRandomTilesIndex(tiles);
const nums = randomNumGenerator(tiles);
// Selecciono 3 para la respuesta
objetivo = nums.slice(0, tiles - 1).reduce((a, b) => a + b, 0);
// reseteo el tablero, establezco un objetivo y colocamos las piezas
await resetTiles();
activateTiles(cellsToActivate, cells);
setCellNums(nums, cellsToActivate);
setObjective(objetivo);
timerPercentage(0);
timerWidth = maxTimerSize;
timerID = setTimer(timeMs);
total = 0;
hideWrapper();
}
function gameOver() {
alert(`¡perdiste! tu puntaje fue de ${puntos}`)
puntos = 0;
timeMs = 100;
newGame(3);
}
selectedListener()
newGame(3)