如何创建一个在C++中生成 3 个掷骰子的函数



我需要创建一个生成 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;
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");
}

你的要求不是很清楚!
假设您希望函数中的掷骰子部分。

编写这样的
函数

int dice_roll()
{
return (rand() % 6 + 1);
}

并像这样
调用此函数

dealer1=dice_roll();

Tanuj Yadav的想法是正确的,你寻找最小的重复代码段,然后将其封装为一个函数。 例如,基本重复单元有效

int dice = rand () % 6 + 1

所以你可以做一个函数。 你需要找回一个"int",并且你希望用法看起来像这样

int dice = roll_die();

要创建一个函数,基本模式是:

return_type name(parameter_type parameter_name, etc)

所以你的返回类型是"int",名称是"roll_die",参数是可选的。我会使用边数作为参数。

int roll_die()
{
return rand () % 6 + 1;
}

int roll_die(int sides = 6)
{
return rand () % sides + 1;
}

^ 如果您不另行指定,则假设边数为 6,但可用于任何边骰子而无需新代码。

这保留了每个程序仅重复一次相同代码的概念。您可以创建一个掷出多个骰子的函数,但它应该调用"roll_die"函数本身,而不是重复"rand () % 6 + 1"三次。重复是不好的形式。你可以在现实世界的编码中侥幸逃脱,但是当你在函数概念上进行测试时,你不应该偷懒。将任何重复的代码转换为通用函数。

下一个级别是"掷三个骰子"。您的一些选择包括返回指针、结构或 std::vector。指针已显示在其他答案中,但我强烈建议不要使用这些实现。如果返回使用 malloc 创建的内存,如果客户端不调用"free",则可能会导致错误。在创建函数时,不应假设调用代码具有特殊知识。您应该对函数代码本身进行错误防护。

出于这个原因,我建议返回一个 std::vector 或带有三个掷骰子的自定义结构。

struct three_dice
{
int roll1, roll2, roll3;
}

或者这个使用数组的版本:

struct three_dice
{
int roll[3];
}

然后,您的函数返回它创建的three_dice对象:

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

然后,您创建两个"three_dice"对象,而不是创建单独的 dice1、dice2、dice3 变量,并让它们从 roll_three_dice 函数的返回类型复制它们的值,该函数本身调用 roll_die 函数三次:

three_dice dealer;
three_dice player;
dealer = roll_three_dice();
player = roll_three_dice();

你可以像这样获取值:

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",因此不会发生可能的内存泄漏。

如果需要返回多个值的函数,请使用引用参数。

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);
#include <iostream>
#include <string>
#include <time.h>
#include<stdlib.h>
using namespace std; 
int* get_dealer_roll()
{
int* dealer = (int*)malloc(3*sizeof(int));
srand (time(NULL));
*(dealer+0) = rand() % 6 + 1;
*(dealer+1) = rand() % 6 + 1;
*(dealer+2) = rand() % 6 + 1;
return dealer;
}
int* get_mdice_roll()
{
int* mdice = (int*)malloc(3*sizeof(int));
srand (time(NULL));
*(mdice+0) = rand() % 6 + 1;
*(mdice+1) = rand() % 6 + 1;
*(mdice+2) = rand() % 6 + 1;
return mdice;
}
int main()
{
int wager; 
int r;

//your money
int cash = 90000;

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;
int* dealer = get_dealer_roll();
cout << "Dealer rolled the following: " << endl;
cout << *(dealer+0) << "-" << *(dealer+1) << "-" << *(dealer+2) << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl; 
cout << "Press any key to roll the dice" << endl;
cin >> r;
int* mdice = get_mdice_roll();
cout << "You rolled the following: " << endl;
cout << *(mdice+0) << "-" << *(mdice+1) << "-" << *(mdice+2) << endl;
system ("pause");
free(mdice);
free(dealer);
}

这里有一个提示:你已经这样做了,因为main()是一个函数。Main 是一个返回整数的函数,该整数不包含在您的代码中(返回 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
}

最新更新