HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Dice Roll Program in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rollprogramdice

Problem

I've made a dice game that rolls three die, calculates the sum of their results, and asks the user whether he/she thinks the next roll's total will be higher, the same, or lower than the current roll. It works but I want to know if there is a better/more efficient way to do this. I just started C programming a couple days ago.

#include 
#include 
#include 

int main()
{
    int i;
    int runTime;
    int die1;
    int die2;
    int die3;
    int temp;
    int total;
    char guess;
    int count = 0;
    srand(time(NULL));

    printf("How many times do you want to play?\n");
    scanf(" %d", &runTime);
    printf("");

    die1 = ( (rand() % 6) + 1);
    die2 = ( (rand() % 6) + 1);
    die3 = ( (rand() % 6) + 1);
    total = (die1 + die2 + die3);

    if(runTime != 0){

        printf("Die 1: %d\n", die1);
        printf("Die 2: %d\n", die2);
        printf("Die 3: %d\n", die3);
        printf("-------------------\n");
        printf("Total: %d\n\n", total);
        temp = total;

        printf("Do you think the next total will be higher, the same, or lower than the previous total? (h, s, l) \n\n");
        scanf(" %c", &guess);

        for(i = 0; i  temp) && (guess == 'h') ){
                printf("You guessed correctly!\n");
                count++;
            }else if( (total == temp) && (guess == 's') ){
                printf("You guessed correctly!\n");
                count++;
            }else if( (total < temp) && (guess == 'l') ){
                printf("You guessed correctly!\n");
                count++;
            }else{
                printf("You guessed incorrectly :(\n");
            }
            temp = total;
            printf("Do you think the next total will be higher, the same, or lower than the previous total? (h, s, l) \n");
            scanf(" %c", &guess);

        }
    printf("You got a total of %d guesses correct!", count);
     }else{
    printf("Goodbye.");
    }
    return 0;
    }

Solution

I wouldn't recommend C or C++ as a starter language.
Try to reimplement the game in Python or Java or some other high level language.

  • You can use a loop instead of repeating yourself with die 1 die 2 and die 3.



  • Use a function for rolling and reporting.



  • The main loop can be a for loop that scanf()s the number of guesses and counts down.



  • A switch statement on the guess token could come in handy.



#include 
#include 
#include 

#define N_DICE 3

/* Conveniently return the sum. */
int roll_dice(int *dice)
{
    int i, sum;
    for (i = sum = 0; i  old_sum; break;
            case 's': correct = sum == old_sum; break;
            case 'l': correct = sum < old_sum; break;
            default: correct = 0; printf("Not h, s or l.\n");
        }
        if (correct)
        {
            printf("You guessed correctly!\n");
            correct_guesses++;
        }
        else
            printf("You guessed incorrectly!\n");
    }
    printf("You got a total of %d guesses correct!\n", correct_guesses);
}

Code Snippets

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

#define N_DICE 3

/* Conveniently return the sum. */
int roll_dice(int *dice)
{
    int i, sum;
    for (i = sum = 0; i < N_DICE; i++)
    {
        dice[i] = rand()%6 + 1;
        sum += dice[i];
    }
    return sum;
}

void print_dice(int *dice, int sum)
{ 
    int i;
    printf("\n\n");
    for (i = 0; i < N_DICE; i++)
        printf("Die %d: %d\n", i + 1, dice[i]);
    printf("-------------------\nTotal: %d\n\n", sum);
}

int main()
{
    int runs, dice[N_DICE], sum, old_sum, correct, correct_guesses;
    char guess;

    srand(time(NULL));
    correct_guesses = 0;
    sum = roll_dice(dice);

    printf("How many times do you want to play? ");
    for (scanf("%d", &runs); runs; runs--)
    {
        print_dice(dice, sum);
        old_sum = sum;
        sum = roll_dice(dice);

        printf(
            "Do you think the next total will be higher, "
            "the same, or lower than the previous total? "
            "(h, s, l)\n"
        );
        scanf(" %c", &guess);
        switch (guess)
        {
            case 'h': correct = sum > old_sum; break;
            case 's': correct = sum == old_sum; break;
            case 'l': correct = sum < old_sum; break;
            default: correct = 0; printf("Not h, s or l.\n");
        }
        if (correct)
        {
            printf("You guessed correctly!\n");
            correct_guesses++;
        }
        else
            printf("You guessed incorrectly!\n");
    }
    printf("You got a total of %d guesses correct!\n", correct_guesses);
}

Context

StackExchange Code Review Q#143834, answer score: 4

Revisions (0)

No revisions yet.