0
I had made this game using textbox, and everything worked ok, but I decided to make a modification and instead of typing put respective images to the jokenpô and the image that the user click will be compared and return to the user who won, at first would be done in Alert same. My problem is taking the user option and assigning it to a variable and making that comparison. How can I do this?
$(document).ready(function(){
var choice = null;
//Gera a opção do computador
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Pedra";
} else if(computerChoice <= 0.67) {
computerChoice = "Papel";
} else {
computerChoice = "Tesoura";
}
//Pedra
$('#pedra, #papel, #tesoura').on('click',function(){
if((computerChoice == choice) || (computerChoice == choice)||(computerChoice == choice))
alert("Empate");
else if (choice === "pedra") {
if (computerChoice === "tesoura")
alert("pedra vence");
else {
alert("papel vence");
}
}
else if (choice === "papel") {
if (computerChoice === "pedra")
alert("papel vence");
else {
alert("tesoura vence");
}
}
else if (choice === "tesoura") {
if (computerChoice === "pedra")
alert("pedra vence");
else {
alert("tesoura vence");
}
}
});
});
You can use the date attribute and set a value for each image. Ex: <img src="scissors.jpg" value date="scissors" /> Then you can recover the value of the attribute at the time of the click. var Choice = $(this). date("value");
– user41722