Home > Back-end >  How to pass rand() values from a function to the main function?
How to pass rand() values from a function to the main function?

Time:12-04

Haven't program for a while, but I have this game project where I need to randomized who will be player 1 and player 2 and I need to use a user-defined function because it will be part of a bigger function. Player 1 and 2 should reflect whaat will be printed above. How can I improve my code? I also cannot use global variables.

#include<stdio.h>

int randomColor(int nRandom, int nRed, int nBlue)
{   
    srand(time(NULL)); 
    nRandom = (rand()%2); 

    switch (nRandom)
    {
        case 0: 
            nRed = 1;
            nBlue = 2;
            printf("\n\n\tPlayer %d = Red\n", nRed);
            printf("\tPlayer %d = Blue\n", nBlue);
            break;
        case 1: 
            nRed = 2;
            nBlue = 1;
            printf("\n\n\tPlayer %d = Blue\n", nRed);
            printf("\tPlayer %d = Red\n", nBlue);
            break; 
    }
}

int main()
{
    int nRandom, nRed, nBlue;
    randomColor(nRandom, nRed, nBlue);
    printf("\nPlayer %d (R) turn", nRed);
    printf("\nPlayer %d (B) turn", nBlue);      
}

CodePudding user response:

Here is one way you can improve your code to return the values of nDark and nLight from the randomColor function, so that they can be used in the main function:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

// Define a struct to store the values of nDark and nLight.
typedef struct {
    int nDark;
    int nLight;
} RandomColor;

RandomColor randomColor()
{
    RandomColor color;
    srand(time(NULL)); 
    int nRandom = (rand()%2); 

    switch (nRandom)
    {
        case 0: 
            color.nDark = 1;
            color.nLight = 2;
            printf("\n\n\tPlayer %d = Dark\n", color.nDark);
            printf("\tPlayer %d = Light\n", color.nLight);
            break;
        case 1: 
            color.nDark = 2;
            color.nLight = 1;
            printf("\n\n\tPlayer %d = Light\n", color.nLight);
            printf("\tPlayer %d = Dark\n", color.nDark);
            break; 
    }

    return color;
}

int main()
{
    // Call the randomColor function to get the values of nDark and nLight.
    RandomColor color = randomColor();

    printf("\nPlayer %d (D) turn", color.nDark);
    printf("\nPlayer %d (L) turn", color.nLight);     
}

In this code, I have defined a struct called RandomColor to store the values of nDark and nLight that are generated by the randomColor function. The randomColor function now returns a RandomColor struct, which contains the values of nDark and nLight that are generated.

In the main function, I call the randomColor function and store the return value in a variable of type RandomColor. I can then access the nDark and nLight values in this variable to use them in the printf statements.

I have also made a few other changes to your code:

I have added the stdlib.h and time.h headers, which are needed for the rand and time functions that are used in the code. I have removed the unnecessary parameters from the randomColor function, since the values of nDark and nLight are now stored in the struct that is returned by the function. I have changed the randomColor function to have a return type of RandomColor, so that it can return the struct containing the nDark and nLight values.

CodePudding user response:

Although what you have is fine, you could be more efficient by having one boolean variable instead of the two. Player1Dark=True or False Then Player2Dark is not required, its just !Player1Dark As they are mustually exclusive

  • Related