Introduction
Welcome to the Rock, Paper, Scissors game! This simple game allows you to play against the computer in a fun, quick way. The objective is to select one of three options: Rock, Paper, or Scissors, and the computer will randomly choose one as well. The rules are simple:
- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock
The goal of the game is to beat the computer by making the right choice!
Code for Rock, Paper, Scissors Game
Below is the C code for the Rock, Paper, Scissors game:
#include
#include
#include
// Function to get the computer's choice
int getComputerChoice() {
return rand() % 3; // Returns 0, 1, or 2 for Rock, Paper, or Scissors
}
// Function to determine the winner
void determineWinner(int playerChoice, int computerChoice) {
if (playerChoice == computerChoice) {
printf("It's a tie!\n");
} else if ((playerChoice == 0 && computerChoice == 2) ||
(playerChoice == 1 && computerChoice == 0) ||
(playerChoice == 2 && computerChoice == 1)) {
printf("You win!\n");
} else {
printf("Computer wins!\n");
}
}
int main() {
int playerChoice, computerChoice;
// Seed the random number generator
srand(time(NULL));
// Display game options to the player
printf("Welcome to Rock, Paper, Scissors!\n");
printf("Choose your option:\n");
printf("0. Rock\n");
printf("1. Paper\n");
printf("2. Scissors\n");
// Take player's choice
printf("Enter your choice (0/1/2): ");
scanf("%d", &playerChoice);
// Ensure valid input
if (playerChoice < 0 || playerChoice > 2) {
printf("Invalid choice. Please choose 0, 1, or 2.\n");
return 1;
}
// Get computer's choice
computerChoice = getComputerChoice();
// Display choices
printf("You chose: ");
if (playerChoice == 0) printf("Rock\n");
else if (playerChoice == 1) printf("Paper\n");
else if (playerChoice == 2) printf("Scissors\n");
printf("Computer chose: ");
if (computerChoice == 0) printf("Rock\n");
else if (computerChoice == 1) printf("Paper\n");
else if (computerChoice == 2) printf("Scissors\n");
// Determine the winner
determineWinner(playerChoice, computerChoice);
return 0;
}
Explanation of the Program Structure
This program is a simple implementation of the Rock, Paper, Scissors game in C. Let’s break it down:
- getComputerChoice(): This function generates a random number between 0 and 2 using the
rand()function. It simulates the computer’s choice of Rock, Paper, or Scissors. - determineWinner(): This function compares the player’s choice with the computer’s choice and prints the outcome, whether it’s a tie, a win, or a loss.
- main(): This is the entry point of the program. It displays the available choices to the player, takes the player’s input, generates the computer’s choice, and then determines and displays the winner.
How to Run the Program
- Open your C programming environment or IDE.
- Copy the code provided and paste it into a new C file (e.g.,
rock_paper_scissors.c). - Compile the program using a C compiler (e.g.,
gcc rock_paper_scissors.c -o rock_paper_scissors). - Run the compiled program (e.g.,
./rock_paper_scissorson Unix-based systems orrock_paper_scissors.exeon Windows). - Enjoy the game! Choose between Rock, Paper, or Scissors and see if you can beat the computer!


I likewise conceive so , perfectly pent post! .