C++ Project on rock paper scissors

Explore a fun C++ project on Rock Paper Scissors Lizard Spock! Inspired by The Big Bang Theory, this console-based game adds complexity and excitement to the classic Rock Paper Scissors. Learn C++ programming with this beginner-friendly project, featuring colorful console output, random number generation, and interactive gameplay.


How the Game Works

The rules of Rock, Paper, Scissors, Lizard, Spock are as follows:

  • Rock crushes Scissors and Lizard
  • Paper covers Rock and disproves Spock
  • Scissors cut Paper and decapitate Lizard
  • Lizard eats Paper and poisons Spock
  • Spock vaporizes Rock and smashes Scissors

The game is played in rounds, and the first player (you or the computer) to reach a specified number of wins is declared the overall winner.

C++ Project on rock paper scissors

You can get the code of C++ Project on rock paper scissors from my GitHub repository

You can also get the code of rock paper scissor game in C++ directly by downloading this Zip File


C++ Game Loop Implementation

1. Colorful Console Output

The program uses ANSI escape codes to add colors to the console output. This makes the game more visually appealing and easier to follow. For example:

  • Green is used for welcoming messages and user wins.
  • Red is used for errors and computer wins.
  • Cyan is used for round numbers and scores.

2. Dynamic Choice Conversion

The getChoiceName function converts the numeric choice (1-5) into its corresponding string representation (Rock, Paper, Scissors, Lizard, Spock). This makes it easier to display the choices in a user-friendly way. For example:

  • If the user selects 1, the program displays “Rock”.
  • If the computer selects 4, the program displays “Lizard”.

3. Winner Determination Logic

The determineWinner function is the heart of the game. It implements the rules of Rock, Paper, Scissors, Lizard, Spock and determines the winner of each round. Here’s how it works:

  • If the user and computer choose the same option, it’s a tie.
  • Otherwise, the function checks the user’s choice against the computer’s choice using a series of conditional statements.
  • The function returns 1 if the user wins, -1 if the computer wins, and 0 if it’s a tie.
C++ Project on rock paper scissors

4. Interactive Game Loop

The main function handles the game loop, which is the backbone of the program. Here’s what it does:

  1. Asks the user how many wins are required to declare an overall winner.
  2. Prompts the user to choose Rock, Paper, Scissors, Lizard, or Spock.
  3. Generates a random choice for the computer using the rand() function.
  4. Determines the winner of the round and updates the scores.
  5. Repeats the process until one player reaches the required number of wins.
  6. Asks the user if they want to play again.

The loop ensures the game is interactive and dynamic, allowing the user to play multiple rounds or quit at any time.


5. Random Number Generation

The computer’s choice is generated using the rand() function, which is seeded with the current time using srand(time(0)). This ensures that the computer’s choices are truly random and unpredictable, making the game more challenging and fun.


The Code Breakdown of C++ Project on rock paper scissors

Here’s a high-level overview of the code rock paper scissor in C++:

1. ANSI Escape Codes for Colorful Output

The program uses ANSI Escape Codes for Colorful Output. This makes the game more visually appealing and easier to follow. For example:

  • Green is used for welcoming messages and user wins.
  • Red is used for errors and computer wins.
  • Cyan is used for round numbers and scores.
#define RESET   "\033[0m"
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN    "\033[36m"

2. Convert Numbers to Strings in C++

The getChoiceName function converts the numeric choice (1-5) into its corresponding string representation (Rock, Paper, Scissors, Lizard, Spock). This makes it easier to display the choices in a user-friendly way.

string getChoiceName(int choice) {
    switch (choice) {
    case 1: return "Rock";
    case 2: return "Paper";
    case 3: return "Scissors";
    case 4: return "Lizard";
    case 5: return "Spock";
    default: return "Invalid";
    }
}

3. Determining the Winner

The determineWinner function implements the game’s rules. It compares the user’s choice with the computer’s choice and returns:

  • 1 if the user wins.
  • -1 if the computer wins.
  • 0 if it’s a tie.
