[英]How do i create a function that generates 3 dice rolls in 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;
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();
如果要一个返回多个值的函数,请使用引用参数。
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);
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” 3次。 重复是不好的形式。 您可以在现实世界的编码中摆脱它,但是在对函数的概念进行测试时,您不应懒惰。 将任何重复的代码转换为通用函数。
下一个级别是“掷三个骰子”。 您的一些选择包括返回指针,结构或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;
}
然后,您不用创建单独的dice1,dice2,dice3变量,而是创建了两个“ three_dice”对象,然后让它们从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”,因此不会发生内存泄漏。
#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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.