Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: To learn how to write a simple game program using top down design. T

ID: 3739015 • Letter: O

Question

Objectives:

To learn how to write a simple game program using top down design.

To learn how to program using one dimensional arrays.

To learn how to use object oriented programming by using class design

To learn how to pass arrays to functions.

To provide appropriate documentation and use of good programming style.

Description of the problem:

Write a simple menu-driven program called “Game of Dice”. 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, Golden_coin, Magic_barrel. This game has two 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 three 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$5000.

Minimum bet for each game is$10.

Two dice are used per game.

Player can place his or her bet on anyone of those six face-patterns of the dice for each game.

The winning prize is based on how many matched dice have been found:

If one matched die has been found, then the prize is2x.

If two matched dice have been found, then the prize is4x.

If no matched die has been found, then the player loses his or her bet for this game-session.

Example of a menu could look like the following:

                  Game of dice: <Fish Shrimp Crab>

                  **************************************

Enter the total amount of money you bring in the casino for gambling

Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)

Open the container that holds the dice

Display your gambling results:

current cash on hand

number of winnings

total number of plays so far

Optional: The menu-screen could be graphical, instead of text based screen.

Hints:

< > dicePattern { Fish = 1, Shrimp, Crab, Chicken, Golden_coin, Magic_barrel };

To generate a random number between 1 to 6:rand() % 6 + 1

Requirement:

Your program has to be implemented with a class calledDice.

The “class Die” should posses the following characteristics:

< > ….. constructor function used to initialize the dice.

< > ….. used to randomly generate the face value of a dice.

< > ….. used to get the face value of a dice.

A private member variable calledFacewith data type.

Feel free to add any other features that you deem necessary.

                  Game of dice: <Fish Shrimp Crab>

                  **************************************

Enter the total amount of money you bring in the casino for gambling

Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)

Open the container that holds the dice

Display your gambling results:

current cash on hand

number of winnings

total number of plays so far

Explanation / Answer

#include<iostream>
#include<string>
#include <stdlib.h> // rand

using namespace std;

// Class Dice definition
class Dice
{
// Data members to store data
int Face;
double balance;
double bet;
int countWin;
int totalPlay;
public:
// Prototype of member functions
int generateRandom();
Dice();
void menu();
string getFaceString(int);
void displayInit();
void play();
int findMatch(int, int);
double getBalance();
};// End of class

// Function to return current balance
double Dice::getBalance()
{
return balance;
}// End of function

// Function to generate and return a random number
int Dice::generateRandom()
{
// Generates and returns a random number between 1 and 6
return rand() % 6 + 1;
}// End of function

// Default constructor to initialize data member
Dice::Dice()
{
Face = 0;
balance = bet = 0;
countWin = totalPlay = 0;
}// End of constructor

// Function to return the string form of the random number generated
string Dice::getFaceString(int no)
{
// Checks the number passed as parameter and returns the appropriate string data
switch(no)
{
case 1:
return "Fish";
case 2:
return "Shrimp";
case 3:
return "Crab";
case 4:
return "Chicken";
case 5:
return "Golden_coin";
case 6:
return "Magic_barrel";
}// End of switch case
}// End of function

// Function to display initial message
void Dice::displayInit()
{
// Displays message
cout<<" ****************************************************";
cout<<" Game of dice: <Fish Shrimp Crab>";
cout<<" ***************************************************";
// Accepts initial balance
cout<<" Enter the total amount of money you bring in the casino for gambling: ";
cin>>balance;
}// End of function

// Function to display menu and accept user choice
void Dice::menu()
{
// Loops till valid choice entered by the user
do
{
// Displays menu
cout<<" 1 - Fish 2 - Shrimp 3 - Crab 4 - Chicken 5 - Golden_coin 6 - Magic_barrel";
// Accepts user choice
cout<<" Select the bet on any die-pattern.";
cin>>Face;
// Checks if the user choice is between 1 and 6 inclusive then valid
if(Face >= 1 && Face <= 6)
// Come out of the loop
break;
// Otherwise, invalid choice
else
cout<<" Invalid selection. Please re enter.";
}while(1); // End of do - while loop
}// End of function

