Solution

// Programmer: Clayton Price date : 9-30-13 // File: slots.cpp class : cs 53 // Purpose: This file contains the main function for a program that emulates // a slot machine in Moe's bar. #include <iostream> #include <ctime>

#include <cstdlib>

using namespace std; int main() { /* -------------------------- declarations -------------------- */ const short NO_MATCH_LOSS = 5; // lose $5 for no matches const short ONE_MATCH_WIN = 1; // win $1 for one match const short TWO_MATCH_WIN = 30; // win $30 for two matches

const float INIT_GAME_BAL = 0; const int LOWER_INIT_BANK_BAL = 20000;// in cents!

const int UPPER_INIT_BANK_BAL = 100000;// in cents!

const short NUM_SIDES = 4; // number of sides on each reel of slot float game_bal=0; // user's money for game float bank_bal=0; // user's money in the Bankk float trans_amount; // user's transfer amount 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 char menu_choice; bool cash_out = false; bool first_time = true; // first time checking bal bool bank_bal_checked = false; bool funds_transferred = false; /* ------------------------ welcomes -------------------------- */ cout<<"\n\nWELCOME TO MOE'S. LET'S PLAY THE SLOTS!"<<endl<<endl; /* ------------------------ playing game ---------------------- */ cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); srand(time(NULL)); // seed generator 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>>menu_choice; switch (menu_choice) // handle choices { case '1': case 'c': // checking balance if(first_time) { first_time = false; bank_bal_checked = true; bank_bal =(rand()%(UPPER_INIT_BANK_BAL-LOWER_INIT_BANK_BAL+1)+LOWER_INIT_BANK_BAL)/100.0; // randomly assigns balance game_bal = INIT_GAME_BAL; } cout<<"\n\n\t\tYour Funds: "<<endl <<"\t\t\tBankk Balance: $"<<bank_bal<<endl <<"\t\t\tGame Balance: $"<<game_bal<<endl<<endl; break; case '2': case 't': if(bank_bal_checked) { 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 cout<<"\n\tYour new Bankk balance is: $"<<bank_bal<<endl <<"\n\t Your new game balance is: $"<<game_bal<<endl; } else cout<<"\n\t\tERROR: You must check your balance first!"<<endl; break; case '3': case 'p': if(funds_transferred) //make sure player has money { 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 { pre_game = game_bal; for(int i=1; i<=num_spins; i++) //for each spin of the slots { tumbler1=rand()%NUM_SIDES; tumbler2=rand()%NUM_SIDES; tumbler3=rand()%NUM_SIDES; /* NOTE: the following code demonstrates how difficult nested ternary can be to read! */ 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; } } } else cout<<"\n\n\t\tYOU MUST TRANSFER FUNDS TO THE GAME BEFORE PLAYING" <<endl<<endl; break; case '4': case 'l': case 'x': cash_out = true; cout<<"\n\n\tYou are cashing out. Your balances are:"<<endl<<endl <<"\t\tBankk Balance: $"<<bank_bal<<endl <<"\t\t Game Balance: $"<<game_bal<<endl<<endl; break; default: cout<<"\n\t\tERROR: invalid choice. TRY AGAIN"<<endl; } } while (!cash_out); cout<<"\nTHANKS FOR PLAYING. COME AGAIN !! "<<endl; return 0; }