[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1656
  • Last Modified:

Dice rolling using 2 classes

This is my first try at object oriented programming and I'm a little turned around.

Trying to use 1 class for dice...and one for die

The general functionality of each class is as follows:

Dice:
"      Two private member variables of class die (and whatever other variables you need)
"      Method for rolling the dice (technically this is a mutator)
"      Accessor for the value of the entire roll (should return -1 if the roll on either die is invalid)
"      Accessor for each die individually

Die:
"      Private member variable to hold the last roll
"      Seed the random number generator and initialize the roll to -1 in the constructor
"      Accessor to get the last roll
"      Method for rolling the die (technically a mutator)

For testing purposes your main function should create a Dice variable, roll it and show the results (for each die and for the entire roll) a couple of times.

// Dice
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;


class Dice
(
      private:
            Die die1;
            Die die2;

      public int roll() (
            return die1.roll() + die2.roll();
      )

      Dice::Dice() (
            die1 = new Die();
            die2 = new Die();
      )
);


class Die
(
      private:
            int lastRoll;

      public int roll() (
            lastRoll = (rand() % 6 + 1);
            return lastRoll;
      )
      
      public int getLast() (
            return lastRoll;
      )
);
      Die::Die() (
            // Get system time
            unsigned seed = time(0);
            // Seed random generator
            srand(seed);
            lastRoll = -1;
      )

int main()
{

    Dice Craps; // define object
    cout << "Dice 1: " << Craps.die1 << endl;
    cout << "Dice 2: " << Craps.die2 << endl;    
      cout << "Total is: " << Craps.roll() << endl;
}
Is what I have so far..my question is not sure if I am accessing the die class correctly. Any help understanding would me most appreciated.
0
IrishJames
Asked:
IrishJames
  • 2
  • 2
1 Solution
 
Frosty555Commented:
You have declared die1 and die2 as "private" members of the "Dice" class. This means they will not be directly accessible from your main() method. Besides that, they're objects. You'll be outputting the memory address they are located at, not the value they contain.

Since you've encapsulated the two die in the "Dice" class, the user (e.g. the main method) should never need to even know the Die class exists. All calls should be made to public functions or properties of the Dice class, and the dice class should encapsulate the two Die objects.

My suggestion:
     1) Make the Die.Roll() and Dice.Roll() function a void. They do things. They don't return anything.
     2) Add a function Die.GetValue() that returns the last rolled value
     3) Add a function Dice.GetValue() that returns the sum of die1.GetValue() and die2.GetValue()
     4) Optionally, add function Dice.GetValue1() and Dice.GetValue2() to return the individual
          values of the two die (e.g they return die1.GetValue() and die2.GetValue())

Then you use it in your main method by going
         Dice Craps = new Dice();
         Craps.roll();
         cout << "Dice 1: " << Craps.GetValue1() << endl;
         cout << "Dice 2: " << Craps.GetValue2() << endl;    
         cout << "Total is: " << Craps.GetValue() << endl;          

0
 
Infinity08Commented:
>>          Dice Craps = new Dice();

That won't work ... This is not Java heh. For dynamically allocated memory, you need a pointer, not an object instance.


Anyway, I see that you haven't tried compiling this, since there are several syntax and other errors in the code. My suggestion would be to try to compile the code, and then fix it one error at a time.


Here are a few problems with the current code :

>> class Dice
>> (

You mixed up ()'s with {}'s.


>>             Die die1;

the Die class is not defined at this point. You need to define it before the Dice class.


>>       public int roll() (

That's not valid in C++. You need to use 'public :' just like you did for 'private'.
Note that you also mixed up ()'s with {}'s.


>>       Dice::Dice() (

You only use the scope (Dice::) when implementing the method outside of the class.


>>             die1 = new Die();

This won't work, since die1 is not a pointer.




Because there are a lot of basic C++ errors, I would suggest to read up a bit before continuing. Here are some basic tutorials to get you started :

        http://www.cplusplus.com/doc/tutorial/

Take special note of the tutorial about classes.
0
 
Frosty555Commented:
>>>          Dice Craps = new Dice();
>That won't work ... This is not Java heh. For dynamically allocated memory, you need a pointer, not an >object instance.

Forgot about that, heh. I do live in the Java and C# world a little too much for this zone ;)
0
 
IrishJamesAuthor Commented:
Ok..thank you gentlemen.
After a long delay with the flu(I do not recommend it) I have changed my work to what I have listed below.

class Die
{
   private:
           
           int numbeRolled;
   public:
           
            Die(); //This is my constructor
            void Roll(); // Method to throw the die
            int LastRoll(); //Accessor to look at the die
};
Die::Die()
{
          numbeRolled = -1;
          srand(time(0));
}
void Die::Roll()
{
     numbeRolled = (rand()%6 + 1);
}
int Die::LastRoll()
{
    return numbeRolled;
}

class Dice
{
      private:
            Die die1;
            Die die2;
      public:
          void Roll();          
          int TotRoll();
          int GetDie1();
          int GetDie2();      
};
 
void Dice::Roll()
          {
            die1.Roll();
            die2.Roll();  
          }
int Dice::TotRoll()
          {
           if ((die1.LastRoll() < 1)||(die2.LastRoll() < 1))
              {
               return -1;
              }
             
               return (die1.LastRoll() + die2.LastRoll());
            }
int Dice::GetDie1()
          {
           return die1.LastRoll();
          }
int Dice::GetDie2()
          {
           return die2.LastRoll();
          }
         

int main()
{
         
    Dice Play();
    Play.Roll();
     
         cout << "Dice 1: " << Play.GetDie1() << endl;
         cout << "Dice 2: " << Play.GetDie2() << endl;    
         cout << "Total is: " << Play.TotRoll() << endl;
         
     system ("PAUSE");
     return 0;
}

I get an error for non-class type. Have I called me object wrong?
0
 
Infinity08Commented:
>>     Dice Play();

This actually declares a function called 'Play' that takes no parameters, and returns a Dice object.
Do this instead :

        Dice Play;

or :

        Dice Play = Dice();



One more thing : you placed the srand(time(0)) in the Die constructor. This means that the random number generator will be re-seeded every time you create a new instance of the Die class. You should only seed the random number generator once - before the first call to rand().
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now