-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadivinar_main.js
More file actions
140 lines (112 loc) · 3.47 KB
/
adivinar_main.js
File metadata and controls
140 lines (112 loc) · 3.47 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
//shuffle functiuon
function shuffle(array) {
let currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
}
let tablero = document.getElementById('tablero');
let cols = 6
let rows = 5
//array de colores
let colores = [
'red', 'blue', 'green', 'yellow', 'orange', 'pink', 'brown', 'lightblue', 'lightgreen', 'purple', 'aquamarine', 'gray', 'greenyellow', 'orangered', 'lightcoral'
]
colores = [...colores, ...colores]
shuffle(colores)
//crear las 30 cajas
for (let row = 0; row < rows; row++){
for (let col = 0; col < cols; col++){
let box = document.createElement('div')
let color = colores.shift()
box.classList.add('box')
box.dataset.col = col;
box.dataset.row = row;
box.dataset.color = color;
box.dataset.open = 0;
box.dataset.paired = 0;
tablero.appendChild(box)
let comprobarColor = (event)=>{
event.target.style.backgroundColor = event.target.dataset.color;
event.target.dataset.open = 1;
let openedBox = document.querySelectorAll('[data-open = "1"]');
if (openedBox.length == 2){
if (openedBox[0].dataset.color === openedBox[1].dataset.color){
openedBox[0].removeEventListener('click', comprobarColor);
openedBox[1].removeEventListener('click', comprobarColor);
openedBox[0].dataset.open = 0;
openedBox[1].dataset.open = 0;
openedBox[0].dataset.paired = 1;
openedBox[1].dataset.paired = 1;
let pairedBoxes = document.querySelectorAll('[data-paired = "1"]');
if(pairedBoxes.length == cols*rows){
stopwatch.stop();
setTimeout(()=>{
alert('Juego Terminado!')
}, 500)
}
} else{
setTimeout(()=>{
openedBox[0].style.backgroundColor = 'black'
openedBox[1].style.backgroundColor = 'black'
openedBox[0].dataset.open = 0;
openedBox[1].dataset.open = 0;
}, 500)
}
}
}
box.addEventListener('click', comprobarColor)
}
}
//timer
var min,sec,ms,count, malt, salt, msalt;
var stopwatch = {
start: function(){
ms = 0;
sec = 0;
min = 0;
count = setInterval(function(){
if(ms == 100){
ms = 0;
if(sec == 60){
sec = 0;
min++;
}
else{
sec++;
}
}
else{
ms++;
}
malt = stopwatch.pad(min);
salt = stopwatch.pad(sec);
msalt = stopwatch.pad(ms);
stopwatch.update(malt + ":" + salt + ":" + msalt);
}, 10);
},
stop: function(){
clearInterval(count);
},
update: function(txt){
var temp = document.getElementById("timer");
temp.firstChild.nodeValue = txt;
},
pad: function(time){
var temp;
if(time < 10){
temp = "0" + time;
}
else{
temp = time;
}
return temp;
}
}
stopwatch.start()