//gamble.cpp, machine.cpp, gambler.cpp
#include <iostream>
using namespace std;
#include "prompt.h"
#include "gambler.h"
#include "machine.h"
#include "dice.h"
void OneGame(int money, int coinType, int & amount)
{
SlotMachine S(coinType);
Gambler G(money);
int coins=money/coinType; //coins = max number of tries
if (money % coinType==0)
{
cout<<"You have "<<coins <<" coins.";
}
else
{
cout<<"You have " << coins <<" coins to spend and "
<< money % coinType <<" dollars remaining.";
}
int factor=S.Spin(); //Value is stored so it remains constant
while (S.NumSpins() > 0)
{
S.Spin(); //update is included in Spin()
if (factor>0)
{ G.GetWinnings(factor);
}
else ; //continue without changing course
amount+=factor; //Totals the factors from each round and will
//be used to calculate total amount of money won/lost}
}
void EndGame(int money, int amount) //no need for & because function is not
//returning or passing a value{
Gambler G(money);
int p=G.UpdateWinnings(amount); // value of amount changed in OneGame
cout<<"Total winnings are $" << p;
if (p < money)
{ cout<<"Bad luck this time. You lost money.";
}
else if (p > money)
{ cout<<"You won money!!";
}
else
{ cout<<"What is money good for? Absolutely nothing!";
}
}
int main ()
{
int money= PromptRange("How many dollars do you want to spend? ",1,1000);
cin >> money;
Gambler G(money);
string response=PromptString("Which dollar machine do you wish to play (one,five or ten)?");
cin >> response;
while (response!= "one"|| response!="five" || response!="ten")
{
cout<<"Oops. Please try again.";
}
int coinType;
if (response=="one") //player does not have choice over machine if he
coinType=1; //chooses to play again
else if (response=="five")coinType=5;
else
coinType=10;
int amount; //amount is referenced in OneGame,passed back to
//EndGame and then finally changed to money to start next gamedo
{
OneGame(money, coinType, amount);
EndGame(money, amount);
money=amount;
}while (G.PlayAgain());
return 0;
}
using namespace std;
#include "machine.h"
#include "dice.h"
SlotMachine::SlotMachine (int coinType) //constructor(no return type)
{
myNumSpins = 0;
myCoinType = coinType;
}
bool SlotMachine::twoEqual(int a, int b, int c )
// pre: all three values are integers
// pos: Returns true if two numbers are equal and false if none are equal
{
return (a==b || a==c || b==c );
}
bool SlotMachine::allEqual(int a, int b, int c)
// pre: all values are integers
// post: Returns true if all values are equal
{
return (a==b && b==c);
}
bool SlotMachine::bonus()
// post: Randomly decides if the bonus, in this case the *, will be displayed
{
Dice coin(2);
if (coin.Roll() == 1) return true;
else return false;
}
int SlotMachine::GetCoinType()
// pre: Player correctly selects type of machine: one, five, or ten
// post: Returns the numerical value of the machine type
{
return myCoinType; //a number
}
int SlotMachine::Spin ()
// pre: myNumSpins has not exceeded max number of spins
// post: randomly displays three numbers, with possible * in middle column,
// in a row and generates the appropriate winning factor if the players wins
{
int winnings = 0;
Dice d(10);
Dice d2(9);
int a=d.Roll(); int b=d2.Roll(); int c=d.Roll();
string star="*";
if (bonus())
cout<<"Spinning... Numbers are "<< a <<" "<<star<<" "<<c;
else b = d2.Roll();
cout<<"Spinning... Numbers are "<< a << " " << b <<" "<< c;
do
{
if (twoEqual(a,b,c) || bonus())
{
winnings=2;
}
else if (allEqual (a,b,c) || a==c && bonus()) //will this work?
{
winnings=10;
}
else winnings=0;
myNumSpins--;
} while (winnings==0);
return winnings; //a factor, not a sum
}
int SlotMachine::NumSpins()
{
return myNumSpins; //already initialized by constructor
}
using namespace std;
#include "gambler.h"
#include "machine.h"
Gambler:: Gambler (int money) //constructor
{
myMoney=money;
myWinnings=0;
}
int Gambler::GetWinnings (int factor)
// pre: spin()>0 , i.e. the player won
// post: returns the amount of money won in dollar value in one spin
{
int value=factor*myCoinType;
cout<<"CONGRATULATIONS!! You've won "<< value << "dollars!";
return value;
}
int Gambler::UpdateWinnings(int amount)
// pre: receives sums of factors from winning rounds
// post: Converts factors into the how much money is won/lost
{
myWinnings=amount*myCoinType;
return myWinnings;
}
bool Gambler::PlayAgain()
// pre: myNumSpins > coins
// post: Returns true if player wants to play again and false otherwise
{
string response;
cout << "Do you want to play your $"<< myWinnings <<" in winnings?";
cin >> response;
return (response=="yes");
}