Thread: Three Dice Roll

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    Three Dice Roll

    Question : Create a game using C wherein user rolls 3 dices and it prints out the total on them-dice a+dice b+dice c.Now after the user looks at the no. they would guess if the next roll will be higher(type H if it is) ,lower(type L for that) and if it is same(S for that). Then they gonna roll it again and if the guess is right print out "Good Job" and if wrong then "You suck" or something like that.

    My Solution:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>


    int main()
    {
    int a,b,c,x;
    char guess;

    x= rand()%6 + rand()%6 + rand()%6 +3;
    printf("The sum of the nos. on three dices is %d\n",x);


    a= rand()%6 +1;
    b= rand()%6 +1;
    c= rand()%6 +1;
    printf("Guess the next no.will be higher,lower or same(H/L/S):");
    scanf(" %s",&guess);
    printf("%d\n",a+b+c);
    switch(guess){
    case 'L' : ((a+b+c) < x) ? printf("\nGood Job") : printf("You Suck");
    break;
    case 'S' : ((a+b+c) == x) ? printf("\nGood Job") : printf("You Suck");
    break;
    case 'H' : ((a+b+c) > x) ? printf("\nGood Job") : printf("You Suck");
    break;
    default : printf("Sorry");
    }


    return 0;
    }



    Please do tell me if it is correct or anything that can be made better.
    Thanks.
    Attached Files Attached Files
    • File Type: c main.c (847 Bytes, 35 views)

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Instead of

    Code:
        a= rand()%6 +1;
        b= rand()%6 +1;
        c= rand()%6 +1;
    why don't you write something similar to line 10... maybe

    Code:
    y= rand()%6 + rand()%6 + rand()%6 +3;
    And then change all your conditionals in the switch to y < x etc instead of a+b+c

    I'd use better variable names than x and y, though.

    You haven't seeded the pseudo random number generator so the sequence of random numbers is going to be the same every time you run the program. Google for srand and call it once at the start of your program.

    Code:
    scanf(" %s",&guess);
    ^-- Did you intend this to be %s? If the user enters more than 1 character your program is likely to crash or be naughty in other ways

    I wouldn't hardcode the value of 6. I'd have something along the lines of
    Code:
    #define DICE_NUM_SIDES 6
    and then

    Code:
    x= rand()%DICE_NUM_SIDES + rand()%DICE_NUM_SIDES + rand()%DICE_NUM_SIDES +3;
    etc

    Makes the code longer, but it's more readable and easier to maintain if someone decides they want the game to use dice with 20 sides each

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    302
    because i enjoy coding random stuff...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define DICECOUNT 3
    #define DICESIDES 6
    #define SUCCESS "CORRECT."
    #define FAILURE "FAIL."
    
    int a, b, c, x, y; char guess;
    
    int roll() { 
        return (rand() % (DICESIDES - 1 + 1)) + 1; 
    };
    
    int rollDice(int diceAmount) {
        int xx = 0;
        for (int i=0; i < diceAmount; i++) {
            xx += roll();
        };
        return xx;
    };
    
    int main() {
        srand(time(0)); 
        
        x = rollDice(DICECOUNT); 
        y = rollDice(DICECOUNT);
    
        printf("NUMBER: %d\n", x);
        printf("[h]igher, [l]ower or [s]ame ?");
        scanf("%s",&guess);
        printf("NUMBER: %d\n", y);
    
        switch(guess) {
            case 's':
                if (x == y) {
                    printf("%s",SUCCESS);
                } else {
                    printf("%s",FAILURE);
                }
            break;
            case 'h':
                if (y > x) {
                    printf("%s",SUCCESS);
                } else {
                    printf("%s",FAILURE);
                };        
            break;
            case 'l':
                if (x > y) {
                    printf("%s",SUCCESS);
                } else {
                    printf("%s",FAILURE);
                }; 
            break;
        };
    
        return 0;
    };
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice roll and count.
    By Cody Staus in forum C Programming
    Replies: 5
    Last Post: 07-11-2014, 05:42 PM
  2. Simulating a dice roll, extreme beginner needs help
    By maybetomorrow in forum C Programming
    Replies: 10
    Last Post: 01-25-2014, 06:47 AM
  3. Dice Roll
    By seanksg in forum C Programming
    Replies: 4
    Last Post: 05-13-2011, 12:37 AM
  4. Dice Roll Program
    By HeidiPagel in forum C Programming
    Replies: 7
    Last Post: 12-13-2010, 10:39 AM
  5. C++ dice roll need help
    By catdieselpow in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2007, 01:32 PM

Tags for this Thread