...

View Full Version : Rock, Paper, Scissors Game Counter



Staceadam
11-04-2012, 10:35 PM
The game runs fine but I am having trouble continually keeping score and adding 1 to the textfields based on if it is a win, tie or loss. The textfields id's are 'won' 'lost' and 'tied'. Any help would be awesome.

window.onload = init;
function init(){

document.getElementById("rock").onclick = game;
document.getElementById("paper").onclick = game;
document.getElementById("scissors").onclick = game;
document.getElementById("lizard").onclick = game;
document.getElementById("spock").onclick = game;

}


function game(){

var compGuess;
var cchoice;
var pchoice;
var won;
var lose;
var tie;



compGuess = (Math.floor(Math.random() * 5)) + 1;

switch (compGuess){
case 1:
cchoice = "rock";
break;
case 2:
cchoice = "paper";
break;
case 3:
cchoice = "scissors";
break;
case 4:
cchoice = "lizard";
break;
case 5:
cchoice = "spock";
break;
default:
message = "Error";
break;
}//end switch

pchoice = (this.id);

//ROCK
if (pchoice=="rock" && cchoice=="scissors"){
document.getElementById('won').innerHTML = won++
alert("Rock beats the scissors. YOU WIN!!")

}
else if (pchoice=="rock" && cchoice=="paper") {
document.getElementById('lost').innerHTML = lose++
alert("Paper beats rock...idk how that works but. YOU LOSE")

}
else if (pchoice=="rock" && cchoice=="lizard") {
document.getElementById('won').innerHTML = won++
alert("Rock crushes lizard. YOU WIN!!")

}
else if (pchoice=="rock" && cchoice=="spock") {
document.getElementById('lost').innerHTML = lose++
alert("Spock vaporizes rock. YOU LOSE")

}
else if (pchoice=="rock" && cchoice=="rock") {
document.getElementById('tied').innerHTML = tie++
alert("You both picked rock. YOU TIED")

}

felgall
11-05-2012, 01:37 AM
You'd need to define won loss and tie outside of the game function of you want them to work for more than one game - you will also need to zero them at the start in order for the ++ to work properly.

Staceadam
11-05-2012, 01:52 AM
ok did that

function won(){
var won = 0
document.getElementById('won').value = won++ }

function lose(){
var lose = 0
document.getElementById('lost').value = lose++}

function tie(){
var tie = 0
document.getElementById('tie').value = tie++}

function game(){

var compGuess;
var cchoice;
var pchoice;


//ROCK
if (pchoice=="rock" && cchoice=="scissors"){
won();
alert("Rock beats the scissors. YOU WIN!!")

}
else if (pchoice=="rock" && cchoice=="paper") {
lose();
alert("Paper beats rock...idk how that works but. YOU LOSE")

}
else if (pchoice=="rock" && cchoice=="lizard") {
won();
alert("Rock crushes lizard. YOU WIN!!")

}
else if (pchoice=="rock" && cchoice=="spock") {
lose();
alert("Spock vaporizes rock. YOU LOSE")

}
else if (pchoice=="rock" && cchoice=="rock") {
tie();
alert("You both picked rock. YOU TIED")

}

I'ts still not taking the int value in the textfield and continuously saving and adding it.

Old Pedant
11-05-2012, 05:22 AM
No.

You must initialize win, loss, and tie outside of *ANY* function.

If they are inside of any function, then they last only as long as that function is executing and go away immediately afterwards.

In other words, to pick an example, every time you call the tie( ) function, you RESTART the tie variable at ZERO! So it NEVER gets larger than 1.

More than that, though, you can't have a function and a variable of the same name (you can, when the variable is inside the function, but even then it's a truly crappy idea).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum