/****************************************************************************** * SnakeEyes * ========= * This program declares two classes: Gambler and Die. * It instatiates a Gambler, who rolls 2 dice 500 times and reports on * the number of SnakeEyes (double ones) the gambler rolled. * * File: SnkaeEyes.cpp */ #include #include #include using namespace std; class Die { public: Die(); // Constructor int roll(); /* Returns new faceValue */ private: int faceValue; }; class Gambler { public: Gambler(); // Constructor int rollTheDice(int MaxRolls); /* Returns Count of # SnakeEyes */ private: int roll, count; Die die1, die2; }; Gambler::Gambler() // Constuctor { roll = 0; count = 0; } int Gambler::rollTheDice(int MaxRolls) { /* Returns Count of # SnakeEyes */ count = 0; for (roll=1;roll<=MaxRolls;roll++) if ((die1.roll()+die2.roll())==2) count++; // SnakeEyes! return count; } Die::Die() // Constructor { srand(time(0)); faceValue = (rand()%6)+1; } int Die::roll() { // Roll the dice and return faceValue! faceValue = (rand()%6)+1; return faceValue; } /* * */ int main(int argc, char** argv) { const int MAX_ROLLS = 500; int snakeEyes = 0; Gambler gambler; snakeEyes = gambler.rollTheDice(MAX_ROLLS); cout << "Rolled " << snakeEyes << " Snake-Eyes in " << MAX_ROLLS << " rolls" << endl;; }