In this lesson, we will create a game of “Rock, Paper, Scissors”.

After implementing the simple logic of the game, we will primarily ensure that:

  1. The choices of the players are not visible to each other.
  2. The game becomes a tournament (so the players can play the game multiple times).

So, let’s get to it!

Rules of the game

There will be 2 players, and each player will choose either of the 3 symbols listed below:

  • ‘s’ represents scissors.
  • ‘p’ represents paper.
  • ‘r’ represents rock.

The rules are as follows:

  1. The scissors can cut the paper.
  2. The rock can break the scissors.
  3. The paper can cover the rock.

Write a program that implements the logic of the “Rock, Paper, Scissors” game. In this game, two players individually select “rock,” “paper,” or “scissors" by typing r for rock, p for paper, or s for scissors. The winner is the one whose choice dominates the other.

Press + to interact
**Rock, Paper, Scissors Game**
"(r)ock, (p)aper, or (s)cissors:"
Player 1: r
Player 2: p
Player 2 wins!
Player one had chosen: r
Player two had chosen: p

Playground for your implementation

Write your code in the code editor below.

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int getch ( void ) 
{
  int ch;
  struct termios oldt, newt;
  
  tcgetattr ( STDIN_FILENO, &oldt );
  newt.c_cc[VMIN] = 1;
  newt.c_cc[VTIME] = 0;
  // newt = oldt;
  // newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
  
  return ch;
}
Write your implementation of the "Rock, Paper, Scissors" game

Instructions

Go through the following instructions one by one and implement your code in the playground above.

Don’t look at the solution until you’ve implemented each instruction on your own first.

0. Getting started

When writing the program, do the following:

  • Take inputs from two players to store their choices.

  • Check player1's winning condition based on the rules of the game.

  • Check the winning condition of player2 based on the rules of the game.

  • Remember the game is drawn if both players choose the same symbol. So you would need to check that too.

  • If any of the players enter an input other than r, s, or p, print that the input is invalid.

  • Finally, print what characters both players chose.

Implement this step in the code playground above.

Click the “Show Solution” button below to view the solution after implementing your code.

1. Encrypting users’ inputs

But there’s a little problem with our game.

Did you notice a flaw in our game? The choices of the two users are always shown on the screen, so both players know what the other one has chosen, which clearly defeats the purpose of the game!

We need to make sure that when the players give input, the inputs are not visible on the console. To do this, we will use the function getch(). The function, getch(), takes the character input without displaying it on the screen. We’ll use this function because we do not want either player to know what the other player has chosen.

Functions are like small programs that perform specific tasks. There are many functions in C++ libraries like printf(), sqrt(), pow(), etc. Don’t worry. We’ll read about functions later in more detail.

The getch() is a predefined function defined in the conio.h header file. For your convenience, we’ve already included it in a user-defined header file and you can use it directly (without including the conio.h header file).

So to take input from the first player and store it in the char type player1_symbol without displaying it, we’ll write:

player1_symbol = getch();

Similarly, we’ll do the same for the second player’s input.

To make it even more interesting, we could give an impression of taking an encrypted input by printing an asterisk (*) instead on the console after we take each player’s input.

Modify your program in the playground above so that the choices of the players are not visible to each other.

Once done, click the “Show Solution” button below to view the solution.

2. Users’ inputs (small and capital)

What if the user enters capital R, S, P instead of small r, s, or p? Our program should allow the capital R, S, and P inputs as valid too. The code we wrote above will print Invalid input if we give capital R, S, or P as input.

Let’s remedy that.

Modify the code in your code editor above so that the program takes both small and capital alphabets as valid inputs.

Once done, click the “Show Solution” button below to view the solution.

3. Turning the game into a tournament

Let’s make this game into a tournament. Modify the program so that we ask the user to choose the number of games they want to play, and then at the end we can display how many times each player won.

Modify your program in the playground above and make the game into a tournament (so the players can play the game multiple times).

Complete implementation

Look at the complete implementation in the code editor given below. Click “Run” to see its output.

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int getch ( void ) 
{
  int ch;
  struct termios oldt, newt;
  
  tcgetattr ( STDIN_FILENO, &oldt );
  newt.c_cc[VMIN] = 1;
  newt.c_cc[VTIME] = 0;
  // newt = oldt;
  // newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
  
  return ch;
}
Complete implementation of the "Rock, Paper, Scissors" game