// Assignment 7 Solution #include #include #include using namespace std; const int GamePoints = 100; const int SKUNK = -1; const int NumberOfPlayers = 3; class Dice { int die1, die2; public: Dice() : die1(0), die2(0) {} int roll(); }; class Player { string name; int rollsPerTurn; int points; public: Player(const string& name, int rpt); string getName() const { return name; } int getPoints() const { return points; } int takeTurn(Dice&); bool declareWinner() const; }; bool Player::declareWinner() const { cout << endl << name << " won the game with " << points << " points." << endl; return true; } Player::Player(const string& n, int rpt) : name(n), rollsPerTurn(rpt), points(0) { } int main() { srand(111); Player player[3] = {{"Curly",1},{"larry",2},{"Moe",4}}; Dice dice; bool gameOver = false; int playerNum = 0; while (!gameOver) { if (player[playerNum].takeTurn(dice) >= 100) break; if (playerNum == 2) cout << "---------------------------------------------\n"; playerNum = playerNum < 2 ? playerNum + 1 : 0; } player[playerNum].declareWinner(); cout << "That's all folks!!!" << endl; } int Dice::roll() { int sum; die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; if (die1 == 1 and die2 == 1) sum = SKUNK; else if (die1 == 1 or die2 == 1) sum = 0; else sum = die1 + die2; cout << " You rolled " << die1 << " and " << die2 << ". That's "; if (sum < 0) cout << " a SKUNK!!!" << endl; else cout << sum << endl; return sum; } // returns number of points for turn int Player::takeTurn(Dice& dice) { int pointsForTurn = 0; int pointsForRoll; cout << name << ", it is your turn\n"; for (int i = 1; i <= rollsPerTurn; i++) { pointsForRoll = dice.roll(); if (pointsForRoll == SKUNK) { pointsForTurn = 0; points = 0; break; } else if (pointsForRoll == 0) { pointsForTurn = 0; break; } else { pointsForTurn += pointsForRoll; if (points + pointsForTurn >= GamePoints) break; } } points += pointsForTurn; cout << " Points for the turn = " << pointsForTurn << ". Total points = " << points << endl << endl; return points; }