In: Computer Science
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed for each game. Rules of this game: Initial cash for this game is specified by player. Maximum bet for each game is $1000. Minimum bet for each game is $30. Three dice are used per game. Player can place his or her bet on any one of those six face-patterns of the die for each game. The winning prize is based on how many matched dice have been found: If two matched dice have been found, then the prize is 2x. If three matched dice have been found, then the prize is 6x. If no matched die has been found, then the player loses his or her bet for this game-session.
// C++ program to simulate Million dollar Game
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
enum diePattern
{
Fish,
Shrimp,
Crab,
Chicken,
GoldenCoin,
Barrel
};
class Die
{
private:
diePattern dieValue;
public:
Die();
void Shake();
diePattern Get();
};
// constructor
Die::Die()
{
dieValue = Fish;
}
// function to roll a die
void Die::Shake()
{
int num = rand()%6;
switch(num)
{
case 0:
dieValue = Fish;
break;
case 1:
dieValue = Shrimp;
break;
case 2:
dieValue = Crab;
break;
case 3:
dieValue = Chicken;
break;
case 4:
dieValue = GoldenCoin;
break;
case 5:
dieValue = Barrel;
break;
}
}
// function to return the die value
diePattern Die::Get()
{
return dieValue;
}
// function declaration
void show_help();
void show_diePattern(diePattern die);
double check_win_or_lose(Die dice[3],double bet_amount);
int main() {
srand(time(NULL));
int choice, total_games=0, total_win=0;
double total_amount, bet_amount=0, amount_won;
Die dice[3];
// loop that continues till the user exits
do
{
cout<<"\n\t\t Million Dollar Game"<<endl;
cout<<"\t\t========================"<<endl;
cout<<"0. Help (display rules of the game)"<<endl;
cout<<"1. Enter the total amount of money you bring to the casino for gambling"<<endl;
cout<<"2. Roll the dice and then place your bet here (Max. is $1000 and Min. is $30)"<<endl;
cout<<"3. Check win or lose for this round only"<<endl;
cout<<"4. Display your gambling results"<<endl<<"\t o current cash on hand"<<endl<<"\t o Percentage of winnings"<<endl<<"\t o total number of games played so far"<<endl;
cout<<"9. Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 0: // display help
show_help();
break;
case 1:
// input of total amount of money
cout<<"Enter the total amount of money : $";
cin>>total_amount;
while(total_amount <= 0)
{
cout<<"Invalid amount. Amount must be greater than 0"<<endl;
cout<<"Enter the total amount of money : $";
cin>>total_amount;
}
break;
case 2: // place a bet on a pattern
dice[0].Shake();
cout<<"\nDice 1 : ";
show_diePattern(dice[0].Get());
cout<<"Enter the bet amount : $";
cin>>bet_amount;
// validate bet amount
while(bet_amount < 30 || bet_amount > 1000)
{
cout<<"Invalid bet amount. Bet amount must be between 30-1000 (inclusive)"<<endl;
cout<<"Enter the bet amount : $";
cin>>bet_amount;
}
// check if user has sufficient amount
if(bet_amount <= total_amount)
total_amount -= bet_amount;
else
cout<<"Insufficient amount left"<<endl;
break;
case 3:
// if user has places the bet, check user won or lost
if(bet_amount > 0)
{
amount_won = check_win_or_lose(dice,bet_amount);
total_games++;
if(amount_won > 0)
{
total_win++;
cout<<"You won $"<<amount_won<<endl;
}else
cout<<"You lose $"<<bet_amount<<endl;
total_amount += amount_won;
bet_amount = 0;
}else
cout<<"Place bet on a pattern first"<<endl;
break;
case 4:
// output the results
cout<<"Gambling Results : "<<endl;
cout<<"Current cash on hand : $"<<total_amount<<endl;
cout<<"Percentage of winnings : "<<fixed<<setprecision(2)<<(((float)(total_win*100))/total_games)<<endl;
cout<<"Total number of games played so far : "<<total_games<<endl;
break;
case 9:
cout<<"Thank you for playing the game"<<endl;
break;
}
}while(choice != 9);
return 0;
}
// function to display rules of the game
void show_help()
{
cout<<"\t\tRules of the game"<<endl;
cout<<"Initial cash for this game is specified by the player"<<endl;
cout<<"Maximum bet for each game is $1000"<<endl;
cout<<"Minimum bet for each game is $30"<<endl;
cout<<"Three dice are used per game"<<endl;
cout<<"Player can place his/her bet on any one of those six face-pattern of the die for each game"<<endl;
cout<<"The winning prize is based on how many matched dice have been found : "<<endl;
cout<<"\tIf two matched dice have been found then the prize is 2x"<<endl;
cout<<"\tIf three matched dice have been found then the prize is 6x"<<endl;
cout<<"\tIf no matched dice has been found then the player loses his or her bet for this game session"<<endl;
}
// function to display the pattern on the die
void show_diePattern(diePattern die)
{
switch(die)
{
case Fish:
cout<<"Fish";
break;
case Shrimp:
cout<<"Shrimp";
break;
case Crab:
cout<<"Crab";
break;
case Chicken:
cout<<"Chicken";
break;
case GoldenCoin:
cout<<"GoldenCoin";
break;
case Barrel:
cout<<"Barrel";
break;
}
cout<<endl;
}
// function to return the amount won by the user
double check_win_or_lose(Die dice[3],double bet_amount)
{
for(int i=1;i<3;i++)
{
dice[i].Shake();
}
cout<<"Dice 2 : ";
show_diePattern(dice[1].Get());
cout<<"Dice 3 : ";
show_diePattern(dice[2].Get());
if((dice[0].Get() == dice[1].Get()) && (dice[0].Get() == dice[2].Get()))
return 6*bet_amount;
else if((dice[0].Get() == dice[1].Get()) || (dice[0].Get() == dice[2].Get()))
return 2*bet_amount;
else
return 0;
}
//end of program
Output: