【问题标题】:c++ dice game simulate rolling two dicesc++骰子游戏模拟滚动两个骰子
【发布时间】:2014-03-16 06:20:50
【问题描述】:

我是 C++ 初学者,我需要创建一个骰子游戏来模拟掷两个骰子。我对使用头文件感到很困惑。但首先,为什么我需要返回骰子的面数?其次,int roll函数有什么作用?重置价值观和面孔?如果是这样,默认值是多少?还有最后一个函数Dice(int n),我是不是用这个函数来控制骰子值的最大总和?该函数必须有一个包含这些函数的类头文件:

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

};

【问题讨论】:

  • 你应该拿两个真正的骰子,掷一下,看看会发生什么。当您调用 roll 函数时,这就是您的应用程序中应该发生的事情。 DiceDice(int size)constructors。去google这个词,应该可以帮助你理解!
  • 你试过读一本关于 c++ 的书吗?
  • 问题不在于你是否是 C++ 初学者。除非您展示所有这些功能是如何定义的,否则没有人可以回答您的问题。你在做课堂作业吗?你的导师给你这个标题了吗?
  • 我不知道如何启动头文件中包含的函数并在cpp文件中使用它们,我想通了,谢谢。

标签: c++ arrays class dice


【解决方案1】:

希望这可以按顺序回答您的问题:

我能看到返回每个骰子的面数的唯一原因是通知用户当前正在滚动哪个骰子。我在下面的代码中展示了一个示例,其中有 dOne.getFaces() 和 dTwo.getFaces()。

int roll() 函数和 getVal() 应该是我假设的同一件事。我已经继续并删除了 getVal() 而只是使用了 roll() 。

Dice() 和 Dice(int size) 只是初始化每个骰子的面数。默认骰子有 6 个面,但用户可以掷出超过 6 个面的骰子,因此是 int 大小。

#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;
}

【讨论】:

    【解决方案2】:

    你需要定义一个构造函数。构造函数所做的是设置类的状态,因此在这种情况下,它是设置有关骰子的信息。

    如果您要将它们直接放在标题中,那么构造函数的格式如下所示:

    //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
    {
    }
    

    如果您在 cpp 文件中执行此操作,它会有点不同,但逻辑应该保持不变。

    //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
    {
    }
    

    其余部分非常相似。如果您正在苦苦挣扎,那么我建议您进一步研究一些 c++ 资源。我会查找构造函数/析构函数以及初始化列表。

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 2011-01-19
      • 2013-09-17
      • 2012-02-29
      • 1970-01-01
      • 2018-10-19
      • 2015-08-25
      • 2014-02-12
      • 2016-02-07
      相关资源
      最近更新 更多