简体   繁体   中英

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. 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. 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 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. 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.

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. 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. 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". 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. 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 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:

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.

#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 is a function that returns an integer, which is not included in your code(return 0;). A void function using all of your code will work.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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