Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Directions --Create all objects for a c++ dice game using the dice and player class and make sure all files are compiled separately. Also, create

Directions --Create all objects for a c++ dice game using the dice and player class and make sure all files are compiled separately. Also, create a separate player and game class.

So far, I have the separately compiled files but I would like to convert the logic and programming of this dice game into a working project with separate files.

Here are the files that I have.

//maindice.cpp

#include

#include "dice.h"

#include "player.h"

#include "Game.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

// Dice dice1();

// Player player1("");

return 0;

}

//Dice.cpp

#include

#include

#include "dice.h"

using namespace std;

Dice::Dice()

{

value=rand()%6 + 1;//assign a random 1-6

}

void Dice::roll()

{

value=rand()%6 + 1;//assign a random 1-6

}

int Dice::getValue()

{

return value;

}

//Dice.h

#ifndef DICE_H

#define DICE_H

#include

#include

using namespace std;

class Dice

{

private:

int value;

public:

Dice();

void roll();

int getValue();

};

#endif

//Game.cpp

#include "Game.h"

#include

#include "dice.h"

#include "player.h"

using namespace std;

Game::Game( string name, float bA, float iB, float eB, float s ):

player(name, bA, iB, eB, s)

{

}

//Game.h

#ifndef GAME_H

#define GAME_H

#include "dice.h"

#include "player.h"

class Game

{

private:

Player player;

Dice dice1;

Dice dice2;

public:

Game( string, float, float, float , float );

};

#endif

//player.cpp

#include

#include

#include

#include "player.h"

using namespace std;

Player::Player()

{

name="";

}

Player::Player(string n)

{

name=n;

}

Player::Player(string n, int a)

{

name=n;

age=a;

}

Player::Player(string n, int a, int s)

{

name=n;

age=a;

score=s;

}

Player::Player(string n, float w)

{

name=n;

winnings=w;

}

Player:: Player (string n ,float bA, float iB , float eB, float s )

{

name=n;

betAmount= bA;

initialBalance= iB;

endingBalance=eB;

sum=s;

}

//player.h

#ifndef PLAYER_H

#define PLAYER_H

#include

//#include "player.h"

using namespace std;

class Player

{

private:

string name;

int score;

int amountTurns;

int ranking;

int age;

int level;

float winnings;

float betAmount;

int highScore;

float initialBalance;

float endingBalance;

float sum;

public:

Player();

Player(string);

Player(string, int);

Player(string, int, int);

Player(string, float);

Player(string, float, float ,float , float);

//gets/sets

};

#endif

-----------------------------------------------------------------------------------

and this is the dice game I would like to have implemented into the separately compiled files.

// C++ dice game

#include #include #include #include using namespace std; using namespace std; // roll dice int roll() { int dice = rand()%6 + 1; return dice; }

// input valid bet int validBet(int balance) { int bet; int count = 0; while(true) { cout << "Enter a valid bet: "; cin >> bet;

count++;

if(bet <= 0 || bet > balance) cout << "Invalid bet "; else break;

if(count == 3) { cout << "Final balance: $" << balance << " "; cout << "Kindly leave the casino "; return 0; } }

return bet; }

int main() { srand(time(0)); // "Seed" the random generator

int initialbalance; int balance; int bettingAmount; int dice1; int dice2; int sum; cout << " Please Enter your starting balance in whole dollars: "; cin >> initialbalance; balance = initialbalance; cout << "Your beginning balance is $" << balance << ", good luck!! "; while(true) { bettingAmount = validBet(balance); if (bettingAmount == 0) { return 0; }

dice1 = roll();// Will hold the randomly generated integer between 1 and 6 dice2 = roll(); // Will hold the randomly generated integer between 1 and 6

sum = dice1 + dice2;

if(sum >= 2 && sum <= 6) { cout << "The roll is " << dice1 << " and " << dice2 << " for a " << sum << ", you win! "; balance = balance + bettingAmount; } else if(sum >= 7 && sum <= 12) { cout << "The roll is " << dice1 << " and " << dice2 << " for a " << sum << ", you lose! "; balance = balance - bettingAmount; }

cout << "Your balance is $" << balance << endl;

char choice; cout << " -->Do you want to continue (y/n)? "; cin >> choice;

if(choice == 'n' || choice == 'N') break;

}

if(balance > initialbalance) { cout << "You have $" << balance << endl; cout << "Well done! Have a wonderful evening! "; } else { cout << "You have $" << balance << endl; cout << "Better luck next time! Have a wonderful evening! "; }

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions