Challenge # 2

+1 Red Man · March 7, 2015
Hello again,

This is my program for Challenge # 2 with comments: Pastebin link here - http://pastebin.com/S8YiuGjq

I'll be grateful to anyone whose reply can make my program more clear and concise.


*Post a screen of your highest score with zero games lost.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
/*Dice Roll Game - User rolls 3 dice and then guesses if
 *the next roll will be higher, lower, or the same as the first
 *roll*/
int main()
{
    char roll, guess, replay;
    int diceRoll, diceRoll2, diceRoll3;
    int higher, lower, same, sum1, sum2;
    int i = 0, won = 0, lost = 0;
    time_t t;

//Prompts user to roll dice
    printf("\nWould You Like To Roll The Dice? (Y or N) ");
    scanf(" %c", &roll);

    if(islower(roll)) roll = toupper(roll);
    if(roll != 'Y'){
        printf("\n\Thank You, Good Bye.\n\n");
        return(0);
    }

/*Initializes random number generator
 *Prints 3 random numbers from 1 to 6
 *Included the Sleep() function from Windows.h*/
    DICE_ROLL:

    srand(time(&t));
    diceRoll  = (rand() % 6 + 1);
    diceRoll2 = (rand() % 6 + 1);
    diceRoll3 = (rand() % 6 + 1);
    sum1 = diceRoll + diceRoll2 + diceRoll3;

    printf("\n*****  *****  *****\n* %d *  * %d *  * %d *  "
           "Total = %d\n*****  *****  *****", diceRoll, diceRoll2, diceRoll3, sum1);
    printf("\n\n\nGood Roll! Now Guess If Your Next Roll Will Be Higher,"
           " Lower,\nor the Same Total Of %d.\n\n", sum1);
    printf("<H or L or S>:");

/*Tests user's answer for characters H/L/S and stores
 *1 in respective variable count*/
        higher = 0, lower = 0, same = 0;
    do{
        scanf(" %c", &guess);
        if(islower(guess)) guess = toupper(guess);
        if(guess == 'H')
            higher = 1;
        if(guess == 'L')
            lower = 1;
        if(guess == 'S')
            same = 1;
        if((higher || lower || same) <= 0){
            printf("Wrong Letter, Try Again.\n<H or L or S>");
        }
    }while((guess != 'H') && (guess != 'L') && (guess != 'S'));

//Second dice roll
    srand(time(&t));
    diceRoll  = (rand() % 6 + 1);
    diceRoll2 = (rand() % 6 + 1);
    diceRoll3 = (rand() % 6 + 1);
    sum2 = diceRoll + diceRoll2 + diceRoll3;

    printf("\n*****  *****  *****\n* %d *  * %d *  * %d *  "
           "Total = %d\n*****  *****  *****", diceRoll, diceRoll2, diceRoll3, sum2);

//Test user's guess against sum of dice roll 1 & 2
    if(higher && sum2 > sum1){
        won++;
        printf("\n\nCorrect!  You Rolled Higher.");
    }else if (lower && sum2 < sum1){
        won++;
        printf("\n\nCorrect!  You Rolled Lower.");
    }else if(same && sum2 == sum1){
        won++;
        printf("\n\nNice, Good Guess!");
    }else{
        lost++;
        printf("\n\nWrong!  Bad Guess.");
    }

    printf("\nGames Won:  %d\nGames Lost: %d\n\n", won, lost);

//Prompts user to replay/quit.  Replay loops back to DICE_ROLL
    printf("\nPlay Again? (Y or N) ");
    scanf(" %c", &replay);

    if(islower(replay)) replay = toupper(replay);
    if(replay == 'Y') goto DICE_ROLL;
    else printf("\n\Thank You, Good Bye.\n\n");

return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Matthew Cliatt · March 9, 2015
Looks good, but you want to be sure to indent your code in your if/else statements. It could just be the way it's displayed here but the lines after "//Test user's guess against sum of dice roll 1 & 2" aren't indented over in the if statements. Also, in your do..while loop the same issue. But looks fine other than that.

Also, your first loop in the DICE_ROLL is unnecessary, since it is only executed once. You could just have those statements outside of the for loop.

You may also want to avoid using any system calls, like when you use "system("cls")". It is considered bad programming practice since it then becomes Operating System dependent.
0 Red Man · March 9, 2015
Hi Matthew,

I will read up on indent style and make some changes.  Thanks for spotting the unnecessary for loop after DICE_ROLL -  I will fix that too.  

And, I'll remove the system calls.  Thanks again.
0 Matthew Cliatt · March 10, 2015
No problem. Looking good!
  • 1

C

106,928 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator