Program: Rock-Paper-Scissors-CHAINSAW!

Here's a variation on the usual "rock-paper-scissors" game, with an added chainsaw to make it a little more interesting.




Each time you take a turn, the code uses a simple 2-dimensional array of bool values to determine whether or not your choice beats that of the computer. It then uses another 2-dimensional array of string values to display the appropriate message.

Note that the program only allows valid choices: "r" for a rock, "p" for paper, "s" for scissors or "c" for chainsaw. This validation is achieved using the simple switch statement. Note also that the player can type their choice in either upper-case or lower-case letters. Their choice is always converted to lower-case using the to lower() function.



To try out the program, select and copy the C++ source-code at the bottom of this post.

Paste into a text editor, such as Nano or Geany.

Then save the new file, ending in .cpp

I used  chainsaw.cpp


To compile from the command-line:


    g++ -o chainsaw chainsaw.cpp


To run from the command-line:


    ./chainsaw



Here's the code:


#include <iostream>    // Making use of screen and keyboard
#include <unistd.h>    // Using time to seed random function
#include <cstdlib>    // Using random function

using namespace std;    // Allows shorter names for cout, cin, endl

int main()
{
    // ANSI colour codes used on the screen
    const string RED = "\e[31m";
    const string GREEN = "\e[32m";
    const string NORMAL = "\e[0m";
    
    const string OBJECT_NAME[] = { "rock", "paper", "scissors", "chainsaw" };
    const int NUM_OBJECTS = 4;

    bool WIN[NUM_OBJECTS][NUM_OBJECTS];
    string DESCRIPTION[NUM_OBJECTS][NUM_OBJECTS];

    // What happens if you choose rock?
    WIN[0][1] = false;  // paper wraps your rock
    DESCRIPTION[0][1] = "Paper wraps rock.";

    WIN[0][2] = true;  // your rock blunts scissors
    DESCRIPTION[0][2] = "Rock blunts scissors.";

    WIN[0][3] = true;  // your rock beats chainsaw
    DESCRIPTION[0][3] = "Rock blunts chainsaw.";

    // What happens if you choose paper?
    WIN[1][0] = true;  // your paper wraps rock
    DESCRIPTION[1][0] = "Paper wraps rock.";

    WIN[1][2] = false;  // your paper cut by scissors
    DESCRIPTION[1][2] = "Scissors cut paper.";

    WIN[1][3] = false;  // your paper shredded by chainsaw
    DESCRIPTION[1][3] = "Chainsaw shreds paper.";

    // What happens if you choose scissors?
    WIN[2][0] = false;  // your scissors blunted by rock
    DESCRIPTION[2][0] = "Rock blunts scissors.";

    WIN[2][1] = true;  // your scissors cut through paper
    DESCRIPTION[2][1] = "Scissors cut paper.";

    WIN[2][3] = false;  // your scissors mangled by chainsaw
    DESCRIPTION[2][3] = "Chainsaw mangles scissors.";

    // What happens if you choose chainsaw?
    WIN[3][0] = false;  // your chainsaw smashed by rock
    DESCRIPTION[3][0] = "Rock smashes chainsaw.";

    WIN[3][1] = true;  // your chainsaw shreds paper
    DESCRIPTION[3][1] = "Chainsaw shreds paper.";

    WIN[3][2] = true;  // your chainsaw mangles scissors
    DESCRIPTION[3][2] = "Chainsaw mangles scissors.";

    // Seed random number process so different each time program runs
    srandom( time( 0 ) );

    cout << "ROCK-PAPER-SCISSORS-CHAINSAW!" << endl;
    cout << "-----------------------------" << endl << endl;

    char yourChoice;
    int yourIndex, computerIndex;

    while ( true )
    {
        // Allow user to type in their choice
        cout << "Choose (r p s c): ";
        cin >> yourChoice;
        yourChoice = tolower( yourChoice );

        // Translate their choice into an array index
        switch ( yourChoice )
        {
            case 'r':    yourIndex = 0;    break;
            case 'p':    yourIndex = 1;    break;
            case 's':    yourIndex = 2;    break;
            case 'c':    yourIndex = 3;    break;
            default:    yourIndex = -1;    break;

        }  // end of switch decision

        // Computer makes random choice of object
        computerIndex = random() % NUM_OBJECTS;

        if ( yourIndex >= 0 )
        {
            // If person and computer choose same thing is a draw
            if ( yourIndex == computerIndex )
            {
                cout << "You both chose " << OBJECT_NAME[yourIndex];
                cout << ". It's a draw!" << endl;
            }
            else
            {
                // Not a draw - decide who won

                cout << "You chose " << OBJECT_NAME[yourIndex];
                cout << "." << endl;

                cout << "Computer chose " << OBJECT_NAME[computerIndex];
                cout << "." << endl;

                // Display what happens for these particular choices
                cout << DESCRIPTION[yourIndex][computerIndex] << " ";

                // Display who won the round
                if ( WIN[yourIndex][computerIndex] )
                    cout << GREEN << "You win.";
                else
                    cout << RED << "Computer wins.";

            }  // end of inner if-else decision that processes valid choices

        }
        else
            cout << "You didn't make a valid choice!";

        cout << NORMAL << endl << endl;

    }  // end of infinite while block

}  // end of main function