Pregunta

I'm a c++ beginner, I need to create a dice game simulate rolling two dices. I'm quite confused with using the header file. But first of all, why do I need to return the no.of face of the dice? Second of all, what does the int roll function do? To reset the values and faces ? If so, what is the default value? And the last function Dice(int n), do I use this function to control the maximum sum of dice values? The function must have a class header file with these functions:

class Dice{

private:
    int face;    // no. of faces of the dice
    int value;    // the face-value that shows up

public:
    int getFace()// returns the no. of face of the dice
    {
    };

    int getVal()
    {
        int dice1;
        int dice2;
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
    };    // returns the face value that shows up

    int roll();     // simulate the roll pf the dice, the  value field is set  and  returned.
    Dice();   // default constructor , a standard six-face dice is created with value = 1
    Dice(int size);  // create a dice of given size

};
¿Fue útil?

Solución

Hopefully this answers your questions in order:

The only reason I can see for returning the number of faces of each dice is to inform the user which dice is currently being rolled. I've shown an example of this in my code below where I have dOne.getFaces() and dTwo.getFaces().

The int roll() function and getVal() were supposed to be the same thing I'm assuming. I've gone ahead and removed getVal() and just used roll() instead.

Dice() and Dice(int size) are just initializing the number of faces for each dice. A default dice will have 6 faces, but the user can roll a dice with more than 6, hence the int size.

#include <iostream>
#include <cstdlib>
#include <time.h>

class Dice
{
    private:
        int faces; // no. of faces of the dice
        int value; // the face-value that shows up
    public:
        int getFaces() {return faces;} // returns the no. of faces of the dice
        int roll() // returns the face value that shows up
        {
            value = rand() % faces + 1;
            return value;
        }
        Dice() : faces(6) {}; // default constructor, create a dice of standard size
        Dice(int size) : faces(size) {}; // create a dice of given size
};

int main()
{
    srand( time(NULL) ); // Initialize random seed
    char yesNo;

    std::cout << "\nWould you like to roll two normal dice? (y/n)\n";
    std::cin >> yesNo;

    if ( yesNo == 'y' )
    {
        Dice dOne, dTwo;

        std::cout << "\nA dice with " << dOne.getFaces() << " faces rolled: " << dOne.roll() << '\n';
        std::cout << "A dice with " << dTwo.getFaces() << " faces rolled: " << dTwo.roll() << '\n';
    }
    else
    {
        int dFaces;
        std::cout << "\nHow many faces would you like each dice to have?\n\n";

        std::cout << "Dice 1: ";
        std::cin >> dFaces;
        Dice dOne(dFaces);

        std::cout << "Dice 2: ";
        std::cin >> dFaces;
        Dice dTwo(dFaces);

        std::cout << "\nA dice with " << dOne.getFaces() << " faces rolled: " << dOne.roll() << '\n';
        std::cout << "A dice with " << dTwo.getFaces() << " faces rolled: " << dTwo.roll() << '\n';
    }

    return 0;
}

Otros consejos

You need to define a constructor. What a constructor does is set up the state of a class, so in this case it's setting up the information about the dice.

If you are to put them directly in the header then the format for a constructor looks like this:

//file: dice.h
//this is default constructor, note the lack of parameters being passed in
Dice(): face(?), value(?) // here we are initializing "face" and "value", replace the question marks with the correct values as per your specification
{
}

If you are doing this in the cpp file it's a little different but the logic should remain the same.

//file: dice.h
Dice(); //This is only a declaration

//file: dice.cpp
#include "dice.h"
Dice::Dice(): face(?), value(?) //This is where we define the constructor. note the Dice:: part here, we need to tell the compiler where to look
{
}

The rest of it is quite similar. If you are struggling then I would suggest you study some c++ resources further. I would looks up constructors/destructors and also initialization lists.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top