0

For this dice rolling game, I am supposed to count wins and losses rolling two dices at once.

If the sum of the two dice is 7 on the first roll then the player wins. If the first roll is not 7, then the play continues to run until it rolls a number the same as the first roll, then the player wins. If the player does not have a 7 on a first roll but gets 7 before matching the first roll, then the player loses.

The player plays 1000 games and count the wins and losses.

I am not entirely sure of the logic of this programming assignment, but my code execute and returns 0s for both wins and losses.

Can anyone please tell me what went wrong with the code?


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

int rolldice()
{
    int num[1000], wins, losses;
    
    srand(time(NULL));
    
    for (int i = 0; i < 1000; i++)
    {
        int dice1 = (rand() % 6) + 1;
        int dice2 = (rand() % 6) + 1;
        
        num[i] = dice1 + dice2;

        if (num[0] == 7)
        {
            wins++;
        }
        
        if (num[0] != 7 && num[i] == num[0])
        {
            wins++;
        }
        if (num[0] != 7 && num[i] == 7)
        {
            losses++;
        }
    return wins, losses;        
    }
    

    
}   
    


int main()
{
    int wins, losses;
    rolldice();
    printf("wins: %d \nlosses: %d", wins, losses);
    
    
}
bloomsdayforever
  • 263
  • 2
  • 13
  • `wins` and `losses` in `main` are not affected by `rolldice()`, you can define both variables as `static int`s at global scope in order to make them visible in both functions. – David Ranieri May 30 '21 at 13:16
  • You can't return local varaible values from a function like that. The `int wins, losses;` in `main()` are *different variables* from those in the function, they have not been initiliased, so when they are reported, the output is essentially random. Anyway, `main()` *ignores* any value returned by `rolldice();` – Weather Vane May 30 '21 at 13:17
  • Your variables are uninitialized, a function can only return a single value (which could be a struct), and `rolldice` doesn't affect the variables in `main`. In addition, it looks like your for loop returns on the first iteration. – Hasturkun May 30 '21 at 13:18
  • if you had written `losses = rolldice();` at least that part *could* be correct, but the function's own variables `wins` and `losses` are also not initialised either. – Weather Vane May 30 '21 at 13:20
  • 2
    You don't need to keep track of games in an array. Other than that there's a lot wrong with the game logic. Start with a function that returns the result of a single game (`1` is win, `0` is loss). Call that function 1000 times and add the result to a variable. After that, the variable contains the number of wins, and the number of losses is 1000 minus that variable. – Cheatah May 30 '21 at 13:21
  • `return wins, losses` is the same as `return losses`. The function only returns a single `int`, and the comma operator is not doing what you seem to think it does. – William Pursell May 30 '21 at 13:25

2 Answers2

0

Take a look at the following:

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

int roll(void)
{
    return 2 + (rand() % 6) + (rand() % 6);
}

int game(void)
{
    int first = roll();
    
    if (first == 7)
        return 1;
    
    while (1) {
        int second = roll();
        
        if (second == first)
            return 1;
        
        if (second == 7)
            return 0;
    }
}

int main(void)
{
    int wins = 0, i;

    srand(time(NULL));

    for (i = 0; i < 1000; i++) {
        wins += game();
    }
    
    printf("wins: %d \nlosses: %d", wins, 1000 - wins);
    
    return 0;
}

I have made a function for a single throw of two dice. That is convenient because we need the value in two different locations.

Then I wrote the logic for a single game. The case where the first roll is 7 is easy. But if the first roll is not a 7, the game could -in theory- go on forever. That is why a while loop is more or less required. In the while loop the logic is reversed and we need to compare to the first roll of the game. That's why we need to store the result in a variable first.

The third part is just to have the game play 1000 times and count wins.

Cheatah
  • 1,825
  • 2
  • 13
  • 21
0

There are a bunch of problems here:

  1. In C variables are NOT initialized 0 by default - check your wins and losses. Incrementing a variable requires reading it, and reading an uninitialized variable is instantly an undefined behavior.

  2. You can't return two values in C. You either make a wrapper that can hold two values and return a single wrapper, or pass pointers into your function and modify data at their address, or use static variables visible everywhere in a file. What you have instead is a comma operator, which always returns the second value.

  3. Your wins and losses variables declared in main() function aren't connected to wins and losses variables in rolldice() function in any way. Regarding the previous question, you might want to use pointers for that (although static variables would be easier).

  4. The logic behind you code is broken. This for loop is simulating a single game, and after getting either a win or a loss it should break;. But it doesn't, so even if you could see the results, they would be nonsensical.

  5. The return statement is inside of your for loop, always exiting after one iteration. This also obviously ruins the entire simulation.

  6. When comparing num[i] == num[0], you should check if i == 0, because otherwise it always succeeds and the player always wins.

Fixing these problems gives the minimum fixes required for your code to work:

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

int wins = 0;
int losses = 0;

int rolldice()
{
    int num[1000];
    
    srand(time(NULL));
    
    for (int game = 0; game < 1000; game++)
    {
        for(int i = 0; true; i++) // simulate a single game
        {
            int dice1 = (rand() % 6) + 1;
            int dice2 = (rand() % 6) + 1;
            
            num[i] = dice1 + dice2;
    
            if (num[0] == 7)
            {
                wins++;
                break;
            }
            
            if (num[0] != 7 && num[i] == num[0] && i != 0)
            {
                wins++;
                break;
            }
            if (num[0] != 7 && num[i] == 7)
            {
                losses++;
                break;
            }  
        }
    }
}   

int main()
{
    rolldice();
    printf("wins: %d \nlosses: %d", wins, losses);
}

This code is as close to yours as possible, except this version is working. However, there are a bunch of other improvements that can be made, see other comments for that.

DL33
  • 194
  • 8