Rock Paper Scissors
by joshuadgoldberg1176 in Circuits > Computers
2822 Views, 1 Favorites, 0 Comments
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.
Open Up Work Space
1. Start out by opening up code.org, click create a project, and click app lab
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.
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.
GetWinner Function
Write a function called getWinner with the parameter, “playersChoice”.
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.
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.
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
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.
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
Finish!
At this point, your program should be finished, click run and play the game to ensure that it works properly.