5
\$\begingroup\$

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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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 < runTime - 1; i++){
            die1 = ( (rand() % 6) + 1);
            die2 = ( (rand() % 6) + 1);
            die3 = ( (rand() % 6) + 1);
            total = (die1 + die2 + die3);

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

            if( (total > 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;
    }
\$\endgroup\$
  • 2
    \$\begingroup\$ "started C programming a couple days ago." --> Very nice initial post. \$\endgroup\$ – chux - Reinstate Monica Oct 11 '16 at 2:43
4
\$\begingroup\$

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 <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);
}
\$\endgroup\$
  • \$\begingroup\$ Joseph didn't say that C is his first language, just that he's new to it. If it is his first language, then that's a pretty good start! \$\endgroup\$ – Toby Speight Oct 11 '16 at 10:03
  • \$\begingroup\$ I didn't assume C is his first language, only that it's a bit too early. The things I pointed out are pretty language-independent and by his question it looks like he's quite new to programming. And is doesn't contain C-stuff like memory management or similar low level things, which I would expect of a more experienced programmer but who is new to C. \$\endgroup\$ – Oskar Skog Oct 11 '16 at 11:50
  • 1
    \$\begingroup\$ Great suggestions. I've taken a beginner Java course at my University but learning C on my own. Functions are new to me so that's why I didn't use them here. I see now how they could make this program much shorter. \$\endgroup\$ – Joseph Tillman Oct 11 '16 at 15:03
3
\$\begingroup\$

Sooner or later you will have trouble with user input using scanf().

scanf() and friends return the number of successfully scanned fields or EOF on certain end-of-file or error conditions.

Recommend to start the habit of checking input results.

// scanf(" %d", &runTime);
//         v--- space not needed - leading white-spaces consumed with most specifiers
if (scanf("%d", &runTime) != 1) Handle_Failure();

// scanf(" %c", &guess);
//         v--- space useful, without it, leading white-spaces not consumed for %c %n %[]
if (scanf(" %c", &guess) != 1) Handle_Failure();
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.