Bucky's challenge #2 | the dice game

0 Daniel Byun · November 13, 2016
Here's my attempt at it. It works, any feedback, suggestion, criticism, praise is appreciated

thanks!


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


int main()
{
    int dice1 = 0;
    int dice2 = 0;
    int dice3 = 0;
    int total = 0;
    char guess;


    puts("Press any key to start the diceroll guessing game!");
    getchar();
    {
        dice1 = ( rand()%6) + 1;
        printf("Dice roll #1 is %d\n", dice1);

      
        dice2 = ( rand()%6) + 1;
        printf("Dice roll #2 is %d\n", dice2);

        
        dice3 = ( rand()%6) + 1;
        printf("Dice roll #3 is %d\n\n", dice3);
    }

    
    total = dice1 + dice2 + dice3;    
    printf("The total of the three dice roll is %d\n\n", total);

    printf("Guess if the next roll is gonna be higher, lower or the same (higher = h, lower = l, same = s)\n");
    scanf(" %c", &guess);

    
    dice1 = ( rand()%6) + 1;

    // the last dice roll is gonna be 3. seems like it prints the same number every time...so yea.

    if (guess == 'l')
    {
        islower((guess));
        printf("You have guessed correctly!\n");
    }

    else if (guess == 's')
    {
        islower((guess));
        printf("Sorry, you're wrong!\n");
    }

    else if (guess == 'h')
    {
        islower((guess));
        printf("Sorry, you're wrong!\n");
    }

    else
    {
        printf("Please guess using 'h' for higher, 'l' for lower, and 's' for the same.\n");
    }
    

    printf("Dice roll #4 is %d\n", dice1);

    

    return 0;

}


second try, tried using a for loop this time.

please criticize the shiz outta it!



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

int main()
{
    int dice1 = 0;
    int dice2 = 0;
    int dice3 = 0;
    int total = 0;
    int i = 0;
    char guess;

    puts("Press any key to start the diceroll guessing game!");
    getchar();

    
    for (i = 0; i < 3; i++)
    {
        dice1 = ( rand()%6) + 1;
        dice2 = ( rand()%6) + 1;
        dice3 = ( rand()%6) + 1;
    }

    printf("Dice roll #1 is %d, Dice roll #2 is %d, Dice roll #3 is %d\n", dice1, dice2, dice3);

    

    total = dice1 + dice2 + dice3;
    printf("The total of the three dice roll is %d\n\n", total);

    

    printf("Guess if the next roll is gonna be higher, lower or the same (higher = h, lower = l, same = s)\n");
    scanf(" %c", &guess);

    
    dice1 = ( rand()%6) + 1;

    
    // the last dice roll is gonna be 2. seems like it prints the same number every time

    if (guess == 'l')
    {
        islower((guess));
        printf("You have guessed correctly!\n");
    }

    else if (guess == 's')
    {
        islower((guess));
        printf("Sorry, you're wrong!\n");
    }

    else if (guess == 'h')
    {
        islower((guess));
        printf("Sorry, you're wrong!\n");
    }

    else
    {
        printf("Please guess using 'h' for higher, 'l' for lower, and 's' for the same.\n");
    }

    printf("Dice roll #4 is %d\n", dice1);


    return 0;

}


Post a Reply

Replies

Oldest  Newest  Rating
0 Lazar Bulic · December 16, 2016
Ok there are a couple suggestions and problems in your code. 

Suggestions:
I see you have noticed to that you get same numbers from
rand()


To correct this (I think Bucky already covered this in his tutorials but not sure watched theme like 5 years ago) try using this as reference http://stackoverflow.com/questions/3067364/generating-random-numbers-in-c
When you are checking to see what is the user input l,h,s maybe it is better to use case statement instead of repeated else if.
Problem:
The function you are using isLower is not used correctly. This function checks if letter passed as argument is lowercase which will be correct every time as you are passing l,h,s Reference: https://www.tutorialspoint.com/c_standard_library/c_function_islower.htm
Solutions:
1) Write your own function to hand the checking
2) Do the checking inside if statement or if you decide to switch to case than in case body.
0 King Arthas · April 23, 2017
Hi,

this is my Code , thanks for the feedback :) 

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


int main(){


    int i,DiceA , DiceB, DiceC,SumA, SumB;
    char Guess;

for(i=0; i<=20; i++){
        DiceA = ( rand()%6) +1;
        DiceB = ( rand()%6 )+1;
        DiceC = ( rand()%6 )+1;
        SumA= DiceA + DiceB + DiceC;
        printf(" \n %d is the sum of result of the three Dices \n", SumA);

        printf(" \n Please Guess the next result if it will be Higher (H), Lower (L) or the Same (S) : ");
        scanf(" %c", &Guess);

        DiceA = ( rand()%6) +1;
        DiceB = ( rand()%6) +1;
        DiceC = ( rand()%6) +1;
        SumB= DiceA + DiceB + DiceC;

         printf(" \n %d is the sum of result of the three Dices in the second rolling \n", SumB);

   if ( SumB > SumA && Guess == 'H'){

    printf("\n You're good \n");

   }else if (SumB < SumA && Guess == 'L'){

   printf("\n You're good\n ");

   }else if ( SumA == SumB && Guess == 'S'){

    printf("\n You're good\n ");
   }else {

    printf("\n You suck !\n ");


   }
}

    return 0;

}
  • 1

C

129,981 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator