2

Ok so this should be fairly easy to answer but for some reason im having an issue.( could be because im very new to programming) I've created a rock,papers,scissors game against the CPU. Now i want to ask the user if they want to play again and if they answer Y then it should go through the loop each time. If they type "N" then the game will end. The main issue im having is that once you type in Y to play again it just gives you the result from the last game. Any pointers would help.

Here is what i have :

var userChoice = "";
var userChoice = prompt("Choose rock, paper, or scissors");
var playagain = "Y";
var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

choice1 = userChoice;
choice2 = computerChoice;

while (playagain == "Y") {
    function compare(choice1, choice2) {

        if (choice1 == choice2) {
            return ("It's a tie!");
        }
        if (choice1 == "rock") {
            if (choice2 == "scissors") {
                return ("You win!");

            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "paper") {
            if (choice2 == "rock") {
                return ("you win!");
            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "scissors") {
            if (choice2 == "rock") {
                return ("computer wins!");
            } else {
                return ("you win!");
            }
        }
    }

    document.write(compare(choice1, choice2));
    document.write("<br>");
    playagain = prompt("Do you want to play again, Y or N");
    userChoice = prompt("Choose rock, paper, or scissors");
}
user2226755
  • 12,494
  • 5
  • 50
  • 73
Dylan Tavares
  • 23
  • 1
  • 3
  • I don't know javascript but you could just reload the webpage if they type "Y" if you wanna be lazy ;) hahaha http://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript – Albert Renshaw Nov 04 '15 at 03:32
  • 1
    haha yea but teacher wants us to ask the question in the code . Which i have its just where i put it ( i think ). – Dylan Tavares Nov 04 '15 at 03:34
  • it writes out the return lines? It's supposed to say either you win , you lose , or it's a tie then break and keep looping if you press Y. – Dylan Tavares Nov 04 '15 at 03:38
  • Move your `while` on the start of your script. – user2226755 Nov 04 '15 at 05:34

4 Answers4

2

The only thing you need is to move all game logics under the while loop:

function compare(choice1, choice2) {
    if (choice1 == choice2) {
        return ("It's a tie!");
    }
    if (choice1 == "rock") {
        if (choice2 == "scissors") {
            return ("You win!");
        } else {
            return ("computer wins!");
            }
        }
        if (choice1 == "paper") {
            if (choice2 == "rock") {
                return ("you win!");
            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "scissors") {
            if (choice2 == "rock") {
                return ("computer wins!");
            } else {
                return ("you win!");
            }
        }
    }
}

var playagain = "Y";

while (playagain == "Y") {
    var userChoice = prompt("Choose rock, paper, or scissors");
    var computerChoice = Math.random();

    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }

    document.write(compare(userChoice, computerChoice));
    document.write("<br>");
    playagain = prompt("Do you want to play again, Y or N");
}

I have:

  • moved everything except the declaration of playagain into the loop
  • removed choice1 and choice2 local variables, as they are redundant
  • removed prompt in the end of the loop, since we have one in the beginning
  • removed the declaration of an empty userChoice in the beginning
  • by the advice of @LiYinKing, moved the function out of the loop

Here is the working JSFiddle demo.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

Inside of the while loop you need to include your user prompt and new computer generated random number. The problem is your loop is calculating the same random number and user input over and over.

Brian Stallter
  • 159
  • 1
  • 1
  • 16
0

It's hard to explain exactly what's wrong because the code is difficult to follow.

  • I don't know how you expect the code that sets the computer's choice to run again after the code executes to the bottom of the script.
  • Declaring named functions in a loop doesn't make sense. Declare them at the top. Then there's nothing left in the body of the loop. Fishy...

What you need is a function that runs the game and a while loop calling that function.

I am on a phone, this is untested

function runGame() {
  var userChoice = prompt("Choose rock, paper, or scissors");
  var computerChoice= Math.random();

  if(computerChoice < 0.34){
      computerChoice = "rock";
  } else if(computerChoice<=0.67){
     computerChoice = "paper";
  } else {
     computerChoice = "scissors";
  }

  if(userChoice==computerChoice){
     return "It's a tie!";
  }
  if(userChoice=="rock"){
    if(computerChoice=="scissors"){
      return "You win!";
    } else{
      return "computer wins!";
    }
  }
  if(userChoice=="paper"){
    if(computerChoice=="rock"){
        return "you win!";         
    } else{
        return "computer wins!";
    }
  }    
  if(userChoice=="scissors"){
    if(computerChoice=="rock"){
        return "computer wins!" ;           
    } else{
        return "you win!" ;
    }
  }
}

alert(runGame());
while ( prompt("Do you want to play again, Y or N") == "Y") {
  alert(runGame());
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

Later with experience and knowledge you can write the script like this :

function compare(choice1, choice2) {
    if (choice1 == choice2) {
        return ("It's a tie!");
    }
    var lose = {
        "rock": "scissors",
        "paper": "rock",
        "scissors": "paper"
    };
    return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
}

(function() {
    var CHOICE = ["rock", "paper", "scissors"];
    var userChoice = "";
    var userChoice, computerChoice, playagain, i = 0;

    do {
        document.write("Ground " + i + " :<br>");
        computerChoice = CHOICE[Math.floor(Math.random() * 3)];

        do {
            userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
        } while (CHOICE.indexOf(userChoice) === -1);

        document.write(userChoice + " vs " + computerChoice + "<br>");
        document.write(compare(userChoice, computerChoice) + "<br>");
        playagain = (""+prompt("Do you want to play again, Y or N")).toUpperCase();

        i++;
    } while (["Y", "YES"].indexOf(playagain) !== -1);
});

Explanation :

I add toLowerCase() function to replace "Rock" to "rock", and I add toUpperCase() to replace "y" to "Y".

In this circumstance do { /* script */ } while (bool) is more suitable.

With :

do {
    userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);

You wait a valid answer, three word only : rock or paper or scissors. When userChoice equals "rock" indexOf return 0 ; equals "paper" indexOf return 1...

This syntax :

return (lose[choice1] == choice2) ? "You win!" : "computer wins!";

Is :

if (lose[choice1] == choice2)
    return "You win!";
else
    return "computer wins!"

JavaScript ternary operator example with functions

Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73