dice game

Write a C++ program to implement a three-player dice game with the following rules:
1. Begin with Player A and roll two dice: dice d1 and dice d2.
2. If the sum of the two dice is odd, then accumulate it as the score of Player A. If even, then the
score is 0.
3. Then roll d1 and d2 for Player B.
4. If the sum of the two dice is odd, then accumulate it as the score of Player B. If even, then the
score is 0.
5. Then roll d1 and d2 for Player C.
6. If the sum of the two dice is odd, then accumulate it as the score of Player C. If even, then the
score is 0.
7. Repeat steps 1 to 6 for n rounds, where n is a positive integer provided as input by the user.

Things to keep in mind:
1. You have to implement three classes, namely Player class, Dice class, and Game class, and a driver program test class that contains the main function.

2. Members of Player class are:
i) Instance variables: id, name, and score.
ii) Parameterized constructor with arguments as id and name.
iii) Method: void updateScore() with argument d1 and d2. This method sums the score for the
player.
iv) Method: String toString() with no argument. This method prints out the player’s name and
score value in each iteration.

3. Member of Dice class is:
i) Method: int roll() with no argument. This method rolls the dice and returns a random
number between 1 and 6 inclusive.

4. Members of Game class are:
i) Instance variables: playerA, playerB, playerC, d1, and d2.
ii) Default constructor which initializes the id and name of each player as well as creates new
objects for dice, i.e. d1 and d2.
iii) Method: Player winner() with arguments Dice1 and Dice2. This method sums the score for
the player.
iv) Method: void play() with no argument. This method contains the loop in which the dice is
rolled and score is recorded for each player.

5. Function in the driver program is:
i) Method: int main(). It is the main method in which an object of class Game is created and
the method play() is invoked
Write a C++ program

And?
can you help to write the code?
What have you written so far?

Your instructions are a very clear skeleton:
Player class, Dice class, and Game class

followed by the member functions for each one.

Write those member functions, testing each one individually as you go.
can not understand why there are two similar functions: void updateScore with d1, d2 and playerWinner with D1,D2
Last edited on
No idea, but that's hardly going to stop you starting the coding, is it?
you are misreading it, or, winner may be poorly described or flat out wrong.
I believe updatescore is just this:
void classname updatescore(die d1, die d2)
{
this->score = d1.value + d2.value; //whatever the names of all this stuff is
//'this' is not necessary, but I needed you to see it was a class variable.
}

game class and player class are different. depending on any inheritance or whatever you may be able to use one for the other. Not sure. Anyway, the reason you have 2 of them is because its 2 classes, how they interact, you need to figure out by going thru the requirements. I would think game's would check multiple players' scores and return who won. But I didn't read that, it was a common sense guess. To me it looks like 'winner' is not correctly described in the problem.
Last edited on
Topic archived. No new replies allowed.