简体   繁体   English

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

[英]How do i create a function that generates 3 dice rolls in C++

I need to create a function that generates 3 dice rolls, I've actually got a piece of code that generates 3 dice rolls but I need to call it from a function. 我需要创建一个生成3个骰子掷骰的函数,实际上我有一段代码可以生成3个骰子掷骰,但是我需要从一个函数中调用它。 Here's my code: 这是我的代码:

#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");
}

Your requirement isnt very clear! 您的要求不是很清楚!
Supposing you want the dice roll part in a function. 假设您希望某个功能中的骰子滚动部分。

Write a function like this 编写这样的函数

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

and call this function like this 然后像这样调用这个函数

dealer1=dice_roll();

If you want a function that returns multiple values, use reference parameters. 如果要一个返回多个值的函数,请使用引用参数。

void roll_3_dice(int &dice1, int &dice2, int &dice3) {
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    dice3 = rand() % 6 + 1;
    return;
}

Then you would call it like this: 然后,您将这样称呼它:

roll_3_dice(dealer1, dealer2, dealer3);

Tanuj Yadav has the right idea, you look for the smallest repeated section of code, then you encapsulate that as a function. Tanuj Yadav有一个正确的想法,您寻找最小的重复代码段,然后将其封装为一个函数。 eg the basic repeated unit is effectively 例如基本的重复单元是有效的

int dice = rand () % 6 + 1

so you can make a function. 所以你可以做一个功能。 you need to get an "int" back, and you want the usage to look like this 您需要取回“ int”,并且您希望用法看起来像这样

int dice = roll_die();

to make a function, the basic pattern is: 要执行功能,基本模式是:

return_type name(parameter_type parameter_name, etc)

so your return type is "int", name is "roll_die", and parameter is optional. 因此您的返回类型为“ int”,名称为“ roll_die”,参数为可选。 I would use number of sides as the parameter. 我将使用边数作为参数。

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

or 要么

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

^ This one assumes sides is 6 if you don't specify otherwise, but can be used for any-sided dice without needing new code. ^如果没有另外指定,此假设边数为6,但是无需任何新代码即可用于任意边骰子。

This maintains the concept of only repeating the same code once per program. 这保持了每个程序仅重复相同代码一次的概念。 You can make a function that rolls more than one die, but it should call the "roll_die" function itself rather than repeating "rand () % 6 + 1" three times. 您可以使一个函数滚动多个骰子,但是它应该调用“ roll_die”函数本身,而不是重复执行“ rand()%6 + 1” 3次。 Repetition is bad form. 重复是不好的形式。 You can get away with it in real-world coding, but you should not be lazy when you're being tested on the concept of functions. 您可以在现实世界的编码中摆脱它,但是在对函数的概念进行测试时,您不应懒惰。 Convert any repeated code to a common function. 将任何重复的代码转换为通用函数。

The next level is to "roll three dice". 下一个级别是“掷三个骰子”。 Some of your choices include returning a pointer, a struct or a std::vector. 您的一些选择包括返回指针,结构或std :: vector。 Pointers have been shown in other answers but I highly recommend against those implementations. 指针已在其他答案中显示,但我强烈建议您不要使用这些实现。 If you return memory created with malloc, it can cause errors if the client doesn't call "free". 如果返回使用malloc创建的内存,那么如果客户端不调用“ free”,则可能导致错误。 When making a function, you should NOT assume that the calling code has special knowledge. 在执行功能时,您不应假定调用代码具有特殊知识。 You should error-proof the function code itself. 您应该对功能代码本身进行防错。

For that reason I suggest either returning a std::vector or a custom struct with the three dice rolls. 因此,我建议您返回一个std :: vector或带有三个骰子卷的自定义结构。 eg 例如

struct three_dice
{
    int roll1, roll2, roll3;
}

Or this version which uses an array: 或使用数组的此版本:

struct three_dice
{
    int roll[3];
}

Then your function returns a three_dice object that it makes: 然后,您的函数将返回一个它产生的three_dice对象:

three_dice roll_three_dice()
{
    three_dice temp;
    temp.roll1 = roll_die();
    temp.roll2 = roll_die();
    temp.roll3 = roll_die();
    return temp;
}

Or like this with the array version: 或像这样使用数组版本:

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

Then, instead of making separate dice1, dice2, dice3 variables, you make two "three_dice" objects, and have them copy their values from the return type of the roll_three_dice function, which itself calls the roll_die function three times: 然后,您不用创建单独的dice1,dice2,dice3变量,而是创建了两个“ three_dice”对象,然后让它们从roll_three_dice函数的返回类型复制其值,该函数本身调用roll_die函数三次:

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

And you can get the values out like this: 您可以像这样获取值:

cout << "You rolled the following: " << endl;
cout << player.roll1 << "-" << player.roll2 << "-" << player.roll3 << endl;

Or if you used an array instead of the three names: 或者,如果您使用数组而不是三个名称:

cout << "You rolled the following: " << endl;
cout << player.roll[0] << "-" << player.roll[1] << "-" << player.roll[2] << endl;

This code is safer because there is no "malloc" so no possible memory leaks. 此代码更安全,因为没有“ 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);
}

Here is a hint: you already did because main() is a function. 这是一个提示:您已经做了,因为main()是一个函数。 Main is a function that returns an integer, which is not included in your code(return 0;). Main是一个返回整数的函数,该整数不包含在您的代码中(返回0;)。 A void function using all of your code will work. 使用所有代码的void函数将起作用。

void diceGame()
{
    everything you have in main
}

int main()
{
    diceGame();

    system("pause");
    return 0;
}

Alternatively: 或者:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM