-
Notifications
You must be signed in to change notification settings - Fork 0
/
juego.js
92 lines (66 loc) · 2.13 KB
/
juego.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
let palabraAzar;
let cantErrores = 0;
let cantAciertos = 0;
const btn = id('reiniciar-partida');
const imagen = id('img-ahorcado');
const btnLetras = document.querySelectorAll( "#letras button" );
btn.addEventListener('click', iniciar);
function iniciar(event){
id('resultado').innerHTML = "";
imagen.src = 'imagenes/Ahorcado0.png';
btn.disabled = true;
cantErrores = 0;
cantAciertos = 0;
const parrafo = id('palabra-a-adivinar');
parrafo.innerHTML = '';
const cantPalabras = palabras.length;
const valorAzar = obtenerRandom(0, cantPalabras);
palabraAzar = palabras[valorAzar];
const cantLetras = palabraAzar.length;
for (let i = 0; i < btnLetras.length; i++){
btnLetras[i].disabled = false;
}
for (let i = 0; i < cantLetras; i++){
const span = document.createElement('span');
parrafo.appendChild( span );
}
}
for(let i = 0; i < btnLetras.length; i++){
btnLetras[i].addEventListener( 'click', clickLetras );
}
function clickLetras(event){
console.log('hola');
const spans = document.querySelectorAll('#palabra-a-adivinar span');
const button = event.target;
button.disabled = true;
const letra = button.innerHTML.toUpperCase();
const palabra = palabraAzar.toUpperCase();
let acerto = false;
for(let i = 0; i < palabra.length; i++){
if(letra == palabra[i]){
spans[i].innerHTML = letra;
cantAciertos++;
acerto = true;
}
}
if(acerto == false){
cantErrores++;
const source = `imagenes/Ahorcado${cantErrores}.png`;
imagen.src = source;
}
if(cantErrores == 6){
id('resultado').innerHTML = "Perdiste, la palabra era " + palabraAzar;
gameOver();
}else if(cantAciertos == palabraAzar.length){
id('resultado').innerHTML = "¡¡¡GANASTE!!!";
gameOver();
}
console.log( "la letra " + letra + " en la palabra " + palabra + " ¿existe?: " + acerto );
}
function gameOver( ){
for(let i = 0; i < btnLetras.length; i++){
btnLetras[i].disabled = true;
}
btn.disabled = false;
}
gameOver();