// Function to play the game
void Dice::play()
{
// Increase the total number of play counter by one
totalPlay++;
// Calls the function to display the menu
menu();
// Loops till valid bet entered by the user
do
{
// Accepts bet amount
cout<<" Roll the dice and then place your bet here";
cout<<" (Max. is $5000, Min. is $10)";
cin>>bet;
// Checks if the bet amount is between 10 and 5000 then valid
if(bet >= 10 && bet <= 5000)
{
// Checks if the bet amount is less than or equals to current balance then valid
if(bet <= balance)
// Come out of the loop
break;
// Otherwise, invalid bet amount
else
cout<<" Insufficient balance. Current Balance: "<<balance;;
}// End of if condition
// Otherwise invalid range of bet amount
else
cout<<" Invalid bet amount. Try again.";
}while(1); // End of do - while loop
// Call the function to generate random number
int no1 = generateRandom();
int no2 = generateRandom();
// Call the function to find the match
int res = findMatch(no1, no2);
// Displays the two random faces of dice generated
cout<<" Random Face1: "<<getFaceString(no1)<<" Random Face2: "<<getFaceString(no2);
// Checks if the return result of findMatch() function is two (for two match)
if(res == 2)
{
// Update the balance by 4 times of bet
balance += 4 * bet;
// Increase the win counter by one
countWin++;
}// End of if condition
// Otherwise, checks if the return result of findMatch() function is one (for one match)
else if(res == 1)
{
// Update the balance by 2 times of bet
balance += 2 * bet;
// Increase the win counter by one
countWin++;
}// End of else if
// Otherwise there is no match
else
// Update the balance by deducting bet amount
balance -= bet;
// Displays the information
cout<<" Display your gambling results:";
cout<<" Current cash on hand: "<<balance;
cout<<" Number of win: "<<countWin;
cout<<" Total number of plays so far: "<<totalPlay;
}// End of function

// Function to find match and return number of match
int Dice::findMatch(int no1, int no2)
{
// Checks for two match
if(Face == no1 && Face == no2)
return 2;
// Check for one match
else if(Face == no1 || Face == no2)
return 1;
// Otherwise, no match
else
return 0;
}// End of function

// main function definition
int main()
{
// Creates an object of class Dice
Dice d;
// Initializes user choice to continue to 'y'
char choice = 'y';
// Calls the function to display initial message
d.displayInit();
// Loops till user choice is 'y' or 'Y'
do
{
// Calls the function to play the game
d.play();
// Checks if the current balance is zero or less than zero stop the program
if(d.getBalance() <= 0)
{
cout<<" No more balance to play.";
exit(0);
}// End of if condition
// Accepts the user choice to continue or not
cout<<" Would you like to play another game (Y/N): ";
cin>>choice;
}while(choice == 'Y' || choice == 'y'); // End of do - while loop
}// End of main function

Sample Output:


****************************************************
Game of dice: <Fish Shrimp Crab>
***************************************************
Enter the total amount of money you bring in the casino for gambling: 100

1 - Fish
2 - Shrimp
3 - Crab
4 - Chicken
5 - Golden_coin
6 - Magic_barrel
Select the bet on any die-pattern.1

Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)9

Invalid bet amount. Try again.
Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)6000

Invalid bet amount. Try again.
Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)50

Random Face1: Magic_barrel
Random Face2: Magic_barrel
Display your gambling results:
Current cash on hand: 50
Number of win: 0
Total number of plays so far: 1
Would you like to play another game (Y/N): y

1 - Fish
2 - Shrimp
3 - Crab
4 - Chicken
5 - Golden_coin
6 - Magic_barrel
Select the bet on any die-pattern.1

Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)60

Insufficient balance.
Current Balance: 50
Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)40

Random Face1: Golden_coin
Random Face2: Golden_coin
Display your gambling results:
Current cash on hand: 10
Number of win: 0
Total number of plays so far: 2
Would you like to play another game (Y/N): y

1 - Fish
2 - Shrimp
3 - Crab
4 - Chicken
5 - Golden_coin
6 - Magic_barrel
Select the bet on any die-pattern.2

Roll the dice and then place your bet here
(Max. is $5000, Min. is $10)10

Random Face1: Magic_barrel
Random Face2: Golden_coin
Display your gambling results:
Current cash on hand: 0
Number of winn: 0
Total number of plays so far: 3
No more balance to play.