Lab 4.2: Rock, Paper, Scissors with Actions
An introduction to parametersGoals
The goals of this lab are to learn the following:
- How to implement actions without parameters and return types
- How to read and understand requirements to write basic actions
Computer Science Principles Curriculum
- Big Idea: Algorithms: EK 4.1.1A, EK 4.1.1B, EK 4.1.1C, EK 4.1.1D, EK 4.1.1E, EK 4.1.1F, EK 4.1.1G, EK 4.1.1H, EK 4.2.1A, EK 4.2.1B
- Big Idea: Programming: EK 5.1.2A, EK 5.1.2B, EK 5.2.1A, EK 5.2.1B, EK 5.2.1C, EK 5.2.1D, EK 5.2.1E, EK 5.5.1A, EK 5.5.1D
Common Core Standards
- English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.10, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.11-12.4, CCSS.ELA-Literacy.RST.11-12.5, CCSS.ELA-Literacy.RST.11-12.10
- Mathematics Content: High School Functions » Building Functions: CCSS.Math.Content.HSF.BF.1A
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP4, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8
Overview
In this lab, you will be writing a Rock Paper Scissors game. In this game a human player will be competing with the computer. The human player will choose rock, paper, or scissors and then the computer will randomly select one of the options. Then your program will tell the player who won the round. The use of actions will allow you to organize the game and make the program look organized.
Task 1: Getting Started
If you're using Quroum Studio. Create a new "Main" project, and name it RockPaperScissors. As in the previous labs, this will leave you with a skeleton Main.quorum file, containing a Main class and Main action. After your project is created, you can use the following code skeleton(replace the code in your project with the following code):
use Libraries.Compute.Random
class Main
/* Action Main has no parameters or return value.
This is where the program begins executing. Within this method you should
give the instructions to the user and then play the game.
*/
action Main
end
/* Action GiveInstructions has no parameters or return value.
It handles giving all game rules and instructions to the user before
the game begins.
*/
action GiveInstructions
end
/* Action PlayGame has no parameters or return value.
It handles anything involving playing the game. This includes asking the player
for their choice, generating the computer's choice, determining the winner,
and determining if the player wishes to quit the game or continue playing.
*/
action PlayGame
end
/* Action PlayersTurn has no parameters and returns an integer value.
This action handles asking the user for their choice of rock, paper, or
scissors. It also checks the input for errors. Then it returns the valid
user choice.
*/
action PlayersTurn returns integer
integer choice = 1
//your code goes here
return choice
end
/* Action ComputersTurn has no parameters and returns an integer value.
This action handles randomly generating the computer's choice. Then it
returns that value.
*/
action ComputersTurn returns integer
integer choice = 1
//your code goes here
return choice
end
/* Action DetermineWinner has two parameters and no return value. The first
parameter is the player's choice and the second is the computer's choice.
This action handles determining the winner of a round in the game. It
also says who won the round
*/
action DetermineWinner(integer playerChoice, integer computerChoice)
end
end
Pay close attention to the comments in the given code. They give you a good idea of how your program will be controlled. You will start by writing the GiveInstructions action. In this action you should say or output the rules and instructions for the rock, paper, scissors game. That message might be something like the following:
Welcome to the Rock Paper Scissors game!
Here are the rules:
The player and the computer will pick rock, paper, or scissors.
Paper beats rock.
Rock beats scissors.
Scissors beat paper.
Enter a 1 for rock, a 2 for paper, or a 3 for scissors.
Once the GiveInstructions action is written, call the action in the Main action. Remember Main is where your program begins executing or running and the end following the Main action is where it ends. Run the program and it should give the instructions you wrote in the GiveInstructions action.
Task 2: Create A Game with Actions
You are going to create a game with actions. You will separate each action so that they serve their own purpose. The first of these actions will be the PlayersTurn action. This action should ask the user to choose rock, paper, or scissors and it should continue to ask the user this until a valid input is given.
Go to the PlayersTurn action. Within that action start writing code at the comment that says "your code goes here". You will need your code to do the following:
- Declare a boolean and initialize it to false to track valid input.
- Create a loop that loops until the input is valid.
- Within that loop, ask the user to choose rock, paper, or scissors.
- Cast the input from the user to an integer and store it in the choice variable(has already been declared in this action).
- Check if the input is valid; if it is valid the loop should stop.
The existing code then returns the user's choice from the action, return choice, is already written into the action. Don't worry about calling and testing this action right now. You will come back to this when you write the PlayGame action. Instead move onto writing the ComputersTurn action.
Go to the ComputersTurn action. Within that action start writing code at the comment that says "your code goes here". You will need your code to do the following:
- Declare a Random object. e.g. Random random.
- Generate a random integer between 1 and 3, including 1 and 3.
- Assign the random integer to the choice variable.
The computers choice is then returned(the code for this is already included).
The next action you are going to write is the DetermineWinner action. This action has two parameters: playerChoice and computerChoice. For this to be a general solution you won't worry about the value of each parameter; those are determined when you call the DetermineWinner action. In this action you want to use conditional statements to determine if there is a tie, if the computer won, or if the player won.
To start writing this action you can use the following code:
if playerChoice = computerChoice then
say "It's a tie!"
end
Add onto this conditional so that, if:
- Player chooses rock and computer chooses paper the output is, "Paper beats rock - the computer wins!"
- Player chooses rock and computer chooses scissors the output is, "Rock beats scissors - you win!"
- Player chooses paper and computer chooses rock the output is, "Paper beats rock - you win!"
- Player chooses paper and computer chooses scissors the output is, "Scissors beats paper - the computer wins!"
- Player chooses scissors and computer chooses paper the output is, "Scissors beats paper - you win!"
- Player chooses scissors and computer chooses rock the output is, "Rock beat scissors - the computer wins!"
The next action you will write is the PlayGame action. This action handles all of the game play, including getting the player's choice, randomly generating the computer's choice, determining a winner of the round, and asking the player if they want to continue playing or stop the game.
In this action, start by defining a text variable called playAgain and initialize it to "Y". Create a loop that will continue to loop when playAgain is equal to "Y" or "y". Inside this loop you need to call the PlayersTurn action and store the returned value in a variable named player with a type of integer. Then do the same with the ComputersTurn action. Store the returned value in a variable named computer with a type of integer. Call DetermineWinner passing the arguments player and computer. Finally, ask the user, "Would you like to play again (Y/N)?", and assign the result to the playAgain variable.
When you are done the PlayGame action should look like the following code:
action PlayGame
text playAgain = "Y"
repeat while playAgain = "Y" or playAgain = "y"
integer player = PlayersTurn()
integer computer = ComputersTurn()
DetermineWinner(player, computer)
playAgain = input("Would you like to play again (Y/N)?")
end
end
The final step is to finish writing the Main action. The Main action should give the instructions, play the game, and say "Game over" when the game is done.
Sample Output
The following is sample output from the game:
Welcome to the Rock Paper Scissors game!
Here are the rules:
The player and the computer will pick rock, paper, or scissors.
Paper beats rock.
Rock beats scissors.
Scissors beat paper.
Enter a 1 for rock, a 2 for paper, or a 3 for scissors.
Rock (1), Paper (2), or Scissors (3)?
4
Error: The value needs to be between 1 and 3. Please try again. You entered 4
Rock (1), Paper (2), or Scissors (3)?
1
Rock beats scissors - you win!
Would you like to play again (Y/N)?
Y
Rock (1), Paper (2), or Scissors (3)?
1
Paper beats rock - the computer wins!
Would you like to play again (Y/N)?
y
Rock (1), Paper (2), or Scissors (3)?
3
It's a tie!
Would you like to play again (Y/N)?
N
Game over
Debug your program and fix any compiler errors there might be. Now would be a great time to use the debugger to test your program. When an action is called use 'step into' to see what happens when a particular action is called. Did your program run correctly? If not, fix any problems. Show the work to the instructor.
Next Tutorial
In the next tutorial, we will discuss Actions with Random Noise Generator, which describes more on actions.