Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions GameDie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
GameDie::GameDie()
{
srand(time(NULL));
counter.resize(FACES);
roll_counter.resize(FACES);

for(int i=0; i<FACES; i++)
counter[i] = 0;
roll_counter[i] = 0;
}

//overloaded constructor
GameDie::GameDie(unsigned int num)
{
if( num == 0 )
{
counter.resize(FACES);
roll_counter.resize(FACES);
}
else{
counter.resize(num);
roll_counter.resize(num);
}
for(int i=0; i<FACES; i++)
{
counter[i] = 0;
roll_counter[i] = 0;
}

}
Expand All @@ -34,13 +34,22 @@ GameDie::GameDie(unsigned int num)
// (inclusive) and return it
int GameDie::roll()
{
int roll = rand() % counter.size();
counter[roll]++;
int roll = rand() % roll_counter.size();
roll_counter[roll]++;
return roll + 1;
}

// return the count of how many times each face has been rolled, as a vector
// where each face's count is at index face-1 (i.e. Face 1 is at index 0)
vector <int> GameDie::get_distribution(){
return counter;
return roll_counter;
}

vector <float> GameDie::get_percentages(){
vector<float> p;
for(int i=0;i<counter.size();i++){
float val = float(counter[i])/float(counter.size());
p.push_back(val);
}
return p;
}
3 changes: 2 additions & 1 deletion GameDie.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class GameDie
GameDie(unsigned int);
int roll();
vector <int> get_distribution();
vector <float> get_percentages();

private:
vector <int> counter;
vector <int> roll_counter;
const static int FACES = 6;
};

Expand Down