random dice roll

Hi, can someone look at this code and tell me if it matches the
requirements. I need to write this program which simulates a game
where a player throws 5 dice and can reroll all of the dice (all
or none) if the player wants. A player can only reroll all the dice
once but does not have to do the reroll. After the original roll and
after possibly rerolling all the dice the sum of all the dice is the
players score. The dice are a little unusual. The first one only has
4 sides, the second has 5 sides, the third has 6 sides,the fourth has
seven sides and the fifth has eight sides. The sides of the first die
are numbered from 1 to 4. The sides of the second die are numbered from
1 to 5, etc.
This is my code:

---Die.h------
class Die
{
public:
Die(); //Default constructor
Die(int sides); //Explicit value constructor
void Print(); //Prints a test
~Die(); //Destructor
void roll();
void conditionalReRoll();
int getValue();
private:
int value;
int numberOfSides;
};


---Die.cpp------
Die::Die()
{
//cout<<"Hello, It's good to be alive."<<endl;
value=0;
numberOfSides=6;
}

Die::Die(int sides)
{
//cout<<"Hello, It's good to be alive."<<endl;
value=0;
numberOfSides=sides;
}

Die::~Die()
{
cout<< "Good bye, cruel world" <<endl;
}

void Die::roll()
{
value=1+(rand()%numberOfSides);
}

void Die::conditionalReRoll()
{
double expectedValue = (numberOfSides+1)/2.0;
if(value<expectedValue)
{
roll();
}
}

int Die::getValue()
{
return value;
}


---SetOfDice.h-------
class SetOfDice
{
public:
//void addDiceToSet(Die die);
void operator += (Die die);
void roll();
void conditionalReRoll();
int getValue();
private:
vector<Die> vectorOfDice;
};


---SetOfDice.cpp----
void SetOfDice::addDiceToSet(Die die)
{
vectorOfDice.push_back(die);
}

void SetOfDice::operator += (Die die)
{
addDiceToSet(die);
}


void SetOfDice::roll()
{
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
vectorOfDice[i].roll();
}
}


void SetOfDice::conditionalReRoll()
{
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
vectorOfDice[i].conditionalReRoll();
}
}


int SetOfDice::getValue()
{
int result=0;
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
result+=vectorOfDice[i].getValue();
}
return result;
}
Last edited on
closed account (3TXyhbRD)
Use code tags and indent the source code, thanks.
Looks ok to me.

You may want to add const to the getValue() functions.
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

And perhaps change value=1+(rand()%numberOfSides);
To something like value = rand() / ( double( RAND_MAX +1 ) ) * numberOfSides ) ;
http://www.cplusplus.com/forum/general/63907/#msg346182
Topic archived. No new replies allowed.