Last active
April 5, 2020 16:45
-
-
Save ramcoelho/0a71cae7084b721afb2d0f49fd43abba to your computer and use it in GitHub Desktop.
Jogo "Senha" em Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="pt-br"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Senha</title> | |
<style> | |
body { | |
font-family: sans-serif; | |
} | |
#container { | |
width: 600px; | |
margin: auto; | |
} | |
#tentativas { | |
margin-top: 1em; | |
} | |
#tentativas div { | |
font-size: 0.8em; | |
margin-top: 0.5em; | |
} | |
.vitoria { | |
font-weight: bold; | |
color: green; | |
} | |
.oculto { | |
position: absolute; | |
visibility: hidden; | |
} | |
h1,button { | |
font-size: 1.3em; | |
} | |
#notas,#label-notas { | |
font-size: 0.8em; | |
margin-top: 0.5em; | |
} | |
#tentativa { | |
margin-top: 1em; | |
font-size: 1.3em; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>Senha</h1> | |
<input type="text" id="tentativa" maxlength="4" /> | |
<button id="tentar">Tentar</button> | |
<button class="oculto" id="reiniciar">Gerar Nova Senha</button> | |
<div id="label-notas">Notas:</div> | |
<textarea id='notas' cols="40" rows="10"></textarea> | |
<div id="tentativas"> | |
</div> | |
</div> | |
<script> | |
var contador; | |
var senha; | |
function inicializar() { | |
var letra; | |
contador = 0; | |
senha = ''; | |
for (var i = 0; i < 4; i++) { | |
letra = String.fromCharCode(65 + (Math.random() * 26)); | |
senha = senha + letra; | |
} | |
contador = 0; | |
document.getElementById('tentativa').value = ''; | |
document.getElementById('notas').value = ''; | |
document.getElementById('tentativas').innerHTML = ''; | |
document.getElementById('tentar').classList = ""; | |
document.getElementById('reiniciar').classList = "oculto"; | |
document.getElementById('tentativa').focus(); | |
} | |
function tentar() { | |
var tentativa = document.getElementById('tentativa').value.toUpperCase(); | |
var existem = 0; | |
var posicao = 0; | |
contador++; | |
for (var t = 0; t < 4; t++) { | |
for (var s = 0; s < 4; s++) { | |
if (senha.charAt(s) == tentativa.charAt(t)) { | |
existem++; | |
if (s == t) { | |
posicao++; | |
} | |
} | |
} | |
} | |
var div = document.createElement('div'); | |
var texto = document.createTextNode(tentativa + " (Existem: " + existem.toString() + ", Na posição: " + posicao.toString() + ")"); | |
div.appendChild(texto); | |
document.getElementById('tentativas').prepend(div); | |
document.getElementById('tentativa').select(); | |
if (posicao == 4) { | |
var div = document.createElement('div'); | |
div.classList = "vitoria"; | |
var texto = document.createTextNode("Parabéns! A senha era " + senha + ". Você precisou de " + contador.toString() + " tentativas."); | |
div.appendChild(texto); | |
document.getElementById('tentativas').appendChild(div); | |
document.getElementById('tentar').classList = "oculto"; | |
document.getElementById('reiniciar').classList = ""; | |
} | |
} | |
inicializar(); | |
document.getElementById("tentar").addEventListener("click", tentar); | |
document.getElementById("reiniciar").addEventListener("click", inicializar); | |
document.getElementById("tentativa").addEventListener("keyup", function(e) { | |
if (13 == e.keyCode) { | |
tentar(); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment