DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Updated on

Self-Taught Developer Journal, Day 33: TOP Project, Rock Paper Scissors cont.

Today....

Plan cont.

For the game, Rock Paper Scissors, the rules are:

  • Rock beats Scissors
  • Scissors beats Paper
  • Paper beats Rock

If the user and computer pick the same option then it would result in a tie.

If the user and computer get a tie for each of the 5 rounds then no one wins and they can play again.

Pseudocode

When the user inputs rock, paper, or scissors
Call the computerPlay function to get the computer's selection
Compare the user's selection and computer selection
If the user wins then add number to player wins
If the computer wins then add number to computer wins
Continue playing for five rounds
After five rounds, compare the player wins to the computer wins to determine winner or loser

Divide and Conquer (Implement)

Starting with computerPlay(), the function will randomly return Rock, Paper, or Scissors. To do this, I Googled to see if there was a random function in JavaScript to use. I found Math.random() which will return at number between 0 (inclusive) to 1 (exclusive). Math.floor() and arithmetic can be used to change the numbers returned.

// return number between 0 - 2 inclusive
Math.floor(Math.random() * 3);
Enter fullscreen mode Exit fullscreen mode

I could have Rock, Paper, and Scissors represent a certain number so I can use the Math.random() function, but have computerPlay() return Rock, Paper, or Scissors. I will try this implementation for now and test it.

computerPlay()

I decided to try using the ternary operator since the conditional statements were simple. I went with generating a random number between 0 - 2 inclusive with 0 = Rock, 1 = Paper, 2 (or else) = Scissors. I also decided to use the variable declaration let instead of const since the values would be changing. I logged the results in the console to check and it worked as expected.

Test results

Next, I started to write a function that will play a single round of Rock Paper Scissors taking two parameters, playerSelection and computerSelection, and return a winner or loser.

gameRound() function example

Lastly, I will write the function game(). The function will loop and play five rounds of Rock, Paper, and Scissors. At the end of the five rounds, the function will return the winner or loser.

As I was implementing game, I realized my gameRound() function wouldn't work and changed it to return who won for the game instead of a statement to log.

gameRound() edited

For the game() function, I added the variables, playerWins and computerWins, to track the result of gameRound() for every iteration of the loop. If the result was a tie then the score or wins are not updated. I also included an array of the correct game inputs and a while loop to re-prompt the user if their input isn't valid. At the end of the five rounds, the player and computer wins are compared to return a winner or loser.

game() example

Game result example

Resources

The Odin Project
https://www.w3schools.com/js/js_random.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Discussion (0)