Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Required Assignment: create a reference and a pointer (C++) a. Create a better dice game. Each player (user or computer) has 2 dices . And

Required Assignment: create a reference and a pointer (C++)

a. Create a better dice game. Each player (user or computer) has 2 dices. And you are required to use 1 reference variable and 1 pointer variable. You need to indicate where you use these in your code.

b. The winning rule is as follows: if 2 dices of a player have the same value as a pair of 6 and the other player does not have a pair of value then a pair of value will win. So, 5&5 will beat 6&4 because 5&5 is a pair. If both players have pairs, (user and computer), the higher pair will win. For example, 6&6 will beat any pair. If both players do not have pair, then higher total points will win.

c. The output of the program may be like the following:

Welcome to the dice ware game. You have 100 in your game purse.

Enter a bet amount to play (0 means exit the program): 10

You have 5 & 5 computer has is 3 & 4

You won 10 dollars!

Your game purse now has 110 dollars

Enter a bet amount to play (0 means exit the program): 5

You have 6 & 4 computer has is 1 & 1

You lost 5

Your game purse now has 105 dollars

Enter a bet amount to play (0 means exit the program): 10

You have 1 & 2 computer has is 3 & 4

You lost 10

Your game purse now has 95 dollars

Enter a bet amount Enter a bet amount to play (0 means exit the program): 0

You choose to exit the program. Thank you and goodbye

Use these class files and create a main:

//GamePurse.h file

#ifndef GamePurse_H

#define GamePurse_H

//GamePurse class declaration

class GamePurse {

int purseAmount; // in main.cpp initialize it to 100

public:

GamePurse(int);

void Win(int);

void Loose(int);

int GetAmount();

};

#endif

//GamePurse.cpp file

#include "GamePurse.h"

//constructor implementation

GamePurse::GamePurse(int balance){

//initializing purseAmount to 0

purseAmount=0;

//if balance is positive, updating purseAmount

if(balance>0){

purseAmount=balance;

}

}

//Win method implementation

void GamePurse::Win(int amount){

//adding amount to purseAmount if amount is positive

if(amount>0){

purseAmount+=amount;

}

}

//Loose method implementation

void GamePurse::Loose(int amount){

//subtracting amount from purseAmount if amount is positive and has enough balance

if(amount>0 && purseAmount>=amount){

purseAmount-=amount;

}

}

//GetAmount method implementation

int GamePurse::GetAmount(){

return purseAmount;

}

// DICEROLL.H FILE

#ifndef DICEROLL_H

#define DICEROLL_H

class DiceRoll

{

public:

DiceRoll(int=0);

void rollDice();

int getRoll() const;

private:

int rollValue;

};

#endif

// DICEROLL.CPP FILE

#include "DiceRoll.h"

# include

using namespace std;

DiceRoll::DiceRoll(int num) : rollValue(num){

}

void DiceRoll::rollDice() {

rollValue = 1+ rand()%6;

}

int DiceRoll::getRoll() const {

return rollValue;

}

// main.cpp // (not whole program) : below is older version. The newer version needs the requirements above

....(took out code)

#include "GamePurse.h"

#include "DiceRoll.h"

using namespace std;

int main(){

......(took out code)

GamePurse purse(100);

DiceRoll player;

DiceRoll computer;

....(took out code)

do {

cout << "Enter.....

cin >> betMoney;

cout << endl;

if(betAmt>0){

//checking if there is enough balance or not

if(betAmt>purse.GetAmount()){

cout<<"You don't have that much amount to bet"<

}else{

player.rollDice();

computer.rollDice();

//displaying both

cout<<"Your dice is "<< player.getRoll()<< endl;

cout<<"Computer dice is "<< computer.getRoll() << endl;

if(player.getRoll()>computer.getRoll()){

....(took out code)

}else if(player.getRoll() < computer.getRoll()){

.... (took out code)

}else{

cout<<"It's a tie!"<

}

cout<<"Your game purse now has" << purse.GetAmount << "dollars" <

}

}

}while(betAmt!=0); //until betAmt is 0

cout<<"You choose to exit the program. Thank you and goodbye"<

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions