我需要创建一个生成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");
}
答案 0 :(得分:1)
您的要求不是很清楚!
假设您想要将骰子卷部件放在一个功能中。
写一个这样的函数
int dice_roll()
{
return (rand() % 6 + 1);
}
并像这样调用此函数
dealer1=dice_roll();
答案 1 :(得分:0)
如果您想要一个返回多个值的函数,请使用参考参数。
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);
答案 2 :(得分:0)
Tanuj Yadav有正确的想法,你寻找最小的重复代码部分,然后你将其封装为一个函数。例如基本的重复单位是有效的
int dice = rand () % 6 + 1
所以你可以做一个功能。你需要得到一个&#34; int&#34;回来了,你希望用法看起来像这样
int dice = roll_die();
制作一个函数,基本模式是:
return_type name(parameter_type parameter_name, etc)
所以你的返回类型是&#34; int&#34;,name是&#34; roll_die&#34;,参数是可选的。我会使用边数作为参数。
int roll_die()
{
return rand () % 6 + 1;
}
或
int roll_die(int sides = 6)
{
return rand () % sides + 1;
}
^如果你没有另外说明,这个假定边是6,但是可以用于任何一面的骰子而不需要新的代码。
这保持了每个程序只重复相同代码一次的概念。你可以创建一个滚动多个骰子的函数,但它应该调用&#34; roll_die&#34;功能本身而不是重复&#34; rand()%6 + 1&#34;三次。重复是不好的形式。你可以在现实世界的编码中使用它,但是当你在功能概念上进行测试时,你不应该懒惰。将任何重复的代码转换为通用函数。
下一个级别是&#34;掷三个骰子&#34;。您的一些选择包括返回指针,结构或std :: vector。指针已在其他答案中显示,但我强烈建议不要使用这些实现。如果你返回使用malloc创建的内存,如果客户端没有调用&#34; free&#34;则会导致错误。在创建函数时,您不应该假设调用代码具有特殊知识。你应该对功能代码本身进行防错。
因此我建议使用三个骰子卷返回std :: vector或自定义结构。 e.g。
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;
}
然后,你不是制作单独的dice1,dice2,dice3变量,而是制作两个&#34; three_dice&#34;对象,让它们从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;
此代码更安全,因为没有&#34; malloc&#34;所以没有可能的内存泄漏。
答案 3 :(得分:-1)
#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);
}
答案 4 :(得分:-2)
这是一个提示:你已经这样做了,因为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
}