Program: Making decisions using a switch statement... Simon says game


STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!

This program is a version of the game "Simon says..." It uses a switch statement to decide between several alternative messages to display on the screen.



Once running, the program "seeds" the random number process, making it more unpredictable, using the current system time. It then enters an infinite while loop.

The program now repeatedly chooses random numbers and makes decisions based on the values chosen.

It uses a simple if decision to determine whether or not to display the text "Simon says..." Remember, if you are not told "Simon says..." then the computer is bluffing and you shouldn't follow the instruction.

The program now uses a switch statement to determine what action you are told to carry out, such as "put your hands on your head".

As you can see from the screenshot above, the program can be compiled and executed on an Apple Mac. It will also work on a Raspberry Pi or on a PC with Ubuntu.

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 simon.cpp


To compile from the command-line:


    g++ -o simon simon.cpp


To run from the command-line:


    ./simon




Here's the code:


#include <iostream>    // Program will be displaying some text on the screen
#include <cstdlib>    // Uses the random() and srandom() functions
#include <ctime>    // Uses the time() function as the random seed
#include <unistd.h>    // Uses the sleep() function to pause during execution



using namespace std;    // Will be using standard identifiers string, cout and endl


int main()
{
    // Seed the random number generator so won't always select the same numbers
    srandom( time( 0 ) );



    int choice;
    string msg;
    int bluff;



    // Infinite loop that will keep making random choices of what to do
    while ( true )
    {
        // Choose a random number, between 0 and 3

        bluff = random() % 4;


        // If 1, 2 or 3 chosen display the message "Simon says "
        // If 0 was chosen then do nothing - it's a bluff!

        if ( bluff > 0 )
            cout << "Simon says... ";



        // Pick a random number between 0 and 3
        choice = random() % 4;


          // Set a message to display, depending on the random number that was chosen
        switch ( choice )
        {

            case 0:  msg = "Put your hands on your head";  break;
            case 1:  msg = "Turn around three times";      break;
            case 2:  msg = "Sit down";                     break;
            case 3:  msg = "Stand up";                     break;

        }  // end of switch statement


        // Wait 3 seconds before displaying what to do
        sleep( 3 );



        cout << msg << endl;


    }  // end of infinite while loop




    return 0;
}



Find out more - buy the book on Amazon...