Doubt in the exercise of stone, paper and scissors

Asked

Viewed 538 times

-1

The exercise is as follows:

Create a jokenpo game.

Each round the player sees the menu "Choose your move: 1-Paper, 2-Stone, 3-Scissors". The game reads the player’s option and checks if it is valid. If invalid, the player loses the round and the game ends. If valid, the computer chooses a random response, which is shown to the player.

If the player wins, he can play one more round and his score increases. The game ends when the player loses a round. The total score is shown at the end of the game.

Follows my code:

<html>
    <head>
    </head>
    <body>
        <script>

            var opcao=parseInt(prompt("Qual e a opcao  1 - Papel 2 - Pedra 3 - Tesoura"));
            var resposta=Math.floor((Math.random() * 3) + 1);
            var opcaoe+="";
            var verificador=true;

            while(verificador==true){
                if(resposta==1){
                    opcaoe+="a resposta e papel";
                }
                else if(resposta==2){
                    opcaoe+="a resposta e pedra";
                }
                else if(resposta==3){
                    opcaoe+="a resposta e tesoura";
                }

                if(opcao===resposta){
                    alert("a resposta e invalida"+opcaoe+".");
                    return verificador=false;
                }else if(opcao==1 && resposta==2){
                    alert("a resposta e valida"+opcaoe+".");
                    return verificador=true;
                }else if(opcao==2 && resposta==1){
                    alert("a resposta e invalida"+opcaoe+".");
                    return verificador=false;
                }else if(opcao==3 && resposta==1){
                    alert("a resposta e valida"+opcaoe+".");
                    return verificador=true;
                }else if(opcao==3 && resposta==2){
                    alert("a resposta e invalida"+opcaoe+".");
                    return verificador=false;
                }else if(opcao==2 && resposta==3){
                    alert("a resposta e valida"+opcaoe+".");
                    return verificador=true;
                }}else if(opcao==1 && resposta==3){
                    alert("a resposta e invalida"+opcaoe+".");
                    return verificador=false;
                }
            }
        </script>
    </body>
</html>

Doubt: Code does not work.

  • And what is your doubt?

  • The doubt is that the exercise does not speak

  • 1

    OK Diogo, pass more details, what error you are presenting, or what was the expected result and did not happen, etc.... https://answall.com/help/how-to-ask

  • I don’t see anything on the page and I think the problem is in the prompt because I don’t see the prompt box but I already tried to replace parseint by Parsefloat and still don’t give

  • What appears in the browser console?

  • Check the opening and closing of the if and while structures, and in the returns remove the "checker=" https://jsfiddle.net/y4mauehp/

Show 1 more comment

1 answer

1

The structure of your code is all wrong. These returns do not exist and this loop while is unnecessary and complicated to work in this case.

The best thing would be to do this with a function, which is much easier to work with:

function inicia(){
    var opcao=parseInt(prompt("Qual e a opcao  1 - Papel 2 - Pedra 3 - Tesoura"));
    var resposta=Math.floor((Math.random() * 3) + 1);
    var opcaoe = "";
    var verificador=true;

    if(resposta==1){
        opcaoe+="a resposta e papel";
    }
    else if(resposta==2){
        opcaoe+="a resposta e pedra";
    }
    else if(resposta==3){
        opcaoe+="a resposta e tesoura";
    }

    if(opcao===resposta){
        alert("a resposta e invalida"+opcaoe+".");
    }else if(opcao==1 && resposta==2){
        alert("a resposta e valida"+opcaoe+".");
        inicia();
    }else if(opcao==2 && resposta==1){
        alert("a resposta e invalida"+opcaoe+".");
    }else if(opcao==3 && resposta==1){
        alert("a resposta e valida"+opcaoe+".");
        inicia();
    }else if(opcao==3 && resposta==2){
        alert("a resposta e invalida"+opcaoe+".");
    }else if(opcao==2 && resposta==3){
        alert("a resposta e valida"+opcaoe+".");
        inicia();
    }else if(opcao==1 && resposta==3){
        alert("a resposta e invalida"+opcaoe+".");
    }
}

inicia();

function inicia(){
	var opcao=parseInt(prompt("Qual e a opcao  1 - Papel 2 - Pedra 3 - Tesoura"));
	var resposta=Math.floor((Math.random() * 3) + 1);
	var opcaoe = "";
	var verificador=true;

	if(resposta==1){
		opcaoe+="a resposta e papel";
	}
	else if(resposta==2){
		opcaoe+="a resposta e pedra";
	}
	else if(resposta==3){
		opcaoe+="a resposta e tesoura";
	}

	if(opcao===resposta){
		alert("a resposta e invalida"+opcaoe+".");
	}else if(opcao==1 && resposta==2){
		alert("a resposta e valida"+opcaoe+".");
		inicia();
	}else if(opcao==2 && resposta==1){
		alert("a resposta e invalida"+opcaoe+".");
	}else if(opcao==3 && resposta==1){
		alert("a resposta e valida"+opcaoe+".");
		inicia();
	}else if(opcao==3 && resposta==2){
		alert("a resposta e invalida"+opcaoe+".");
	}else if(opcao==2 && resposta==3){
		alert("a resposta e valida"+opcaoe+".");
		inicia();
	}else if(opcao==1 && resposta==3){
		alert("a resposta e invalida"+opcaoe+".");
	}
}

inicia();

Browser other questions tagged

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