Solution

/*

Programmer: Jennifer Leopold

Date: February 10, 2016

File: hw4.cpp

Purpose: Simulate a combination slot machine + ATM machine

that has a menu allowing the user to check their

bank balance, transfer funds to the game, play the

game, or quit the game.

To compile: g++ hw4.cpp -o hw4

To execute: ./hw4

*/

#include <iostream>

#include <ctime>

using namespace std;

// ASCII value of first character in seqn of chars for the slot machine

const short SYMBOL1 = 97;

// Number of display chars on slot machine tumblers (e.g. 'a'..'d')

const short NUM_DISPLAY_CHARS = 4;

// Wins/losses for various outcomes

const short LOSS_IF_NO_MATCH = 5;

const short WIN_IF_TWO_MATCH = 1;

const short WIN_IF_THREE_MATCH = 30;

// Minimum and maximum starting bank balances

const int MIN_INIT_BANK_BALANCE = 200;

const int MAX_INIT_BANK_BALANCE = 1000;

const int INIT_BANK_BALANCE_INCR = 100;

int main()

{

bool gameOver = false; // true when user wants to

// end the program

short action; // menu choice

int bankBalance = 0; // amt available to xfer

// to game balance

int gameBalance = 0; // balance to which game

// winnings/losses

// added/deducted

int transferAmount; // amt xfer'd from bank

// balance to game

// balance

int winnings; // winnings for 1 game

bool bankBalanceChecked = false; // true when user has

// checked their balance

// upon entering game

short tumbler1, tumbler2, tumbler3; // slot machine tumblers

short numMatches; // # tumbler char matches

short spins; // # games user plays

// at one time

// Greeting

cout << "\nWelcome to Cletus' ATM-Slot-o-Matic!\n";

// Seed the random number generator

srand(time(NULL));

// Initialize bank balance

bankBalance = MIN_INIT_BANK_BALANCE +

((rand() % ((MAX_INIT_BANK_BALANCE /

INIT_BANK_BALANCE_INCR) - 1)) *

INIT_BANK_BALANCE_INCR);

// Continue letting user play until s/he chooses to quit

do

{

cout << "\nOptions:\n";

cout << "1. Check bank balance\n";

cout << "2. Transfer funds to game\n";

cout << "3. Play\n";

cout << "4. Leave (cash out)\n";

cout << "Enter your choice (1-4): ";

cin >> action;

switch (action)

{

// Check bank balance

case 1: cout << "Current bank balance: $"

<< bankBalance << endl;

cout << "Current game balance: $"

<< gameBalance << endl;

bankBalanceChecked = true;

break;

// Transfer funds to game

case 2: if (! bankBalanceChecked)

cout << "**First check your bank balance!\n";

else

{

cout << "How much do you want transfer? ";

cin >> transferAmount;

if (transferAmount > bankBalance)

cout << "No, that amount is more than your "

<< "bank balance of $"

<< bankBalance << "!\n";

else if (transferAmount <= 0)

cout << "No, amount must be > 0!\n";

else {

bankBalance -= transferAmount;

gameBalance += transferAmount;

cout << "Transfer completed.\n";

cout << "Current bank balance: $"

<< bankBalance << endl;

cout << "Current game balance: $"

<< gameBalance << endl;

}

}

break;

// Play game

case 3: if (! bankBalanceChecked)

cout << "**First check your bank balance!\n";

else

{

do

{

cout << "How many times do you "

<< "want to spin? ";

cin >> spins;

if (spins <= 0)

cout << "Enter a positive number!\n";

} while (spins <= 0);

if ((spins * LOSS_IF_NO_MATCH) > gameBalance)

cout << "You don't have enough funds "

<< "to cover that many losses!\n";

else

{

for (short i = 1; i <= spins; i++)

{

tumbler1 = rand() % NUM_DISPLAY_CHARS;

tumbler2 = rand() % NUM_DISPLAY_CHARS;

tumbler3 = rand() % NUM_DISPLAY_CHARS;

cout << "\nYour result is "

<< static_cast<char>(tumbler1 +

SYMBOL1)

<< static_cast<char>(tumbler2 +

SYMBOL1)

<< static_cast<char>(tumbler3 +

SYMBOL1)

<< endl;

numMatches = 0;

if ((tumbler1 == tumbler2) ||

(tumbler1 == tumbler3) ||

(tumbler2 == tumbler3))

numMatches = 2;

if ((tumbler1 == tumbler2) &&

(tumbler1 == tumbler3) &&

(tumbler2 == tumbler3))

numMatches = 3;

if (numMatches == 2)

winnings = WIN_IF_TWO_MATCH;

else if (numMatches == 3)

winnings = WIN_IF_THREE_MATCH;

else winnings = -LOSS_IF_NO_MATCH;

gameBalance += winnings;

cout << "That is " << numMatches

<< " matches\n";

cout << "You "

<<((winnings > 0)? "win" : "lose")

<< " $" << abs(winnings) << endl;

cout << "Current game balance: $"

<< gameBalance << endl;

}

}

}

break;

// Leave (cash out)

case 4: if (! bankBalanceChecked)

cout << "**First check your bank balance!\n";

else gameOver = true;

break;

// Invalid selection

default: cout << "**Invalid choice!\n";

}

// If no more funds, don't let user continue

if ((bankBalance == 0) && (gameBalance == 0))

{

cout << "You're completely out of money, time to quit!\n";

gameOver = true;

}

} while (! gameOver);

// Output final balances and sign-off

cout << "\nEnding bank balance: $"

<< bankBalance << endl;

cout << "Ending game balance: $"

<< gameBalance << endl;

cout << "Y'all come play again sometime!\n";

return 0;

}