jeudi 8 février 2018

When roll a dice with over 7 digits in a dice

My aim is to make a program that will throw two dice until the top faces of the two dice total to a specified number. Such as: ( Output) Total sought : 11 Result of throw 1 : 1 + 3;Result of throw 2 : 4 + 4 ; Result of throw 3 : 6 + 2 ; Result of throw 4 : 5 + 6 ; You got your total in 4 throws

However, in my code, i roll a dice with over 7 digits. Help me please, and not replace while loop with "if-else" please? Thank you!

My code:

#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int intRandom(int a=1,int b=6)
{
    int n;
    n=a+rand()%b;
    return n;

}

int main()
{
    int i,n,x,y; /*n:total, i:count*/
    printf("Dice Thrower\n================\n");
    printf("Total sought: ");
    do
    {
        scanf("%d",&n);
    } while (n<2 && n>12);

    {
        i=1;
        srand(time(NULL));
        do
        {
            x=intRandom(2,6);
            y=intRandom(6,2);

            printf("Result of throw %d: %d + %d\n",i,x,y);
            if ((x+y)==n) printf("\nYou got your total in %d throws!\n",i);

            fflush(stdin);

            i++;            
        } while ((x+y)!=n);
    }
    getch();
}




Aucun commentaire:

Enregistrer un commentaire