C ++骰子游戏模拟掷两个骰子
c++ dice game simulate rolling two dices
我是 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
};
希望这能按顺序回答您的问题:
我可以看到返回每个骰子的面数的唯一原因是通知用户当前正在掷骰子。 我在下面的代码中展示了一个例子,其中我有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?nn";
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;
}
你需要定义一个构造函数。构造函数的作用是设置类的状态,因此在本例中,它是设置有关骰子的信息。
如果要将它们直接放在标头中,则构造函数的格式如下所示:
//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 ++资源。我会查找构造函数/析构函数以及初始化列表。
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 模拟两个骰子的滚动并保存在矢量中
- 如何将 GMock EXPECT_CALL设置为为模拟函数调用两个不同的函数
- C ++骰子游戏模拟掷两个骰子