Rock, Paper, Scissors Game

Below is my Rock, paper, scissors game as I’ve written it. I am no javascript expert, but I try. I “elevated” the logic behind what the computer chooses from most versions of the game detail. All of the logic works as written. However, it only works when in the developer console, and I step it along step by step. If I run it normally in the browser it just keeps asking for the user’s input. It doesn’t ever print out the user’s choice, computer’s choice nor the result of the game. This next sentence may explain my ignorance in javascript, but in order to use some of the logic, I wanted to use I set up an array at the beginning that I input info and reference throughout. To allow me to start the game over without resetting the browser and therefore losing my array data, I set it up in a loop. I’m sure there is a “simple” reason why this is happening. I’m open to any input or suggestions on the game. Thank you for your help.

<!DOCTYPE html>
<html lang="en-us">
    <head>
        <title>Rock, Paper, or Scissors</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--        <link rel="stylesheet" media="screen and (min-width: 600px)" href="../css/main.css">
        <link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">-->
    </head>
    <body>
        <header>
        </header>
        <main>
<!--            <form>
                <fieldset>
                    <input type="button" value="Rock">
                    <br>
                    <input type="button" value="Paper">
                    <br>
                    <input type="button" value="Scissors">
                </fieldset>
            </form>-->
<p id="demo"> </p>
            <script>
                //My array
                var rPS = new Array(0, 0, 0);
                
                
                do {
                    // User choice
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    if (! userChoice) {
                        // User choice was undefined
                        document.write("<p>Player 1, you cheated! Refresh this screen and fight like a man.</p>");
                    } else {
                        // Display user choice
                        document.write("<p>Player 1:" + " " + userChoice + "</p>");
                    }
                    
               
                
                // Computer choice
// Ideas:
// 1) If there is no previous hand have computer play paper
// 2) If the other player wins play the opposite on the next hand.
// 3) If you win never play the same option twice in a row.
// https://www.psychologytoday.com/us/blog/the-blame-game/201504/the-surprising-psychology-rock-paper-scissors
// https://statmodeling.stat.columbia.edu/2007/05/21/how_to_win_at_r/

                    var computerChoice; 
                    if (computerChoice == null) {
                        computerChoice = "paper";
                    } else if (rPS [2] === 1) {
                        if (rPS [0] === "paper"){
                                computerChoice = "scissors";
                        } else if (rPS[0] === "scissors") {
                                computerChoice = "rock";
                        } else (computerChoice = "paper")
                    } else if (rPS [2] === 2){
                            if (rPS [1] === "paper"){
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "rock";
                                } else {
                                    computerChoice = "scissors";
                                }
                            } else if (rPS [1] === "rock"){
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "paper";
                                } else {
                                    computerChoice = "scissors";
                                }
                            } else {
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "rock";
                                } else {
                                    computerChoice = "paper";
                                }
                                }   
                    } else {
                        if (rPS [2] === 0){
                            if (rPS[1] === "paper") {
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "rock";
                                } else if ( computerChoice <= 0.75){
                                    computerChoice = "paper";
                                } else {
                                    computerChoice = "scissors";
                                }
                            } else if (rPS[1] === "rock") {
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "scissors";
                                } else if ( computerChoice <= 0.75){
                                    computerChoice = "paper";
                                } else {
                                    computerChoice = "rock";
                                }
                            } else {
                                computerChoice = Math.random();
                                if (computerChoice < 0.50){
                                    computerChoice = "paper";
                                } else if ( computerChoice <= 0.75){
                                    computerChoice = "rock";
                                } else {
                                    computerChoice = "scissors";
                                }
                            }
                        }
                    }
                         
                     

                     //document.getElementById("demo").innerHTML = rPS;

                     // Display computer choice
                     document.write("<p>Computer:" + " " + computerChoice + "</p>");



                     // Compare user choice vs computer choice
                     var compare = function(choice1,choice2) {
                         if (choice1 === choice2) {
                             return "It's a tie!";
                         }
                         if (choice1 === "rock") {
                             if (choice2 === "scissors") {
                                 // rock wins
                                 return "You win!";
                             } else {
                                 // paper wins
                                 return "You lose! Try again.";
                             }
                         }
                         if (choice1 === "paper") {
                             if (choice2 === "rock") {
                                 // paper wins
                                 return "You win!";
                             } else {
                                 // scissors wins
                                 return "You lose! Try again.";
                             }
                         }
                         if (choice1 === "scissors") {
                             if (choice2 === "rock") {
                                 // rock wins
                                 return "You lose! Try again.";
                             } else {
                                 // scissors wins
                                 return "You win!";
                             }
                         }
                     };
                     // Run the compare function
                     var results = compare(userChoice,computerChoice);

                     //logging info to array at top
                     rPS[0] = userChoice;
                     rPS[1] = computerChoice;
                     //from comps point of view
                     if (results == "It's a tie!") {
                         rPS[2] = 0;
                     } else if (results == "You win!") {
                         //loss
                         rPS[2] = 1;
                     } else {
                         //win
                         rPS[2] = 2;
                     }
                     document.getElementById("demo").innerHTML = rPS;
                     // Display results
                     var final = "<br><hr><br>" + results;
                     document.write(final);

                     var play = "yes"; //prompt("Would you like to play Again?");
                } while (play === "yes" || play === "Yes" || play === "YES");
               
                
            </script>
            
        </main>
    </body>
</html>

My JS is basic at best but I believe that it’s because document.write doesn’t happen until the script is finished while the prompt is displayed as it is encountered which is then before any document.writes have been written to the page.

Avoid document.write and display the results in normal html on the page.

Prompts and alerts will (depending on browser) happen before preceding code has taken effect. So avoid alerts and prompts also and write your own code that will wait for a user interaction before they happen.

I’ll leave the JS experts to comment on your code although I note you don’t take into account that rock paper or scissors may be mis-typed when you collect the data.

I will tell you your computer will always choose paper. See if you can figure out why.

var computerChoice; 
if (computerChoice == null) {

This will always be true, because you’re resetting computerChoice every loop.

You will find it easier to control the flow if you use the buttons and event triggers to play the game rather than input alert boxes, for the reason Paul has identified. Plus it’s less annoying to the user to not have boxes flashed in their face constantly.

You can start a second round of the game automatically once you resolve the current round.

1 Like

Hi there aaronjcurtis,

I made a “Rock, Paper, Scissors” game some while ago.

Check out the attachment…

game.zip (18.3 KB)

…it may, or may not, help you with your personal project. :winky:

Either way, it could provide a little light amusement.:biggrin:

coothead

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.