DEV Community

TakDevelops
TakDevelops

Posted on

Career Pivot into Development Journal, Day 2: Rock, Paper Scissors

Understanding The Problem

Create a program that takes a user’s input of either Rock, Paper, or Scissors, and play this again a computer that randomly generates Rock, Paper, or Scissors. If the player wins 1 round, add 1 point to the player. If the computer wins 1 round, add 1 point to the player. Play 5 rounds and announce the winner with the most points after 5 rounds

Plan

  • Does program have a UI? What will it look like? What functionality will the interface have? Sketch this out on paper. No UI as this will be played in the console
  • What are the inputs? Will the user enter data or will you get input from somewhere else? User enters in ‘Rock’, ‘Paper’, or ‘Scissors’ when prompted
  • What’s the desired output? A string announcing the winner of the round, A point added to the winner. A final string announcement of the winner at the end of 5 rounds
  • Given input X, what are the steps necessary to return output Y?

Pseudocode

When the user inputs a string of ‘rock’, ‘paper’, or ‘scissors’

Call the computer’s random selection of ‘rock’, ‘paper’, or ‘scissors’

Compare the user’s input with the computer’s random selection

If the user wins, alert the string ‘You Win!’
and add 1 point to the user
then return the current score of the user and the computer

If the computer wins, alert the string ‘Boohoo, you lost to the computer.’
and add 1 point to the user
then return the current score of the user and the computer

If the user and computer draw, alert the string ‘It’s a draw’

Loop for 5 rounds

If the user’s final score is greater than the computer’s final score
alert ‘Congratulations, you beat the computer!

If the computer’s final score is greater than the user’s final score
alert ‘Sorry, you lost to the computer...’
Enter fullscreen mode Exit fullscreen mode

Divde & Conquer

Now, let's pick the simplest subproblem from the pseudocode above to solve.

Let's start with Call the computer’s random selection of ‘rock’, ‘paper’, or ‘scissors’

In order to do this, we first have to create a variable with an array of strings that can be selected upon:

const options = ['rock', 'paper', 'scissors'];
Enter fullscreen mode Exit fullscreen mode

Then, we can use Math.random to generate a random number between 0 and <1, then multiply by options.length (which equals 3):

Math.random() * options.length
Enter fullscreen mode Exit fullscreen mode

This gives a number between 0 and 3. However in the options array, we can only index 0, 1, and 2 for 'rock', 'paper', scissors' respectively. So we can use Math.floor to round down to the nearest integer:

Math.floor(Math.random() * options.length)
Enter fullscreen mode Exit fullscreen mode

To select a random item from the array:

options[Math.floor(Math.random() * options.length)];
Enter fullscreen mode Exit fullscreen mode

Then, declare a function and return the entire expression above in the function:

let computerPlay = function () {
    return options[Math.floor(Math.random() * options.length)];
};
Enter fullscreen mode Exit fullscreen mode

Next, let's tackle the subproblem When the user inputs a string of ‘rock’, ‘paper’, or ‘scissors’

We want to display a prompt, and ensure that it's case-insensitive (so users can input rock, ROCK, RocK or any other variation).

First, declare a variable that stores the prompt input value:

let playerSelection = prompt ('Rock, Paper, or Scissors?');
Enter fullscreen mode Exit fullscreen mode

To ensure it's case insensitive, make all characters in the string that passes through prompt into playerSelection lower case:

let playerPlay = playerSelection.toLowerCase();
Enter fullscreen mode Exit fullscreen mode

Then set playerSelection = playerPlay:

playerSelection = playerPlay;
Enter fullscreen mode Exit fullscreen mode

To be continued...

Top comments (0)