【发布时间】: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函数时,这就是您的应用程序中应该发生的事情。Dice和Dice(int size)是constructors。去google这个词,应该可以帮助你理解! -
你试过读一本关于 c++ 的书吗?
-
问题不在于你是否是 C++ 初学者。除非您展示所有这些功能是如何定义的,否则没有人可以回答您的问题。你在做课堂作业吗?你的导师给你这个标题了吗?
-
我不知道如何启动头文件中包含的函数并在cpp文件中使用它们,我想通了,谢谢。