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

  1. Open your C programming environment or IDE.
  2. Copy the code provided and paste it into a new C file (e.g., rock_paper_scissors.c).
  3. Compile the program using a C compiler (e.g., gcc rock_paper_scissors.c -o rock_paper_scissors).
  4. Run the compiled program (e.g., ./rock_paper_scissors on Unix-based systems or rock_paper_scissors.exe on Windows).
  5. Enjoy the game! Choose between Rock, Paper, or Scissors and see if you can beat the computer!
© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)