Rock, Paper and Scissors game in C++

This tutorial would help in the understanding of the following topics:

  • Functions
  • User-input
  • Recursion

The “Rock, Paper and Scissors” is a classic game that can be used as an excuse to grasp the basics of a programming language (C++ in this tutorial). To execute multiple rounds of the game, a swift flow of control is required.

Although the entire algorithm of the game can be encapsulated in the main function itself, it is prudent to split the algorithm into different minor segments, each contained inside one function.

The benefits of using multiple functions are:



  • easier debugging in case of errors
  • easier documentation for understanding by other coders
  • re-usage of certain functions in other programs

Code  for Rock paper scissors game in C++

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define rounds 4 //pre-defining the total number of rounds for the game
static int userScore = 0;
static int computerScore = 0;
static int machine;
static char user;
char properInput()//returns only the first character given input as the choice
 {
 	char c=getchar();
 	if(c>='A'&&c<='Z')c = c - 'A' + 'a';//converting the uppercase into lowercase
 	while(getchar()!='\n');//ignoring all other characters given input till the user presses enter
 	return c;
 }
char computerChoice()//returns a random choice for the computer for every round
 {
 	srand(time(0));//this ensures that random numbers are generated according to the given time, hence providing variety every time
 	machine = rand()%3;//this ensures that there are only three possible values: 0,1 and 2
  if(machine==0)return 'r';
  else if(machine==1)return 'p';
  else return 's';
 }
void userChoice()//takes input the choice of the user for every round
 {
  printf("\nInput your choice: ");
  user=properInput();
  while(user!='r' && user!= 'p' && user!='s'){
    printf("Invalid choice, re-input your choice: ");
    user=properInput();
  }
 }
void printRules()//prints the rules of the game
 {
  printf("Rock can beat scissors, paper can beat rock and scissors can beat paper");
  printf("\nInput r for rock, the p for paper and s for scissors");
 }
void game(int currentRound)//recursive function to execute the game for the pre-defined number of rounds
 {
  if(currentRound==rounds)return;//base case which allows complete exit from the function
  userChoice();
  char computer = computerChoice();
  if(computer=='r')printf("Machine played rock. ");
  else if(computer=='p')printf("Machine played paper. ");
  else printf("Machine played scissors. ");
  if(user==computer)
  {
    printf("The round is a draw.\n");
  }
  else if(user=='r' && computer=='s' || user=='p' && computer=='r' || user=='s' && computer=='p')
  {
    printf("You win this round.\n");
    userScore++;
  }
  else
  {
    printf("You lose this round.\n");
    computerScore++;
  }
  game(++currentRound);//starting the next round
 }
void result()//prints the outcome of all the rounds
{
  printf("You scored %d and the system scored %d, ",userScore,computerScore);
  if(userScore==computerScore)printf("it is a draw.\n");
  else if(userScore>computerScore)printf("you win.\n");
  else printf("you lose.\n");
}
int main()//calls the required functions to execute the program
{
  printf("There will be %d round(s).\n",rounds);
  printRules();
  game(0);
  result();
  return 0;
}

Output:

There will be 4 round(s).
Rock can beat scissors, paper can beat rock and scissors can beat paper
Input r for rock, the p for paper and s for scissors
Input your choice: paper
Machine played rock. You win this round.

Input your choice: rock
Machine played rock. The round is a draw.

Input your choice: rock
Machine played scissors. You win this round.

Input your choice: rock
Machine played rock. The round is a draw.
You scored 2 and the system scored 0, you win.

Summary:

The global variables used in the program are explained below:

  • int userScore – stores the total number of victories of the user across the rounds of the game
  • int computerScore – stores the total number of victories of the machine across the rounds of the game
  • int machine – stores the remainder of the generated random number after division by 3 for each round of the game
  • char user – stores the first character of the input given by the user for a particular round of the game

Note:

  • the functions of the program are explained through comments in the code itself
  • getchar() is a library function which returns one character from the input stream
  • srand(int) is a library function which sets a seed for generating random integers
  • time(int) is a library function which returns the time in integers according to the parameter passed
  • rand() is a library function which returns a random integer

Also,read:

 

The execution of the program is explained below:

  • printRules() is called to display the rules of the game
  • game(int) is called and passed with parameter 0
    1. the value of the parameter is checked with the pre-defined number of rounds, if both are equal then control comes out of the function
    2. the choices of the user and the machine are taken by using userChoice() and computerChoice()
    3. the scores of user or machine are incremented upon logical comparison with the choices from step 2
    4. game(int) is called again (recursion) with the value of the original parameter increased by 1
  • result() is called to display the outcome of the game

Leave a Reply

Your email address will not be published. Required fields are marked *