Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rock, Paper, Scissors Game Counter

    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")

    }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,185
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    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).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •