Trouble with hangman play

Asked

Viewed 1,005 times

0

I’m trying to create a hangman’s game. The game itself is almost ready.

The problem is when he checks to see if the letter matches the typed one. I checked if the letter was the same and if it was not he gave a value to the control variable and depending on the value he took 1 of the odds (plays), but even hitting he keeps taking 1 of the play.

<html>
<head>
    <title>Jogo da Forca</title>
    <script type="text/javascript">
        var palavra=new Array();
        var controlando=0;
        var cont=0;
        var tracos=[];
        var conpt=0;//controle
        var jogadas=5;
function preencher (valor){
        var elemento = document.getElementById("tela");
        var value= elemento.value;
        if (controlando==0){
             elemento.value=value+valor;

        }
        if(controlando==1){
            preenchimento(valor);
            alert(jogadas);

                }}

}
function preenchimento(valor){
            var elemento = document.getElementById("resp");
               var value= elemento.value;
                var checando=0;
            for(var i=0; i<palavra.length;i++){
               if (valor == palavra[i]){
                   tracos[i]=valor;
                   document.getElementById(valor).disabled = true;
                    conpt=2;
               }
                else 
                    conpt=1;
            }
    if (conpt==1)
        jogadas=jogadas-1;




               elemento.value=tracos;


    }



function backspace(campo) {
    valor = campo.value;
    tamanho = valor.length
    campo.value = valor.substring(0, tamanho-1)
        }
function iniciar(tela){
        var copia= tela.value;
        document.getElementById("tela").disabled = 1; //checar se pode
        palavra=copia;
        controlando=1;
        criarTracos();
    }
function criarTracos(valor){
            var elemento = document.getElementById("resp");
            var tam = palavra.length;
            for (var i=0; i<tam;i++)
            {tracos[i]="__";}
             elemento.value=tracos;

            }



    //https://www.youtube.com/watch?v=aVp232wRQyE&index=17&list=UUezdgg4HLLhPwGKNfJzaBww
    </script>

</head>
<body>
<div id="all" >
<h1>JOGO DA FORCA</h1>

    <div id="campo">
        Palavra: <input type="password" id="tela" value=""/>
    </div>
      <input type="text" id="resp" value="" onload="criarTracos();"/>
    <div id="teclas">
        <br/>
                <input type="button" value="Q" id="Q" onClick="preencher(value);" >
                <input type="button" value="W" id="W" onClick="preencher(value);"   >
                <input type="button" value="E" id="E" onClick="preencher(value);" >
                <input type="button" value="R" id="R" onClick="preencher(value);"  >
                <input type="button" value="T" id="T" onClick="preencher(value);">
                <input type="button" value="Y" id="Y" onClick="preencher(value);">
                <input type="button" value="U" id="U" onClick="preencher(value);">
                <input type="button" value="I" id="I" onClick="preencher(value);">
                <input type="button" value="O" id="P" onClick="preencher(value);">

        <br/>
                <input type="button" value="A" id="A" onClick="preencher(value);">
                <input type="button" value="S" id="S" onClick="preencher(value);">
                <input type="button" value="D" id="D" onClick="preencher(value);" >
                <input type="button" value="F" id="F" onClick="preencher(value);">
                <input type="button" value="G" id="G" onClick="preencher(value);">
                <input type="button" value="H" id="H" onClick="preencher(value);" >
                <input type="button" value="J" id="J" onClick="preencher(value);">
                <input type="button" value="K" id="K" onClick="preencher(value);">
                <input type="button" value="L" id="L" onClick="preencher(value);">

        <br />
                <input type="button" value="Z" id="Z" onClick="preencher(value);">
                <input type="button" value="X"id="X" onClick="preencher(value);">
                <input type="button" value="C" id="C" onClick="preencher(value);" >
                <input type="button" value="V" id="V" onClick="preencher(value);">
                <input type="button" value="B" id="B" onClick="preencher(value);">
                <input type="button" value="N" id="N" onClick="preencher(value);">
                <input type="button" value="M" id="M" onClick="preencher(value);" >
        <input type="button" value="APAGAR" id="APAGAR" onClick="backspace(tela);" style="width=1000px;">
        <br/>
     </div>
     <br />
 <input type="button"  value="iniciar" id="iniciar" onClick="iniciar(tela);" >
</div>


</body>
</html>
  • 1

    What is the variable renan?

  • 2

    You haven’t said what’s wrong.

  • What exactly is going wrong? try using the jsfiddle to show what’s going wrong.

1 answer

1


Let me reformat your javascript:

    var palavra = new Array();
    var controlando = 0;
    var cont = 0;
    var tracos = [];
    var conpt = 0; // controle
    var jogadas = 5;

    function preencher(valor) {
        var elemento = document.getElementById("tela");
        var value = elemento.value;
        if (controlando == 0) {
            elemento.value = value + valor;
        }
        if (controlando == 1) {
            preenchimento(valor);
            alert(jogadas);
        }
    }

} // ESTE DAQUI ESTÁ SOBRANDO

function preenchimento(valor) {
    var elemento = document.getElementById("resp");
    var value = elemento.value;
    var checando = 0;
    for (var i = 0; i < palavra.length; i++) {
        if (valor == palavra[i]) {
            tracos[i] = valor;
            document.getElementById(valor).disabled = true;
            conpt = 2;
        }
        else 
            conpt = 1;
    }

    if (conpt == 1)
        jogadas = jogadas - 1;

    elemento.value = tracos;
}

function backspace(campo) {
    valor = campo.value;
    tamanho = valor.length // Faltou o ponto-e-vírgula.
    campo.value = valor.substring(0, tamanho - 1) // Faltou o ponto-e-vírgula.
}

function iniciar(tela) {
    var copia = tela.value;
    document.getElementById("tela").disabled = 1; //checar se pode
    palavra = copia;
    controlando = 1;
    criarTracos();
}

function criarTracos(valor) {
    var elemento = document.getElementById("resp");
    var tam = palavra.length;
    for (var i = 0; i < tam; i++) {
        tracos[i] = "__";
    }
    elemento.value = tracos;
}

It seems to me that you just got lost in time to see what is opening and what is closing, and because of that your program does not work well because there is something that is not closing where it should. Or some code you thought you put inside some function, but actually put out.

In your example there is a key lock that does not open anywhere. As your code is a total mess due to lack of identation, it is difficult to realize this.

So here are the tips:

  • Find your code properly. Just doing this should make it clear what your mistakes are.
  • Always use the keys in blocks if, else, for, while and do-while, otherwise you’ll get lost easy.

But your big problem is here:

for (var i = 0; i < palavra.length; i++) {
    if (valor == palavra[i]) {
        tracos[i] = valor;
        document.getElementById(valor).disabled = true;
        conpt = 2;
    }
    else 
        conpt = 1;
}

if (conpt == 1)
    jogadas = jogadas - 1;

Note that it defines the conpt with 2 whenever the letter is correct and 1 when it is incorrect. That is, if there was a correct letter, but then an incorrect one, the value will end up being 1! And here’s the error, it means that only the last letter matters.

Let’s fix it:

var conpt = false;
for (var i = 0; i < palavra.length; i++) {
    if (valor == palavra[i]) {
        tracos[i] = valor;
        document.getElementById(valor).disabled = true;
        conpt = true;
    }
}

if (!conpt)
    jogadas = jogadas - 1;

And then you remove the var conpt = 0; which is there at the beginning, as you are already declaring it and using it only within the function preenchimento.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.