How to Build a Rock, Paper, Scissors Game in C

Hello everyone. Today, we will build the famous game of rock, paper, scissors in C. This blog is beginner-friendly, which means even if you have never written a C program or never touched programming in your life, you could still build your first mini project step by step.
I arranged this blog in steps so that beginners can understand the code and experts can easily go to the section they are stuck at.
Step 1: Include Required Header Files.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Header files are basically those files that tell compiler(this program checks if there are any problems or errors in the code) what resources(files) to include to make our job easier.
#include <stdio.h>
stands for “standard io . h”. This file tell the compiler to includeprintf()
andscanf()
which we require in out program.#include <stdlib.h>()
stands for “standard library . h”. This file tells the compiler to includerand()
function.#include <time.h>
file tells the compiler to include time function and allows us to get a random number.
Step 2: Get the User’s Input.
int user;
printf("Choose an option:\n");
printf("0 - Rock\n1 - Paper\n2 - Scissors\n");
scanf("%d", &user);
To start, we create a number called user
to remember what the player picks in the game. This number is important because it helps us know if the player chooses rock, paper, or scissors. Then, we show a message on the screen to explain how the player can make their choice. For example, we might say that typing 0 means rock, 1 means paper, and 2 means scissors. This explanation is important so the player knows how to choose correctly.
After showing these instructions, we ask the player to type their choice using a special command called scanf()
. This command reads what the player types and saves it in the user
number. This way, we know what the player picked, and we can use it to see who wins the game when we compare it to what the computer picks randomly.
Step 3: Let the Computer Pick Randomly.
srand(time(NULL));
int computer = rand() % 3;
srand(time(NULL));
srand()
stands for seed random.Think of it like setting a starting point for random number generation.
time(NULL)
gives the current time in seconds.Using time as the seed makes the result different every time you run the program.
Without this line, the computer might choose the same number every time you run the game!
int computer = rand() % 3;
rand()
generates a random number, but it could be a very large number.% 3
means we only keep the remainder when dividing by 3, which gives us:Only these possible results:
0
,1
, or2
.So this line picks a random number from 0 to 2:
0 = Rock
1 = Paper
2 = Scissors
Let’s say you run the game, and
rand()
generates56321
.56321 % 3 = 2
→ The computer chooses Scissors.
Step 4: Show What Both Players Chose.
printf("You chose %d. Computer chose %d.\n", user, computer);
This line displays the numbers selected by the user and the computer.
Later, you can improve this by converting the numbers into words like “Rock,” “Paper,” or “Scissors.”
Step 5: Decide Who Wins.
if (user == computer) {
printf("It's a draw!\n");
} else if ((user == 0 && computer == 2) ||
(user == 1 && computer == 0) ||
(user == 2 && computer == 1)) {
printf("You win!\n");
} else {
printf("You lose!\n");
}
Absolutely! Let's break down Step 5: Decide Who Wins in more detail, in a way that's crystal-clear for beginners.
⚔️ Step 5: Decide Who Wins.
After both the user and the computer have made their choices, we need to compare those choices and determine the winner using game rules.
Here’s the code again:
if (user == computer) {
printf("It's a draw!\n");
} else if ((user == 0 && computer == 2) ||
(user == 1 && computer == 0) ||
(user == 2 && computer == 1)) {
printf("You win!\n");
} else {
printf("You lose!\n");
}
if (user == computer)
This line checks if both the user and computer picked the same number.
0 vs 0 → Rock vs Rock
1 vs 1 → Paper vs Paper
2 vs 2 → Scissors vs Scissors
In all these cases, it’s a tie. So the program prints:
printf("It's a draw!\n");
else if (...)
— User wins
Here we check for all the winning combinations for the user:
(user == 0 && computer == 2) // Rock beats Scissors
(user == 1 && computer == 0) // Paper beats Rock
(user == 2 && computer == 1) // Scissors beats Paper
If any one of these is true, the user wins. So the program prints:
printf("You win!\n");
else
— Computer wins
If the game wasn’t a draw, and the user didn’t win, then the only remaining option is:
➡️ The computer wins
So we simply say:
printf("You lose!\n");
🎯 Summary of Game Rules in Code
User | Computer | Result |
Rock (0) | Scissors (2) | You Win |
Paper (1) | Rock (0) | You Win |
Scissors (2) | Paper (1) | You Win |
Same Choice | Same Choice | It's a Draw |
All Others | - | You Lose |
ENTIER CODE.
Wrap everything in the main()
function
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int user, computer;
printf("Choose an option:\n");
printf("0 - Rock\n1 - Paper\n2 - Scissors\n");
scanf("%d", &user);
srand(time(NULL));
computer = rand() % 3;
printf("You chose %d. Computer chose %d.\n", user, computer);
if (user == computer) {
printf("It's a draw!\n");
} else if ((user == 0 && computer == 2) ||
(user == 1 && computer == 0) ||
(user == 2 && computer == 1)) {
printf("You win!\n");
} else {
printf("You lose!\n");
}
return 0;
}
You Did It!
You’ve just written your first full game in C — and you understand every part of it.
Subscribe to my newsletter
Read articles from Gautam Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
