Dice Game: Logic and Classes

So, I have been working on this homework assignment on and off now trying to figure out how to properly structure it so that it works. Unfortunately, I am having trouble get the proper results. I have the assignment criteria here:

For this assignment, you will create a dice game. Your program will have the player pitted against the computer. Both the player and the computer get to roll the dice 6 times each and the user with the highest overall dice total will win the game.

The main purpose of this program is to use classes and objects as learned in chapter 6. In this program you must create a dice class which you will create 2 objects of the dice class to represent the 2 dice in this game. In this dice class you must store as private data, the face value of the die. You must also have member functions that roll the die (this will get a random number that will represent the die face value.) and print the die onto the screen for the user to see.

In main() you should create 2 dice objects and call the proper roll dice functions and print functions for the game to work. There will be six turns so you should have some sort of a loop in your program and you will need to keep track of the player and computers total scores as the game is played.

Remember that you must add comment blocks, inline comments and format your code as described in the first program handout to get full credit on this assignment. Do not use global variables or GOTO statements or you will get a 0 on this assignment.

Sample Output:

Welcome to the C++ dice game!

You are playing against the computer. Each player gets to roll 2 dice a total
of 6 times. The player with the highest total of dice values wins!

Are you ready to roll?
Press any key to continue . . .
- - - - -
| O |
| |
| O |
- - - - -

- - - - -
| O O |
| |
| O O |
- - - - -

Now the computer's turn.
Press any key to continue . . .
- - - - -
| O |
| O |
| O |
- - - - -
- - - - -
| |
| O |
| |
- - - - -

So far you are winning!

The score is... You: 6, Computer: 4
Press any key to continue . . .

Here is my code:
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

class Dice {
private:
int total_Rolls;
int total_Score;
int die;
public:
Dice();
void dice_Faces1();
void dice_Faces2();
int random_Number_Gen() {return rand() % 6 + 1;}
void Rolls();
void Display();
};

Dice::Dice() : die(0), total_Rolls(0), total_Score(0)
{}

void Dice::Rolls()
{
die = 0;
total_Rolls++;

die = random_Number_Gen();

total_Score += die;
}

void Dice::Display()
{
cout << "Last roll was: " << die << endl;
cout << "Total amount of Rolls: " << total_Rolls << endl;
cout << "Total score is: " << total_Score << endl;
}

void Dice::dice_Faces1()
{
switch (die)
{
case 1:
cout << "- - - - -" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << " - - - - -" << endl;

break;

case 2:
cout << "- - - - -" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << " - - - - -" << endl;

break;

case 3:
cout << "- - - - -" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << " - - - - -" << endl;

break;

case 4:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;

case 5:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| O |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;

case 6:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| O O |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;
}

}

void Dice::dice_Faces2()
{
switch (die)
{
case 1:
cout << "- - - - -" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << " - - - - -" << endl;

break;

case 2:
cout << "- - - - -" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << " - - - - -" << endl;

break;

case 3:
cout << "- - - - -" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << " - - - - -" << endl;

break;

case 4:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;

case 5:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| O |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;

case 6:
cout << "- - - - -" << endl;
cout << "| O O |" << endl;
cout << "| O O |" << endl;
cout << "| O O |" << endl;
cout << " - - - - -" << endl;

break;
}

}

int main()
{
srand(time(0));
Dice user, computer;

for (int i = 0; i < 5; i++)
{
user.Rolls();
user.Display();

user.dice_Faces1();
user.dice_Faces2();


computer.dice_Faces1();
computer.dice_Faces2();

computer.Rolls();
computer.Display();


cin.get();
}



return 0;
}


Please tell me everything that is wrong with my code! Thanks in advance!
Last edited on
upper ascii has box drawing characters that would look better.
And, you should be able to make a function that draws the die faces depending on the value you want to show. It would look a lot better on screen and cut the code size and mess way down to do this.

There is a newer way to do random numbers, and a ton of threads on it here.

its <ctime>

time.h is C not C++

There is a concern about namespace std and not using it, more threads you can read on that here. Most prefer std::everything.

It generally looks OK if it works. I didn't try to run it. These items I gave are cleanup and modernization, not critical to it working.

Thanks! I figured it out and trimmed it down a bit!
Topic archived. No new replies allowed.