Introduction: Rock Paper Scissors

Purpose: After completing this, you will learn how to make a simple game of Rock, Paper Scissors from scratch using Code.org.

Materials / Requirements needed: Basic understanding of Javascript syntax, a computer, a Code.org account.

Step 1: Open Up Work Space

1. Start out by opening up code.org, click create a project, and click app lab

Step 2: Design User Interface

2. Click the design tab in the upper left corner of the coding environment, and drag three buttons (Rock, Paper, Scissors). Label them and change their IDs accordingly. Also In the design tab, drag labels for: CPU choice, Player Choice, and a Win or Lose indicator. ID these accordingly, end result should look like image above.

Step 3: Create Click Functions

Create Event functions that run when each of the buttons are clicked. To do this, click on the desired button in the design tab, then click insert code under the events tab in the design work space.

Step 4: GetWinner Function

Write a function called getWinner with the parameter, “playersChoice”.

Step 5: Calling the GetWinner Function

In each Click Event function, call the getWinner function, sending a string with the name of item corresponding to the function.

Step 6: Get CPU's Choice

In the getWinner function, initialize a variable “cpuChoice, and have it send a random number from 0 to 2 to a new randomPick function. Create the randomPick function with an int parameter.

Step 7: Write RandomPick

In the randomPick function, return a different item for each random number from 0 to 2. Ex. if x = 0 return “Rock”. Set the text of a Label to “CPU chooses ” && item

Step 8: Determine Winner

Back in the getWinner function, compare the playerChoice to cpuChoice using if else statements to determine the winner. Initialize a Boolean which sets to true if the Player is determined the winner, and stays false otherwise. Caution: Check if there is a tie first.

Step 9: Record Results

At the end of the getWinner function, set a global variable for the CPU win count and Player win count and adjust each variable accordingly. Adjust the corresponding label at the end of the getWinner function (once winner has been determined). Change the main label to either “You Win”, or “You Lose” here as well

Step 10: Finish!

At this point, your program should be finished, click run and play the game to ensure that it works properly.