Challenge#2 Dice game

+1 clifford tampor · February 5, 2016
UPDATED 2/10/2016

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

int main()

{   int total_1, total_2, numberOfRoll, diceRoll;
    char guess, t_1, t_2, t_3;

    total_1 = total_2 = numberOfRoll = diceRoll = 0;

    srand( time(NULL) );

do{
    for(numberOfRoll = 1; numberOfRoll <= 3 ; ++numberOfRoll){
        diceRoll = ( rand()%6 ) + 1;

        printf("%d roll: %d \n", numberOfRoll, diceRoll);
        total_1 = diceRoll + total_1;


        if (numberOfRoll == 3) {
                printf("sum is %d", total_1);


                    if ( (t_2 == 'Y') || (t_2 == 'y') ){
                        if( (guess == 'H') || (guess == 'h') ) {
                            if( total_1 > total_2) printf("\nWow!! You guessed it right!! ");
                            else printf("\nWrong!!");
                                }
                        if( (guess == 'L') || (guess == 'l') ){
                            if( total_2 > total_1) printf("\nWow!! You guessed it right!! ");
                            else printf("\nWrong!!");
                                }
                        if( (guess == 'S') || (guess == 's') ){
                            if( total_2 == total_1) printf("\nWow!! You guessed it right!! ");
                            else printf("\nWrong!!");
                                }
                                                        }

                                }
    }

    do{
        t_2 = NULL;
        t_1 = NULL;
    printf("\nWould you like to roll the dice again? Y/N ");
    scanf(" %c", &t_2);
        if ( t_2 == 'N' || t_2 == 'n' || t_2 == 'Y' || t_2 == 'y' ) break;
        if ( t_2 >= 'a' || t_2 >= 'A' || t_2 <= 'Z' || t_2 <= 'z' ) printf( "Invalid Input!!");

        }while(t_1 != 'n');
    if ( t_2 == 'N' || t_2 == 'n') break;

    do{
            t_3 = NULL;
            guess = NULL;
    printf("\nHihger, Lower or Same? H/L/S ");
    scanf(" %c", &guess);
        if ( guess == 'H' || guess == 'h' || guess == 'L' || guess == 'l' || guess == 'S' || guess == 's' ) break;
        if ( guess >= 'A' || guess >= 'a' || guess <= 'z' || guess <= 'Z' ) printf("Invalid Input!!");

    }while( t_3 != 'n');

    total_2 = total_1;
    total_1 = 0;

        }while( t_2 != 'n' );


    getch();
}


i just have 1 question, why is it that i get same numbers and totals everytime i run it? 1st sum is 17 then 16 etc. is that normal? are my codes right?

>>i did some google to know the problem, and yep that seed thing lol honestly i still dont understand that part. i just inserted this "srand( time(NULL) );" then boom!! it work.. now its perfect. just need some design. :D 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Linguist Llama · February 6, 2016
This question is very common. If your resource doesn't answer common questions like this one, it might be a good idea to think about choosing a new resource.
  • 1

C

107,109 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator