You are writing some simple code to simulate rolling a die. The possible outcome
ID: 3865910 • Letter: Y
Question
You are writing some simple code to simulate rolling a die. The possible outcomes are the values 1 to 6.
a) 6 points
The Die class has these public behaviors:
- construct a die object. Takes no parameters.
- roll: rolls the die and returns the outcome. Outcomes 1 to 6 have an equal chance of occurring.
- getRollCount: takes a value in the range 1 to 6 and returns how many times the die outcome was equal to that value.
- getTotalRolls: returns how many times the die was rolled.
Please give the interface and implementation of the Die class.
b) 7 points
You would like to simulate a loaded die. A loaded die has all the behaviors of a fair die but there is a difference. A loaded die has one preferred value which is not totally fair. All the values in the range 1 to 6 can occur by chance, but the preferred value is guaranteed to be the outcome whenever the previous roll's outcome was "one less".
E.g.: if the preferred value is 2, the die outcome will always be a 2 after a 1 roll. If the preferred value is 5, there will always be a 5 after a 4. If the preferred value is 1, there will always be a 1 after a 6. Here is a possible sequence of outcomes for a loaded die with a preference for 6: 3, 1, 1, 5, 6, 4, 5, 6, 2, 6, 3. The 6 outcomes after the 5 outcomes were guaranteed.
The preferred value is specified in the constructor of the class.
Please give the interface and implementation of the LoadedDie class. You should not have unnecessarily duplicate code.
c) 4 points
Please implement a function named rollDice which takes a vector of loaded dice and a positive integer count and rolls each die count times. The function returns nothing. Call your function from the main.
d) 3 points
Please implement a function named rollDie which takes a pointer to a die, rolls it, and returns its outcome. The function should work as expected for both fair and loaded dice.
Explanation / Answer
#include<iostream>
#include<vector>
#include <ctime>
#include <cstdlib>
using namespace std;
class Die{
protected:
vector<int> outcome;
public:
virtual int roll(){
int val;
val = rand() % 6 + 1;
outcome.push_back(val);
return val;
}
int getRollCount(int a){
int count;
count = 0;
for (int i =0; i<outcome.size(); i++){
if (outcome[i] == a)
count++;
}
return count;
}
int getTotalRolls(){
return outcome.size();
}
};
class LoadedDie : public Die {
private:
int preferred_value;
public:
LoadedDie(int a){
preferred_value = a;
}
int roll(){
if (outcome.size() > 0) {
if (preferred_value == 1){
if (outcome[outcome.size()-1] == 6){
outcome.push_back(1);
return 1;
}
}
if ((preferred_value - outcome[outcome.size()-1]) == 1){
outcome.push_back(preferred_value);
return preferred_value;
}
else {
return Die::roll();
}
}
else {
return Die::roll();
}
}
int getPreferredValue(){
return preferred_value;
}
};
void rollDice(vector<LoadedDie> &a, int count){
for (int i = 0; i<a.size(); i++){
cout << "Loadede Die " << i << ":" << a[i].getPreferredValue() << ":";
for (int j=0; j<count; j++){
cout << a[i].roll() << " ";
}
cout << endl;
}
}
int rollDie(Die *a){
return a->roll();
}
int main(){
Die d;
LoadedDie ld(4);
Die *ptr;
vector<LoadedDie> l;
srand((unsigned)time(0));
for (int i = 0; i<10; i++){
LoadedDie ld1(rand()%6 + 1);
l.push_back(ld1);
}
for (int i = 0; i<10; i++)
d.roll();
cout << "GetRollCount:" << d.getRollCount(4) << endl;
cout << "TotalRollCount:" << d.getTotalRolls() << endl;
rollDice(l,8);
ptr = &d;
cout << "Rolldie output(Die object):" << rollDie(ptr) << endl;
ptr = &ld;
cout << "Rolldie output(LoadedDie object):" << rollDie(ptr) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.