int determineWinner(int userChoice, int computerChoice) {
    if (userChoice == computerChoice) return 0; // Tie
    switch (userChoice) {
    case 1: // Rock
        if (computerChoice == 3 || computerChoice == 4) return 1;
        break;
    case 2: // Paper
        if (computerChoice == 1 || computerChoice == 5) return 1;
        break;
    case 3: // Scissors
        if (computerChoice == 2 || computerChoice == 4) return 1;
        break;
    case 4: // Lizard
        if (computerChoice == 2 || computerChoice == 5) return 1;
        break;
    case 5: // Spock
        if (computerChoice == 1 || computerChoice == 3) return 1;
        break;
    }
    return -1; // Computer wins
}

4. Main Game Loop

The main function handles the game loop. Here’s what it does:

  1. Asks the user how many wins are required to declare an overall winner.
  2. Prompts the user to choose Rock, Paper, Scissors, Lizard, or Spock.
  3. Generates a random choice for the computer.
  4. Determines the winner of the round and updates the scores.
  5. Repeats until one player reaches the required number of wins.
  6. Asks the user if they want to play again.
int main() {
    int userChoice, computerChoice;
    int userScore = 0, computerScore = 0;
    int rounds = 0, maxWins;
    char playAgain;

    srand(static_cast<unsigned int>(time(0))); // Seed random number generator

    cout << GREEN << "Welcome to Rock, Paper, Scissors, Lizard, Spock!" << RESET << endl;
    cout << "How many wins to declare the overall winner? (e.g., 3): ";
    cin >> maxWins;

    do {
        rounds++;
        cout << CYAN << "\nRound " << rounds << RESET << endl;
        cout << "1. Rock\n2. Paper\n3. Scissors\n4. Lizard\n5. Spock\n";
        cout << "Enter your choice (1-5): ";
        cin >> userChoice;

        if (userChoice < 1 || userChoice > 5) {
            cout << RED << "Invalid choice. Please choose a number between 1 and 5." << RESET << endl;
            continue;
        }

        computerChoice = rand() % 5 + 1; // Computer's random choice

        cout << BLUE << "You chose: " << getChoiceName(userChoice) << RESET << endl;
        cout << MAGENTA << "Computer chose: " << getChoiceName(computerChoice) << RESET << endl;

        int result = determineWinner(userChoice, computerChoice);
        if (result == 0) {
            cout << YELLOW << "It's a tie!" << RESET << endl;
        }
        else if (result == 1) {
            cout << GREEN << "You win this round!" << RESET << endl;
            userScore++;
        }
        else {
            cout << RED << "Computer wins this round!" << RESET << endl;
            computerScore++;
        }

        cout << CYAN << "Score: You " << userScore << " - " << computerScore << " Computer" << RESET << endl;

        if (userScore >= maxWins || computerScore >= maxWins) {
            if (userScore > computerScore) {
                cout << GREEN << "Congratulations! You won the series!" << RESET << endl;
            }
            else {
                cout << RED << "Computer won the series. Better luck next time!" << RESET << endl;
            }
            break;
        }

        cout << "Do you want to play another round? (y/n): ";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    cout << GREEN << "Thanks for playing! Final Score: You " << userScore << " - " << computerScore << " Computer" << RESET << endl;

    return 0;
}

Key Features

  • User-Friendly Interface: The game uses colorful text and clear prompts to guide the user.
  • Input Validation: The program ensures the user enters a valid choice (1-5).
  • Randomized Computer Choice: The computer’s choice is generated using the rand() function, seeded with the current time for randomness.
  • Score Tracking: The program keeps track of the user’s and computer’s scores and declares an overall winner.

How to Run the Code

  1. Copy the code into a C++ IDE or editor (e.g., Visual Studio, Code::Blocks, or online compilers).
  2. Compile and run the program.
  3. Follow the on-screen instructions to play the game.

Conclusion of C++ Project on rock paper scissors

This C++ implementation of Rock, Paper, Scissors, Lizard, Spock is a fun and interactive way to practice programming concepts like loops, conditionals, functions, and random number generation. It’s also a great example of how to create a simple yet engaging console-based game.

Feel free to modify the code, add new features, or customize the rules. For example, you could:

  • Add a graphical interface.
  • Introduce more choices (e.g., Dynamite, Water).
  • Implement a two-player mode.


Source code of C++ Project on rock paper scissors

#include <iostream>
#include <cstdlib>  // For rand() and srand()
#include <ctime>    // For time()
#include <string>   // For string handling

using namespace std;

// ANSI escape codes for text colors
#define RESET   "\033[0m"
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN    "\033[36m"

// Function to convert choice number to string
string getChoiceName(int choice) {
    switch (choice) {
    case 1: return "Rock";
    case 2: return "Paper";
    case 3: return "Scissors";
    case 4: return "Lizard";
    case 5: return "Spock";
    default: return "Invalid";
    }
}

// Function to determine the winner
int determineWinner(int userChoice, int computerChoice) {
    // Rules for Rock, Paper, Scissors, Lizard, Spock
    if (userChoice == computerChoice) {
        return 0; // Tie
    }
    switch (userChoice) {
    case 1: // Rock
        if (computerChoice == 3 || computerChoice == 4) return 1; // Rock crushes Scissors and Lizard
        break;
    case 2: // Paper
        if (computerChoice == 1 || computerChoice == 5) return 1; // Paper covers Rock and disproves Spock
        break;
    case 3: // Scissors
        if (computerChoice == 2 || computerChoice == 4) return 1; // Scissors cut Paper and decapitate Lizard
        break;
    case 4: // Lizard
        if (computerChoice == 2 || computerChoice == 5) return 1; // Lizard eats Paper and poisons Spock
        break;
    case 5: // Spock
        if (computerChoice == 1 || computerChoice == 3) return 1; // Spock vaporizes Rock and smashes Scissors
        break;
    }
    return -1; // Computer wins
}

int main() {
    int userChoice;
    int computerChoice;
    int userScore = 0;
    int computerScore = 0;
    int rounds = 0;
    int maxWins;
    char playAgain;

    // Seed the random number generator
    srand(static_cast<unsigned int>(time(0)));

    cout << GREEN << "Welcome to Rock, Paper, Scissors, Lizard, Spock!" << RESET << endl;
    cout << "How many wins to declare the overall winner? (e.g., 3): ";
    cin >> maxWins;

    do {
        rounds++;
        cout << CYAN << "\nRound " << rounds << RESET << endl;
        cout << "1. Rock" << endl;
        cout << "2. Paper" << endl;
        cout << "3. Scissors" << endl;
        cout << "4. Lizard" << endl;
        cout << "5. Spock" << endl;
        cout << "Enter your choice (1-5): ";
        cin >> userChoice;

        // Validate user input
        if (userChoice < 1 || userChoice > 5) {
            cout << RED << "Invalid choice. Please choose a number between 1 and 5." << RESET << endl;
            continue;
        }

        // Generate computer's choice
        computerChoice = rand() % 5 + 1;

        // Display choices
        cout << BLUE << "You chose: " << getChoiceName(userChoice) << RESET << endl;
        cout << MAGENTA << "Computer chose: " << getChoiceName(computerChoice) << RESET << endl;

        // Determine the winner
        int result = determineWinner(userChoice, computerChoice);
        if (result == 0) {
            cout << YELLOW << "                                   It's a tie!" << RESET << endl;
        }
        else if (result == 1) {
            cout << GREEN << "                                    You win this round!" << RESET << endl;
            userScore++;
        }
        else {
            cout << RED << "                                      Computer wins this round!" << RESET << endl;
            computerScore++;
        }

        // Display scores
        cout << CYAN << "Score: You " << userScore << " - " << computerScore << " Computer" << RESET << endl;

        // Check if someone has won the series
        if (userScore >= maxWins || computerScore >= maxWins) {
            if (userScore > computerScore) {
                cout << "";
                cout << GREEN << "                  Congratulations! You won the series!" << RESET << endl;
            }
            else {
                cout << RED << "                    Computer won the series. Better luck next time!" << RESET << endl;
            }
            break;
        }

        // Ask if the user wants to play another round
        cout << "";
        cout << "Do you want to play another round? (y/n): ";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    cout << GREEN << "Thanks for playing! Final Score: You " << userScore << " - " << computerScore << " Computer" << RESET << endl;

    return 0;
}


Leave a comment