For an assignment I'm required to create a game using die rolls that uses multiple classes. For Dice.cpp
I i wrote a function that just gets a random die roll from 1 to 6, and then I need to write a separate class that takes the die roll's value and uses it to determine what piece is added in the game, which i did in player.cpp
. I tried using #include "Dice.h"
just like i would for main.cpp, but that still tells me that my d.getRoll()
is not defined in its scope. The takeTurn function is where the problem is, and I'm required to make it void and have no parameters. Any help would be great.
player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include "Dice.h"
using namespace std;
class player
{
public:
player();
void setPlayerName();
string getPlayerName();
void setCootieName();
string getCootieName();
void takeTurn();
private:
int numLeg, numHead, numEye, numWing, numBody, numAntenna;
string cootieName;
string playerName;
int roll;
};
#endif // PLAYER_H
player.cpp
#include "player.h"
player::player()
{
numLeg = 0;
numHead = 0;
numEye = 0;
numWing = 0;
numBody = 0;
numAntenna = 0;
cootieName = "Undefined";
playerName = "Undefined";
}
void player::setPlayerName()
{
getline(cin, playerName);
}
string player::getPlayerName()
{
return(playerName);
}
void player::setCootieName()
{
getline(cin, cootieName);
}
string player::getCootieName()
{
return(cootieName);
}
void player::takeTurn()
{
roll = d.getRoll();
"You rolled a " << roll << "." << endl;
if (roll == 1)
{
numBody++;
}
else if (roll == 2)
{
numHead++;
}
else if (roll == 3)
{
numLeg++;
}
else if (roll == 4)
{
numAntenna++;
}
else if (roll == 5)
{
numWing++;
}
else
{
numEye++;
}
cout << "Cootie called " << cootieName << " has: " << endl;
cout << numLeg << " Leg(s)" << endl;
cout << numHead << " Head(s)" << endl;
cout << numEye << " Eye(s)" << endl;
cout << numWing << " Wings(s)" << endl;
cout << numBody << " Body(s)" << endl;
cout << numAntenna << " Antenna(s)" << endl;
}
Dice.h
#ifndef DICE_H
#define DICE_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
class Dice
{
public:
Dice(int, int);
int getRoll();
private:
int rollTop, rollBot;
};
#endif // DICE_H
Dice.cpp
#include "Dice.h"
Dice::Dice(int bot, int top)
{
rollBot = bot;
rollTop = top;
}
int Dice::getRoll()
{
return(rand() % rollTop + rollBot);
}
EDIT: Here's the main.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include "Dice.h"
#include "player.h"
using namespace std;
int main()
{
srand (time(NULL));
int playerChoice;
Dice d(1,6);
player p;
cout << "Enter player name" << endl;
p.setPlayerName();
cout << "Your name is " << p.getPlayerName() << "." << endl << endl;
cout << "Time to create a Cootie!" << endl;
cout << "Please name your Cootie!" << endl;
p.setCootieName();
cout << "Add body parts using die rolls." << endl;
cout << "To roll the die, input 1" << endl;
cout << "To exit, input 0" << endl;
cin >> playerChoice;
while(1)
{
if (playerChoice == 1)
{
p.takeTurn();
}
else
{
return 0;
}
}
}
You need to either create an instance of
Dice
to call the method on like thisor you need to make getRoll static. In the function declaration in Dice.h you'd put
and you would use it like this