Modular Breakdown
Learn to break down the game into its components.
We'll cover the following
How to start
We will set up the game using the same flow as the previous lesson. This will help us in a better understanding of the game. Let’s go through the steps as the game progresses.
-
Game introduction,
displayIntroductionAndRules()
: This involves defining the game’s rules. -
Ask the user how many rounds they want to play,
getRound()
: This step involves asking the user to specify the number of games they want to play. This could be a single match, best-of-three, best-of-five, or any other number of rounds the user desires. The number of rounds the user specifies determines how many times the game will be played before declaring a winner. -
User’s input,
getPlayerChoice()
: We need to set up a function to get the input from the user. -
Computer’s move,
generateComputerChoice()
: Write a function to get the computer choice.
-
Check for a win,
determineWinner()
: Write a function to check if a player has won the game. This function should check the rules and compare the choices of both players. -
Main game loop,
gameLoop()
: Write the main game loop to coordinate the flow using the functions defined in previous steps to get user input, check for wins or ties, and update the result. -
End game,
endGame()
: Once a win or tie has been detected, end the game by displaying the final results.
Once the game setup is complete, the actual game of Rock Paper Scissors can begin. The user and the computer (or another player) will make their hand gestures according to the game’s rules, and the winner of each round will be determined based on which gesture beats which. At the end of the specified number of rounds, the winner will be declared based on who won the most games.
Game flow
Let’s go through the game once again and understand the flow. The widget below shows the implementation of Rock Paper Scissors in C++. Click the “Run” button to load the game. It’ll take a few seconds to run the game.
// Click the Run button