Question
C++ help! I need the .h files to have .cpp files but unsure of how to separate them without the code breaking. cout statements cannot
C++ help! I need the .h files to have .cpp files but unsure of how to separate them without the code breaking. cout statements cannot be in any of the classes either, only in main.cpp! Please help!
Trying to build a pig dice game (6 side die) that will first ask the user to pick between game A or game B. Then will proceed to have players 1 & 2 (HUMAN CONTROLLED, NO CP's) roll (for example, player one will have the option of rolling until they hit a 1 or snake eyes. If they choose to hold, it will go to player 2), while keeping track of the score.
*For game A (one dice game), if player 1 or 2 rolls a 1, their score goes back to 0.
*For game B (two dice game), if player 1 or 2 rolls snake eyes (two 1's) OR a 1, their score goes back to 0. If possible, include pointers for the classes!
Here is the code I have that:
Dice.h----------------------
#ifndef DICE_H_
#define DICE_H_
//import player and gamerules header file below
#include "Player.h"
#include "GameRules.h"
//declare class dice
class Dice
{
public:
//declare function roll for class dice
int roll()
{
//declare integer to store random number
int randInteger;
//setting random time to stop execution
srand(static_cast
//store value to rand integer
//range will be 1 to 6
randInteger = (int)(1 + rand() % (6 - 1 + 1));
return randInteger;
}
};
#endif /* DICE_H_ */
Player.h--------------------------
#ifndef PLAYER_H_
#define PLAYER_H_
//import string to
#include
//including game rules header file
#include "GameRules.h"
//importing dice header file
#include "Dice.h"
//declare player class
class Player
{
private:
//define name to store player name
std::string name;
//turn will decide whose turn is it
bool turn;
//store score of player
int score;
public:
//constructor of player using name field
Player(std::string name)
{
//set class name to parameter name
this->name = name;
//set turn to false initially
turn = false;
//set score to 0
score = 0;
}
//default constructor of class player
Player()
{
//set name to x
name = "X";
//set turn to false
turn = false;
//set score to 0
score = 0;
}
//copy constructor of player
//it will set current player to the one what is passing in parameter
Player(const Player &p)
{
//set this class pointer to the address of player p
*this = p;
}
//overriding = operator to assign player value to calling object
//passing player as const so that cannot change value of p
Player& operator=(const Player &p)
{
//set name to p name
name = p.name;
//set turn to p turn
turn = p.turn;
//set score to p score
score = p.score;
//return calling object reference
return *this;
}
//setter for name
void setName(std::string n)
{
//set name to n
name = n;
}
//setter for score
void Score(int sc)
{
//set score to sc
score = sc;
}
//getter for store. const so that cannot change value of score
int Score()const
{
//RETURN SCORE
return score;
}
//set turn
void Turn(bool t)
{
//set turn to t
turn = t;
}
//get the turn. const so that cannot change turn
bool Turn()const
{
//return turn
return turn;
}
//getter of the name
std::string Name()const
{
return name;
}
};
#endif /* PLAYER_H_ */
GameRules.h----------------------
//include both header file
#include "Player.h"
#include "Dice.h"
#ifndef GAMERULES_H_
#define GAMERULES_H_
//declare clas game rules
class GameRules
{
//define protected variables
protected:
//define two players
//define player 1
Player p1;
//define player 2
Player p2;
//define public variables
public:
//constructor of game rules
//passing two player in parameter
GameRules(Player a, Player b)
{
//set both player to a and b
p1 = a;
p2 = b;
}
//setter for player one
void setPlayer1(Player a)
{
//setting p1 to a
//calling overloaded = operator here
p1 = a;
}
//setter for player two
void setPlayer2(Player b)
{
//setting p2 to b
//calling overloaded = operator here
p2 = b;
}
//create virtual function play
virtual void play() = 0;
};
//define game mode a that extends game rules
class GameModeA : public GameRules
{
//adding dice to the private member
private:
//adding dice d1
Dice d1;
//define public methods to the class dice
public:
//adding gamemode a constructor
//calling parent constructor as well by doing : gameRules(a,b)
GameModeA(Player a, Player b) : GameRules(a, b){}
//define virtual function play definition here
void play()
{
//set player one turn to true
p1.Turn(true);
int n;
char playerChoice;
//loop until it's player one turn
while (p1.Turn())
{
//print score of player one
std::cout << " Score = " << p1.Score();
//print player name and instructions
std::cout << " It Is " << p1.Name() << " Turn: Roll The Dice (R) or Hold Your Turn (H):";
//get playerChoice
std::cin >> playerChoice;
//change choice to upper case
playerChoice = (toupper(playerChoice));
//check if choice is H
if (playerChoice == 'H')
{
//set player one turn to false
p1.Turn(false);
//set player two turn to true
p2.Turn(true);
//output player one name and score
std::cout << " " << p1.Name() << " Score = " << p1.Score();
break;
}
//roll the dice and get random number n
n = d1.roll();
//if n is 1
if (n == 1)
{
//set player one turn to false
p1.Turn(false);
//set player two turn to true
p2.Turn(true);
//set player one score to 0
p1.Score(0);
//print player one name and lose message
std::cout << " " << p1.Name() << " Rolled a Pig ";
//print score
std::cout << " " << p1.Name() << " Score = " << p1.Score();
break;
}
//increase player one score by n
p1.Score(p1.Score() + n);
}
//loop until player two turn
while (p2.Turn())
{
//print score
//everything is same as mentioned above
std::cout << " Score = " << p2.Score();
std::cout << " It Is " << p2.Name() << " Turn: Roll The Dice (R) or Hold Your Turn (H):";
std::cin >> playerChoice;
playerChoice = (toupper(playerChoice));
if (playerChoice == 'H')
{
p2.Turn(false);
p1.Turn(true);
std::cout << " " << p2.Name() << " Score = " << p2.Score();
break;
}
n = d1.roll();
if (n == 1)
{
p2.Turn(false);
p1.Turn(true);
std::cout << " " << p2.Name() << " Rolled a Pig ";
std::cout << " " << p2.Name() << " Score = " << p2.Score();
break;
}
p2.Score(p2.Score() + n);
}
//if player one score is more than player two
if (p1.Score() > p2.Score())
{
//print player one win
std::cout << " Player " << p1.Name() << " wins the game! ";
}
//if player one score is less than player two
else if (p1.Score() < p2.Score())
{
//print player two win
std::cout << " Player " << p2.Name() << " wins the game! ";
}
else
{
//else print draw
std::cout << " Game Draw ";
}
}
};
//game mode b extends gameRules
//this is second child class
class GameModeB : public GameRules
{
private:
//having two dice in game mode B
Dice d1;
Dice d2;
public:
//constructor player a and b : calling parent class constructor also
GameModeB(Player a, Player b) :GameRules(a, b){}
//declare gamemodeB play definition
void play()
{
//set player one turn to true
p1.Turn(true);
int n, n1;
char playerChoice;
//while player one turn
while (p1.Turn())
{
//print score
std::cout << " Score = " << p1.Score();
//print turn notification
std::cout << " It Is " << p1.Name() << " Turn: Roll The Dice (R) or Hold Your Turn (H):";
//get player choice
std::cin >> playerChoice;
//convert choice to upper case
playerChoice = (toupper(playerChoice));
//if choice is H
if (playerChoice == 'H')
{
//set player one turn to false
p1.Turn(false);
//set player two turn to true
p2.Turn(true);
std::cout << " " << p1.Name() << " Score = " << p1.Score();
break;
}
//roll first dice
n = d1.roll();
//roll second dice
n1 = d2.roll();
//if both are showing 1
if (n == 1 && n1 == 1)
{
//player one turn to false
p1.Turn(false);
//player two turn to true
p2.Turn(true);
//player one score to 0
p1.Score(0);
std::cout << " " << p1.Name() << " Rolled a Pig ";
std::cout << " " << p1.Name() << " Score = " << p1.Score();
break;
}
//increase player one score by n and n1
p1.Score(p1.Score() + n + n1);
}
//while player two turn
//logic is same as above
while (p2.Turn())
{
std::cout << " Score = " << p2.Score();
std::cout << " It Is " << p2.Name() << " Turn: Roll The Dice (R) or Hold Your Turn (H):";
cin >> playerChoice;
playerChoice = (toupper(playerChoice));
if (playerChoice == 'H')
{
p2.Turn(false);
p1.Turn(true);
std::cout << " " << p2.Name() << " Score = " << p2.Score();
break;
}
n = d1.roll();
n1 = d2.roll();
if (n == 1 && n1 == 1)
{
p2.Turn(false);
p1.Turn(true);
p2.Score(0);
std::cout << " " << p2.Name() << " Rolled a Pig ";
std::cout << " " << p2.Name() << " Score = " << p2.Score();
break;
}
p2.Score(p2.Score() + n + n1);
}
if (p1.Score() > p2.Score())
{
std::cout << " Player " << p1.Name() << " Win The Game ";
}
else if (p1.Score() < p2.Score())
{
std::cout << " Player " << p2.Name() << " win the Game ";
}
else
{
std::cout << " Game Draw ";
}
}
};
#endif /* GAMERULES_H_ */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started