Skip to main content

Rock, Paper, Scissors game using C programming

 Rock, Paper, Scissors game using C programming:

Program:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>


int main() {

    char choice;

    char userChoice;

    int computerChoice;

    int userScore = 0, computerScore = 0;


    srand(time(NULL)); // Seed the random number generator


    printf("Let's play Rock, Paper, Scissors!\n");

    printf("Enter your choice (R for Rock, P for Paper, S for Scissors):\n");


    while (1) {

        printf("Your choice: ");

        scanf(" %c", &userChoice);


        // Convert user's choice to uppercase

        userChoice = toupper(userChoice);


        // Generate computer's choice (0 for Rock, 1 for Paper, 2 for Scissors)

        computerChoice = rand() % 3;


        // Display computer's choice

        switch (computerChoice) {

            case 0:

                printf("Computer chooses: Rock\n");

                break;

            case 1:

                printf("Computer chooses: Paper\n");

                break;

            case 2:

                printf("Computer chooses: Scissors\n");

                break;

        }


        // Determine the winner

        if (userChoice == 'R') {

            if (computerChoice == 0) {

                printf("It's a tie!\n");

            } else if (computerChoice == 1) {

                printf("Computer wins!\n");

                computerScore++;

            } else {

                printf("You win!\n");

                userScore++;

            }

        } else if (userChoice == 'P') {

            if (computerChoice == 0) {

                printf("You win!\n");

                userScore++;

            } else if (computerChoice == 1) {

                printf("It's a tie!\n");

            } else {

                printf("Computer wins!\n");

                computerScore++;

            }

        } else if (userChoice == 'S') {

            if (computerChoice == 0) {

                printf("Computer wins!\n");

                computerScore++;

            } else if (computerChoice == 1) {

                printf("You win!\n");

                userScore++;

            } else {

                printf("It's a tie!\n");

            }

        } else {

            printf("Invalid choice. Please enter R, P, or S.\n");

            continue; // Restart the loop if the choice is invalid

        }


        // Display scores

        printf("Your score: %d\n", userScore);

        printf("Computer's score: %d\n", computerScore);


        // Ask if the user wants to play again

        printf("Do you want to play again? (Y/N): ");

        scanf(" %c", &choice);


        if (toupper(choice) != 'Y')

            break; // Exit the loop if the user doesn't want to play again

    }


    printf("Thanks for playing!\n");


    return 0;

}

Output:

Let's play Rock, Paper, Scissors!
Enter your choice (R for Rock, P for Paper, S for Scissors):
Your choice: R
Computer chooses: Scissors
You win!
Your score: 1
Computer's score: 0
Do you want to play again? (Y/N): Y
Your choice: P
Computer chooses: Rock
You win!
Your score: 2
Computer's score: 0
Do you want to play again? (Y/N): Y
Your choice: S
Computer chooses: Paper
Computer wins!
Your score: 2
Computer's score: 1
Do you want to play again? (Y/N): N
Thanks for playing!

Comments

Popular posts from this blog

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. 🔗 Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here 🛠️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...