Program: Fruit machine using a while loop



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

This example creates a simple fruit machine.


At the start of the game, the player has £1 in credit. Each spin costs 20p. If all three reels match then they win some money and can play for longer. Once their money runs out, the game is over.




As with the "Simon says..." example, the program "seeds" the random number process, making it more unpredictable, using the current system time. The while loop checks how much money you have left and decides whether or not to play another game.

The program now repeatedly chooses random numbers to correspond to the positions of each of the three reels on the fruit machine.

The screenshot shows the program running on an Ubuntu laptop, but it can also be compiled and executed on an Apple Mac or Raspberry Pi.

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


To compile from the command-line:


    g++ -o fruit fruit.cpp


To run from the command-line:


    ./fruit




Here's the code:


#include <iostream>    // To use screen
#include <cstdlib>     // To use random()

#include <unistd.h>    // To see numbers using time()
#include <iomanip>     // Set decimal places to display

using namespace std;    // Names for cout and endl




int main()
{
    srandom( time( 0 ) );  // Seed the random numbers



    string symbol[5];  // Array of symbols on reel


    symbol[0] = "\e[35m  grapes ";
    symbol[1] = "\e[33m  pasty  ";
    symbol[2] = "\e[32m  melon  ";
    symbol[3] = "\e[31m cherries";
    symbol[4] = "\e[38;5;226m  lemon  ";



    int reel1, reel2, reel3;


    // Start with £1 credit    int credit = 100;


    // Keep playing until run out of money
    while ( credit > 0 )
    {



        // Play a game
        credit = credit - 20;  // Costs you 20p


        // Display money left on row 3, column 10 at top of screen
        cout << "\e[3;10H";  // Row 3, column 10
        cout << "Credit £";
        cout << fixed << setprecision(2);
        cout << static_cast<float>(credit)  /  100;
        cout << "        " << endl;



        // Simulate the spinning reels
        for ( int swaps=0; swaps<30; swaps++)
        {
            reel1 = random() % 5; reel2 = random() % 5; reel3 = random() % 5;


            cout << "\e[5;10H"; // Row 5, column 10
            cout << symbol[reel1] << flush;


            cout << "\e[5;30H"; // Row 5, column 30
            cout << symbol[reel2] << flush;


            cout << "\e[5;50H"; // Row 5, column 50
            cout << symbol[reel3] << flush;


            cout << endl;


            // Pause for tenth of a second
            usleep( 100000 );


        }  // end of the for loop


        // Reset colours
        cout << "\e[0m";


        // Move cursor to row 7, column 10 of screen
        cout << "\e[7;10H";


        // Check whether all 3 reels show the same thing
        // If they do you win the jackpot

        if ( reel1==reel2 and reel2==reel3 )
        {
            cout << "£5 JACKPOT!" << endl;
            credit = credit + 500;
        }
        else
            cout << "Try again. " << endl;



        // Pause for 3 seconds before next go
        sleep(3);


        // Move cursor to row 7, column 10 on screen
        cout << "\e[7;10H";
        cout << "              " << endl;



    }  // end of while loop


    // Program execution only reaches here if out of credit
    cout << "Out of money - Game over";
    cout << endl << endl;



    return 0;
}


Find out more - buy the book on Amazon!