Slottsfncts.cpp

// Programmer: Clayton Price date: 4/9/19

// File: slottsfncts.cpp

// Purpose: This file contains the function definitions for the

// functions used int he slotts simulation program.

#include "slotts.h"

using namespace std;

// This function outputs message of welcome to users.

// Pre: none.

// Post: message output to the screen welcoming user.

void welcome()

{

cout<<"\n\nWELCOME TO MOE'S. LET'S PLAY THE SLOTS!"<<endl<<endl;

return;

}

// This function outputs message of farewell to users.

// Pre: none.

// Post: message output to the screen.

void goodbyes()

{

cout<<"\nTHANKS FOR PLAYING. COME AGAIN !! "<<endl;

return;

}

// This function will present a menu to the user and return a valid choice

// Pre: none.

// Post: menu is presented to user and only a valid char is returned.

char present_menu()

{

char choice;

do // present menu

{

cout<<"\n\n\t\tOPTIONS"<<endl<<endl

<<"\t1. Check Bankk Balance"<<endl

<<"\t2. Transfer Funds"<<endl

<<"\t3. Play"<<endl

<<"\t4. Leave/Cash Out"<<endl<<endl;

cout<<"\t\tYour choice: ";

cin>>choice;

} while (choice != '1' && choice != '2' && choice != '3' && choice != '4'

&& choice != 'c' && choice != 't' && choice != 'p' && choice != 'l'

&& choice != 'x');

return choice;

}

// The establish_bankBalance function will randomly give the player a bank bala

// between and including MIN_INITIAL_BAL and MAX_INITIAL_BAL, set first_time

// to false, set bank_bal_checked to true.

// Pre: none.

// Post: The bank_bal is set between and including MIN_INITIAL_BAL and MAX_INIT

// bank_bal_checked is true (ref var), first_time is set false (ref var).

void establish_bankBalance(bool & first_time,bool & bal_checked,float &

bank_balance)

{

int min=MIN_INITIAL_BAL / 100;

int max=MAX_INITIAL_BAL / 100;

first_time = false;

bal_checked = true;

bank_balance = static_cast<float>( (rand()%(max - min + 1) + min)*100 );

return;

}

// The show_balances function will display to the scree the game balance and

// the bank balance.

// Pre: none.

// Post: bank and game balances are displayed to the screen.

void show_balances(const float game_bal,const float bank_bal)

{

cout<<"\n\n\t\tYour Funds: "<<endl

<<"\t\t\tBankk Balance: $"<<bank_bal<<endl

<<"\t\t\tGame Balance: $"<<game_bal<<endl<<endl;

return;

}

// The transfer function will query the user for an amount of money to transfer

// and deduct a proper amount from the bank balance and add that to the game

// balance and sets funds_trasnferred to true.

// Pre: none.

// Post: valid transfer amount entered by user is deducted from bank bal (ref

// var) and added to the game bal (ref var). funds_transferred is now true.

void transfer(bool & funds_transferred,float & bank_bal,float & game_bal)

{

int trans_amount;

funds_transferred = true; //flags that this choice has been made

do

{

do

{

cout<<"\nHow much money do you want to transfer: ";

cin>>trans_amount;

if(trans_amount<=0)

cout<<"\n\t\tERROR; You can't transfer negative dollars! Try again."

<<endl;

} while(trans_amount < 0);

if (trans_amount > bank_bal)

cout<<"\n\t\tERROR: You don't have that much money in the bank! Try again."

<<endl;

} while (trans_amount > bank_bal);

bank_bal-=trans_amount; //diminish bank account

game_bal+=trans_amount; //add to game balance

return;

}

// The play game function will play the slots for the user based on how much

// money he/she has in the game.

// Pre: none.

// Post: The player's game_bal will be increased or decreased according to

// winnings or losses.

void play_game(float & game_bal)

{

float pre_game = 0; // used for temp var

short num_spins=0; // number of spins by user

short tumbler1,tumbler2,tumbler3; // values for each reel

char reel1,reel2,reel3; // chars for each reel

if (!game_bal) //no game money

cout<<"\n\t\tYou haven't any money to gamble with. Go find some!"

<<endl;

else

{

do

{

cout<<"\n\t\tHow many spins do you desire: ";

cin>>num_spins;

} while (num_spins <=0); //positive num spins check

if (num_spins*NO_MATCH_LOSS > game_bal)

cout<<"\n\tSO SORRY: you haven't enough money on the table\n "

<<"\tto cover the possible loss from "<<num_spins<<" spins."

<<endl;

else // playing!

{

pre_game = game_bal;

for(int i=1; i<=num_spins; i++) //for each spin of the slots

{

tumbler1=rand()%NUM_SIDES; //pick first side random

if(i<=MIN_CHEAT_SPINS || (game_bal-pre_game)<MAX_TOLLERANCE) //fair play

{

tumbler2=rand()%NUM_SIDES; //...also others non-cheating

tumbler3=rand()%NUM_SIDES;

}

else

cheat(tumbler1,tumbler2,tumbler3); // cheating !!

reel1=(tumbler1==0?'a':(tumbler1==1?'b':(tumbler1==2?'c':'d')));

reel2=(tumbler2==0?'a':(tumbler2==1?'b':(tumbler2==2?'c':'d')));

reel3=(tumbler3==0?'a':(tumbler3==1?'b':(tumbler3==2?'c':'d')));

cout<<reel1<<' '<<reel2<<' '<<reel3;

if (reel1==reel2 && reel2==reel3) // ALL MATCH

{

cout<<"\tTHREE OF A KIND: You win $"<<TWO_MATCH_WIN<<"!!\n";

game_bal+=TWO_MATCH_WIN;

}

else if(reel1 != reel2 && reel1 != reel3 && reel2 != reel3)//NO MATCH

{

cout<<"\tNO MATCHES: You lose $"<<NO_MATCH_LOSS<<".\n";

game_bal-=NO_MATCH_LOSS;

}

else // ONE MATCH

{

cout<<"\tONE MATCH: You win $"<<ONE_MATCH_WIN<<"!\n";

game_bal+=ONE_MATCH_WIN;

}

}

cout<<"\n\tTOTAL "<<((pre_game-game_bal)<0?"GAIN":"LOSS")<<" is: $"

<<((pre_game-game_bal)<0?-(pre_game-game_bal):pre_game-game_bal)

<<endl;

}

}

return;

}

// The cheat function will return via ref parameters the values of tumblers 2

// and 3 based on the value of tumbler 1. Their values will be such that no

// match is possible.

// Pre: none.

// Post: values for tumblers 2 and 3 are established for these ref variables

// based on the value of tumbler 1.

void cheat(const short tum1, short & tum2, short & tum3)

{

tum2=(tum1+2)%NUM_SIDES;

tum3=(tum1+3)%NUM_SIDES;

return;

}