如何创建一个函数,在C++中生成3个骰子卷
我需要创建一个生成3个掷骰子的函数,实际上我有一段生成3个掷骰子的代码,但我需要从一个函数调用它。这是我的密码:如何创建一个函数,在C++中生成3个骰子卷,c++,C++,我需要创建一个生成3个掷骰子的函数,实际上我有一段生成3个掷骰子的代码,但我需要从一个函数调用它。这是我的密码: #include <iostream> #include <string> #include <time.h> using namespace std; int cash = 90000; int main() { int wager; int r; // dealer's die int dealer1;
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int cash = 90000;
int main()
{
int wager;
int r;
// dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
//your money
cout << "Wager up boy!" << endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
srand(time(NULL));
dealer1 = rand() % 6 + 1;
dealer2 = rand() % 6 + 1;
dealer3 = rand() % 6 + 1;
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
mdice1 = rand() % 6 + 1;
mdice2 = rand() % 6 + 1;
mdice3 = rand() % 6 + 1;
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system("pause");
}
这里有一个提示:您已经这样做了,因为main是一个函数。Main是一个返回整数的函数,该整数不包含在codereturn 0;中;。使用所有代码的void函数将起作用
void diceGame()
{
everything you have in main
}
int main()
{
diceGame();
system("pause");
return 0;
}
或者:
void diceGame(); //prototype
int main()
{
diceGame();
system("pause");
return 0;
}
void diceGame()
{
everything you have in main
}
你的要求不是很清楚!假设你想要一个函数中的掷骰子部分,写一个这样的函数
int dice = roll_die();
然后像这样调用这个函数
int dice = roll_die();
如果需要返回多个值的函数,请使用引用参数
void roll_3_dice(int &dice1, int &dice2, int &dice3) {
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
那么你会这样称呼它:
roll_3_dice(dealer1, dealer2, dealer3);
cout << "You rolled the following: " << endl;
cout << player.roll1 << "-" << player.roll2 << "-" << player.roll3 << endl;
Tanuj Yadav有一个正确的想法,你寻找最小的代码重复部分,然后把它封装成一个函数。e、 g.基本重复单元有效地
int dice = rand () % 6 + 1
所以你可以做一个函数。您需要返回一个int,并且希望用法如下所示
int dice = roll_die();
要创建函数,基本模式是:
return_type name(parameter_type parameter_name, etc)
所以您的返回类型是int,名称是roll\u die,参数是可选的。我将使用边数作为参数
int roll_die()
{
return rand () % 6 + 1;
}
或
^这一个假设边是6,如果你没有另外指定,但可以用于任何边骰子,而不需要新的代码
这保持了每个程序只重复相同代码一次的概念。您可以创建一个滚动多个骰子的函数,但它应该调用滚动骰子函数本身,而不是重复rand%6+1三次。重复是不好的形式。在现实世界的编码中,您可以不受影响,但在测试函数概念时,您不应该懒惰。将任何重复的代码转换为公共函数
下一关是掷三个骰子。您的一些选择包括返回指针、结构或std::vector。其他答案中已经显示了指针,但我强烈建议不要使用这些实现。如果返回使用malloc创建的内存,如果客户端不调用free,可能会导致错误。在创建函数时,不应假定调用代码具有特殊知识。您应该对函数代码本身进行防错
出于这个原因,我建议要么返回一个std::vector,要么返回一个带有三个骰子的自定义结构。e、 g
struct three_dice
{
int roll1, roll2, roll3;
}
或使用阵列的此版本:
struct three_dice
{
int roll[3];
}
然后,函数返回它生成的三个骰子对象:
three_dice roll_three_dice()
{
three_dice temp;
temp.roll1 = roll_die();
temp.roll2 = roll_die();
temp.roll3 = roll_die();
return temp;
}
或者与阵列版本类似:
three_dice roll_three_dice()
{
three_dice temp;
temp.roll[0] = roll_die();
temp.roll[1] = roll_die();
temp.roll[2] = roll_die();
return temp;
}
然后,不制作单独的dice1、dice2、dice3变量,而是创建两个three_dice对象,并让它们从roll_three_dice函数的返回类型复制其值,该函数本身调用roll_die函数三次:
three_dice dealer;
three_dice player;
dealer = roll_three_dice();
player = roll_three_dice();
你可以这样得到这些值:
roll_3_dice(dealer1, dealer2, dealer3);
cout << "You rolled the following: " << endl;
cout << player.roll1 << "-" << player.roll2 << "-" << player.roll3 << endl;
或者,如果使用数组而不是三个名称:
cout << "You rolled the following: " << endl;
cout << player.roll[0] << "-" << player.roll[1] << "-" << player.roll[2] << endl;
这段代码更安全,因为没有malloc,所以没有可能的内存泄漏。那么我们不应该使用函数,bcz所有东西都已经在functionmain中了。不,函数非常有用。我是说他可以把他放在主目录中的所有东西都变成一个函数。Main是一个函数,我想关键是他在主函数中有几个地方可以掷3个骰子。他希望将其移动到一个单独的函数中,这样他就不必每次都重复代码。这就是把东西分解成函数的意义。Barmar说得对,对不起,关于我的代码的结构,我对这个很陌生,所以我只是按照我在网上看到的/我认为正确的,全部和/或部分。我同意。你使用的是C++11编译器吗?我想你会喜欢读这篇文章的:全局变量,真的吗?我对这类东西不熟悉这不是一个好的解决方案。没有必要在这篇文章中引入指针。此外,您没有为掷骰子的核心概念创建函数。为什么相同的掷骰子重复6次?为什么玩家骰子和庄家骰子有不同的功能?如果我们想要一个滚动四个骰子的版本,你会为每个可以滚动的骰子数量的庄家和玩家创建两个新功能吗?投票被否决,因为这只是一个糟糕的编码。你可以做的只是制作一个unifiedroll X骰子函数,为玩家或庄家返回任意数量的骰子。它正在工作,谢谢,伙计,如果我的代码的组成很奇怪,哦,对不起。@elburatski没问题!有没有可能修改它,使功能掷3个骰子,而不是调用3次?@elburatski是的,Barmar的答案是这样做的。在这种情况下,他将传递三个整数作为参考,这意味着在函数中所做的更改也将反映到需要存储骰子结果的函数的实际值上,然后在函数中他将数据分配给它们。@elburatski,但是您将无法将其用于单掷骰子