2012年2月21日 星期二

Exercise #8


This assignment will give you practice writing constructors and destructors, static data members, static member functions, friend functions, and overloaded operator functions. The purpose of the program is to write a simple game involving dice. You are to create the three classes described below.  Use the main() provided. The sample output should give you additional information about the program requirements.

Create a Die class with the following specifications:
  • One private data member, an unsigned short, value.
  • A private member functions, roll() that generates a random number and assigns it to value. You can use the expression, rand() % 6 + 1, to generate the random number.
  • Add an overloaded operator < private member function that can be used to compare the value of two Die objects.   
  • Name the class Dice as a friend of the Die class. Remember to forward declare the Dice class.
  • Reminder:  all members of the Die class are private. Access must be controlled through the Dice class.
Create a Dice class with the following specifications:
  • One data member, dice, a five element array of Die.
  • A roll() function that calls the Die roll() to assign values to the dice array. This function should call the following sort() function.
  • A private member function sort() that will sort the dice array.
  • A min() function that returns the minimum of the dice array.
  • A sum() function that returns the sum of the dice array.
  • A max() function that returns the maximum of the dice array.
  • A average() function that returns the average of the dice array, rounded to the nearest integer.
  • A median() function that returns the median of the dice array.
  • A averageOfHighAndLow() function that returns the average of the maximum and minimum values of the dice array. "Round up" this average.. For example, if the high is 6 and the low is 1, your function should return 4 (that's 3.5 rounded up). The min(), sum(), max(), average(), median() and averageOfHighAndLow() functions should return an unsigned short.
  • An overloaded! Operator that prints the value of the 5 dice and the points for the roll.
  • An overloaded+ operator that can be used to determine the point value of the dice array. The points are equal to the average + median + averageOfHighAndLow for the roll of the dice.
Create a Player class with the following specifications:
  • Private data members, char* name and unsigned short score.
  • Two static unsigned short data members, NumberOfPlayers and PlayerNumberWhoseTurnItIs.
  • A constructor and destructor.
  • Two accessor functions, getName() and getScore() to return the name and the score.
  • A roll() function that calls the Dice roll(), prints the value of the dice (hint:  use the ! operator of the Dice class) and returns the points for the roll(hint: use the Dice overloaded operator+).
  • A takeTurn() function that calls the roll() function, adds that turns points to the score and returns the score.
  • A static member function, whoseTurnIsIt(), that returns the PlayerNumberWhoseTurnItIs.
  • A static member function, nextPlayer(), that manages the PlayerNumberWhoseTurnItIs.
  • A static member function, HowManyPlayers() that returns the NumberOfPlayers.
Write one non-class member function, printScores() that prints out the player names and scores as shown the program output.  It should have a Player* argument.

Additional requirements:
  • The game ends when one player scores 100 points.
  • Try to match the program output, except for the random points and scores.
  • Your program should use every function and data member listed.  You should "reuse" a lot of code.
 Use this main():

int main(void) {
  Dice Lucky;
  Player Beatles[4];
  unsigned short PlayerScore = 0;
  printScores(Beatles);
  while (PlayerScore < 100) {
    Player::nextPlayer();
    PlayerScore=Beatles[Player::whoseTurnIsIt()].takeTurn(Lucky);
    printScores(Beatles);
  }
  cout << "The winner is "
       << Beatles[Player::whoseTurnIsIt()].getName() << endl;
  return 0;
}

Here is the program output:

Enter player name => John
Enter player name => Paul
Enter player name => George
Enter player name => Ringo
Scores: John 0   Scores: Paul 0   Scores: George 0   Scores: Ringo 0
-------------------------------------------------------------------------
John, you rolled 1 3 5 5 6  - that's 13 points
Scores: John 13   Scores: Paul 0   Scores: George 0   Scores: Ringo 0 
-------------------------------------------------------------------------
Paul, you rolled 1 4 5 5 5  - that's 12 points
Scores: John 13   Scores: Paul 12   Scores: George 0   Scores: Ringo 0 
-------------------------------------------------------------------------
George, you rolled 1 2 3 4 4  - that's 9 points
Scores: John 13   Scores: Paul 12   Scores: George 9   Scores: Ringo 0 
-------------------------------------------------------------------------
Ringo, you rolled 1 2 2 4 5  - that's 8 points
Scores: John 13   Scores: Paul 12   Scores: George 9   Scores: Ringo 8 
-------------------------------------------------------------------------
John, you rolled 1 2 3 3 4  - that's 9 points
Scores: John 22   Scores: Paul 12   Scores: George 9   Scores: Ringo 8 
-------------------------------------------------------------------------
Paul, you rolled 2 3 4 6 6  - that's 12 points
Scores: John 22   Scores: Paul 24   Scores: George 9   Scores: Ringo 8 
-------------------------------------------------------------------------
George, you rolled 2 3 3 4 6  - that's 11 points
Scores: John 22   Scores: Paul 24   Scores: George 20   Scores: Ringo 8 
-------------------------------------------------------------------------
Ringo, you rolled 1 2 3 5 6  - that's 10 points
Scores: John 22   Scores: Paul 24   Scores: George 20   Scores: Ringo 18 
-------------------------------------------------------------------------
John, you rolled 1 2 3 3 4  - that's 9 points
Scores: John 31   Scores: Paul 24   Scores: George 20   Scores: Ringo 18 
-------------------------------------------------------------------------
Paul, you rolled 2 2 3 3 3  - that's 9 points
Scores: John 31   Scores: Paul 33   Scores: George 20   Scores: Ringo 18 
-------------------------------------------------------------------------
George, you rolled 2 3 3 6 6  - that's 11 points
Scores: John 31   Scores: Paul 33   Scores: George 31   Scores: Ringo 18 
-------------------------------------------------------------------------
Ringo, you rolled 1 2 4 6 6  - that's 12 points
Scores: John 31   Scores: Paul 33   Scores: George 31   Scores: Ringo 30 
-------------------------------------------------------------------------
John, you rolled 1 2 3 4 5  - that's 9 points
Scores: John 40   Scores: Paul 33   Scores: George 31   Scores: Ringo 30 
-------------------------------------------------------------------------
Paul, you rolled 1 2 3 4 5  - that's 9 points
Scores: John 40   Scores: Paul 42   Scores: George 31   Scores: Ringo 30 




George, you rolled 1 2 2 4 6  - that's 9 points
Scores: John 98   Scores: Paul 99   Scores: George 91   Scores: Ringo 87 
-------------------------------------------------------------------------
Ringo, you rolled 1 2 4 4 6  - that's 11 points
Scores: John 98   Scores: Paul 99   Scores: George 91   Scores: Ringo 98 
-------------------------------------------------------------------------
John, you rolled 2 2 3 4 5  - that's 10 points
Scores: John 108   Scores: Paul 99   Scores: George 91   Scores: Ringo 98 
-------------------------------------------------------------------------
The winner is John


沒有留言:

張貼留言