#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

#define YES 1
#define NO 0
#define MAX 100


int continue_function(void);
int dice_roll(void);
int next_dice_roll(void);
char user_guess();
void display_result (void);


    int dice_a, dice_b, dice_c;
    int i, x, y, cont;
    int sum;
    int past_roll, new_roll;
    char guess;


int main()
{

     printf("\n\n");
    printf("\nThis program lets you guess if the sum of the");
    printf("\nnumbers that will show up when 3 dices are rolled");
    printf("\nis higher, less or same as the previous roll\n\n");

     printf("\nenter l to guess if the next roll will be lower");
    printf("\nenter s to guess if the next roll will be the same");
    printf("\nenter h to guess if the next roll will be higher\n\n");


    cont=continue_function();
    if(cont==YES)
    {
       display_result();
    }
    else{
       printf("\n\nGame aborted by user...\n");
    }



     return 0;
}

int continue_function(void)
{
    printf("\n\nDo you wish to continue? (0=NO/1=YES): ");
    scanf(" %d", &x);

    while(x<0 || x>1){
        printf("\n%d is invalid", x);
        printf("\nPlease enter 0 to quit or 1 to continue");
        scanf(" %d", &x);
    }

    if(x==1){
        return (YES);
    }if(x==0){
        return (NO);
    }
}

int dice_roll(void)
{

    for(i=1; i<MAX; i++)
    {

       do{
         dice_a = (rand() % 6) +1;
         }while(i<0 || i>MAX);

         do{
         dice_b = (rand() % 6) +1;
         }while(i<0 || i>MAX);

         do{
         dice_c = (rand() % 6) +1;
         }while(i<0 || i>MAX);

         sum = dice_a + dice_b + dice_c;

         printf("\nThe random numbers chosen are: %d, %d and %d", dice_a, dice_b, dice_c);
         printf("\n And their sum is: %d.", sum);
            break;
    }

         return sum;
}

char user_guess()
{
    char lower = 'l';
    char same = 's';
    char higher = 'h';
    int x=0;

   do{
    printf("\n\nenter a letter l, h or s to guess the nxt sum: ");
    scanf(" %c", &guess );

         switch(guess){
           case 'l' : return guess;
                   break;
           case 's' : return guess;
                   break;
           case 'h' : return guess;
                   break;
           default : x = 5;

          printf("\nwrong entry");

    }

    }while(x==5);

}

int next_dice_roll()
{
    new_roll = dice_roll();

    return new_roll;
}

void display_result(void)
{
    for(cont=YES,y=0; y<MAX && cont==YES; y++)
    {
       past_roll = dice_roll();
       guess = user_guess();
       new_roll = next_dice_roll();


    if(guess=='h' && new_roll > past_roll){
        printf("\n\nyou guessed right, CONGRATULATION");
    }else if(guess=='l' && new_roll < past_roll ){
        printf("\n\nyou guessed right, CONGRATULATION");
    }else if(guess=='s' && new_roll==past_roll){
        printf("\n\nyou guessed right, CONGRATULATION");
    }else{
         printf("\n\nyou lost");
    }

        cont = continue_function();
    }

}