Please send questions to
st10@humboldt.edu .
#include <string>
#include <iostream>
#include "Dice.h"
using namespace std;
/*--------------------------------------------------
Contract: main : void -> int
Purpose: perform unit tests for class Dice
(basic tests of its constructor(s),
accessors, mutators, and any other
member functions) and permit further
tests of member function roll to be run as
desired (until the user types in n to say
they want to do no further testing), prompting
the user for how many sides the die should
have and how many times it should be rolled for
each additional test.
Examples: not really applicable --- this will
perform and display to the screen the
results of tests of Dice's constructors,
accessors, and mutators, and will then
show the rolls of dice with a user-specified
number of sides for a user-specified number
of rolls (until they type n to stop).
Compile using:
g++ -o testDice testDice.cpp Dice.o randgen.o
-----------------------------------------------------*/
int main()
{
// some local variables
string answer;
int desiredNumSides;
int desiredNumRolls;
// testing both of Dice's constructors
Dice regular;
Dice fancier(12);
Dice testDie;
// test Dice's accessor functions (and confirms
// constructors, too, note!)
cout << "6-sided-die #sides: " << regular.get_numSides()
<< endl;
cout << "6-sided-die #rolls so far: "
<< regular.get_numRolls() << endl;
cout << "12-sided-die #sides: " << fancier.get_numSides()
<< endl;
cout << "12-sided-die #rolls so far: "
<< fancier.get_numRolls() << endl;
// test Dice's mutator function
fancier.set_numSides(15);
cout << "changed 12-sided die to one with 15 --" << endl;
cout << " and its numSides IS now: "
<< fancier.get_numSides() << endl;
// roll needs special individual testing
Dice myDie(3);
cout << "3-sided die roll should return 1, 2, or 3: "
<< endl << "roll is: " << myDie.roll() << endl;
cout << "6-sided die roll should return value in [1, 6]:"
<< endl << "roll is: " << regular.roll() << endl;
cout << "15-sided die roll should return val in [1,15]:"
<< endl << "roll is: " << fancier.roll() << endl;
// is numRolls getting properly updated:
cout << "return 1 if 6-sided die has been rolled once: "
<< (regular.get_numRolls() == 1) << endl;
// give user a chance to test roll further
// (and give us an excuse to use a for loop! 8-) )
cout << endl;
cout << "Would you like to test roll further?" << endl;
cout << "(type n for no, anything else for yes)" << endl;
cout << endl;
cout << "your answer: ";
cin >> answer;
while (answer != "n")
{
// ask user how many sides the test die
// should have, and how many rolls they
// want to see
cout << "how many sides should die have?: ";
cin >> desiredNumSides;
cout << "how many times shall die be rolled?: ";
cin >> desiredNumRolls;
// roll such a die that many times!
testDie = Dice(desiredNumSides);
for (int i=0; i < desiredNumRolls; i++)
{
cout << "roll #" << i+1 << ": "
<< testDie.roll() << endl;
}
cout << "was die rolled " << desiredNumRolls
<< " times?" << endl;
cout << " num rolls: " << testDie.get_numRolls()
<< endl;
cout << "Would you like to test roll further?"
<< endl;
cout << "(type n for no, anything else for yes)"
<< endl;
cout << endl;
cout << "your answer: ";
cin >> answer;
}
return 0;
}