-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
162 lines (119 loc) · 4.43 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
//Declarações de constantes e variáveis
let pageOne = document.querySelector(".page-one");
let pageTwo = document.getElementById("page-two").style.display = "none";
let pageThree = document.querySelector(".page-three").style.display = "none";
let adicionarPalavra = document.getElementById("adiciona");
let palavras = ["cachorro", "alura", "gato", "labora", "elefante",
"one", "java", "sprint", "oracle", "challenge", "casa", "carro"];
let palavraSecreta = palavras[Math.floor(Math.random() * palavras.length)];
const letrasCertas = [];
const letrasErradas = [];
let display = document.getElementById("display");
function checaInput(letra){
letra = letra.toLowerCase();
if(letrasErradas.includes(letra)){
}else{
if(palavraSecreta.includes(letra)){
letrasCertas.push(letra);
}else{
letrasErradas.push(letra);
}
}
atualizaJogo();
}
function isLetra(codigo) {
return codigo >= 65 && codigo <= 90;
}
function atualizaJogo(){
mostraLetrasErradas();
mostrarLetrasCertas();
desenhaForca();
verificaresultado();
}
function mostraLetrasErradas(){
const conteinerErro = document.querySelector(".letras-erradas");
conteinerErro.innerHTML = "";
letrasErradas.forEach(letra => {
conteinerErro.innerHTML += " " + letra;
});
}
function mostrarLetrasCertas() {
const container = document.querySelector(".letras-certas");
container.innerHTML ="";
palavraSecreta.split("").forEach(letra => {
if(letrasCertas.includes(letra)) {
container.innerHTML += letra;
}else {
container.innerHTML +=" _" ;
}
})
}
function verificaresultado(){
const container = document.getElementById("display").innerText;
const parteCorpo = document.querySelectorAll(".forca-parte");
let mensagem = "";
if(palavraSecreta.toUpperCase() === container){
document.querySelector(".page-three").style.display = "none";
mensagem = "Você Venceu." +"<br><br>"+ " Parabéns!";
document.getElementById("popu").style.color = "#15f409";
}
if(letrasErradas.length === parteCorpo.length){
mensagem = "Fim de Jogo, você perdeu." + "<br><br>" + "A palavra secreta é: " + "<br><br>" + "[ "+ palavraSecreta.toLocaleUpperCase() + " ]";
document.querySelector(".page-three").style.display = "none";
document.getElementById("popu").style.color = "#bc1b68";
}
if(mensagem){
document.querySelector("#mensagem").innerHTML = mensagem;
document.querySelector(".mensagem-popup").style.display = "flex";
}
}
function desenhaForca() {
const parteCorpo = document.querySelectorAll(".forca-parte");
for(let i = 0; i < letrasErradas.length; i++){
parteCorpo[i].style.display = "block";
}
}
function comecarJogo() {
pageOne.style.display = "none";
pageThree = document.querySelector(".page-three").style.display = "block";
atualizaJogo();
document.addEventListener("keydown", (event) => {
if(isLetra(event.keyCode)){
checaInput(event.key);
}
})
const btn = document.querySelectorAll(".letras-teclado");
btn.forEach(function(button) {
button.addEventListener('click', function(){
checaInput(button.innerHTML)
});
});
}
function salvarPalavra(){
let palavra = document.querySelector(".textarea-palavra");
if(palavra.value == ""){
alert("Digite uma palavra ou frase:");
}else{
palavras.push(palavra.value.normalize("NFD").replace(/[^a-zA-Zs]/g, " ").trim());
document.getElementById("page-two").style.display = "none";
document.querySelector(".page-three").style.display = "block";
comecarJogo();
}
}
function adicionaPalavra() {
pageOne.style.display = "none";
pageTwo = document.getElementById("page-two").style.display = "block";
document.getElementById("texto").focus();
}
function cancelarPalavra() {
pageTwo = document.getElementById("page-two").style.display = "none";
pageOne.style.display = "block";
}
function desistir(){
pageThree = document.querySelector(".page-three").style.display = "none";
pageOne.style.display = "block";
window.location.reload();
}
function novoJogo(){
window.location.reload();
}