Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  craps
Language: C/C++
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void playcraps();

int main(void)
{
    char answer;
    do{
        printf("\n\nWould you like to play craps? nType y for YES and n for NO: nAnswer:");
        answer=getchar();
        fflush(stdin);
        if (answer=='y' || answer=='Y')
            playcraps();
        }while (answer=='y' || answer=='Y');

return 0;    
}

void playcraps()
{
    srand(time(NULL));
    
    int dice1, dice2, roll, point;
    dice1=rand()%6+1;
    dice2=rand()%6+1;
    roll=dice1+dice2;
    printf("nYou rolled a %d and a %d, totaling %d.n", dice1, dice2, roll);
    point=roll;
    
    if (roll==2 || roll==3 || roll==12){
        printf("You lose.n");
        return;
        }
    else if (roll==7 || roll==11){
        printf("You won.n");
        return;
        }
    else
        {
            do{
                dice1=rand()%6+1;
                dice2=rand()%6+1;
                roll=dice1+dice2;
                printf("You rolled a %i and a %i, totalling %d.n", dice1, dice2, roll);
                if (roll==point){
                    printf("You win.n");
                    return;
                }
            }while (roll != 7);
        printf("You lose.n");
        }
}
          
          
Comments: