Having decided to brush up on my programming, I wanted to try and make a Yahtzee game, only console based. It is object-oriented for the most part.
My trouble is with generating different numbers for each die. I will post my Dice class to help show what I am doing:
class Dice {
public:
Dice();
int roll();
int getFaceValue() const;
private:
void rollDie();
int faceValue;
bool hold;
};
and the rest
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "Dice.h"
Dice::Dice() {
rollDie();
srand( time(0) );
hold = true;
}
int Dice::roll() {
rollDie();
return faceValue;
}
int Dice::getFaceValue() const {
return faceValue;
}
void Dice::rollDie() {
if ( !hold ) {
faceValue = (1 + rand() % 6);
}
}
Just to test it I run this code:
Dice dice[5];
int i = 0;
while ( i < 20 ){
rollTheDice(dice);
cout << endl;
i++;
}
All "rollTheDice(dice)" does is call the "roll()" function for each object in the array. To simulate someone rolling dice.
I get the same values each iteration of the loop, I suspect this is because each Dice object is created with the same code, so the numbers generated are the same.
What I am looking for is the way to generate different values for each Dice object each time.