Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void play_craps();
- int main(void)
- {
- play_craps();
- return 0;
- }
- void play_craps()
- {
- int rand_int(int a, int b);
- srand(time(NULL));
- int die_1,die_2,die_3,die_4,roll,roll_2;
- // getting the outputs of two dice throw
- die_1=rand_int(1,6);
- die_2=rand_int(1,6);
- roll=die_1 + die_2;
- printf("Player rolled %d + %d = %d\n\n",die_1,die_2,roll);
- // checking the winning or loosing conditions
- if(roll==7 || roll==11)
- {
- printf("Player wins.");
- return;
- }
- else if(roll==2 || roll==3 || roll==12)
- {
- printf("Player loses.");
- return;
- }
- // else continuing the loop until a result is obtained
- else
- {
- printf("Point is %d, The game continues:\n\n",roll);
- do
- {
- // again similarly generating outputs
- die_3=rand_int(1,6);
- die_4=rand_int(1,6);
- roll_2=die_3 + die_4;
- printf("Player rolled %d + %d = %d\n",die_3,die_4,roll_2);
- if(roll_2==roll)
- {
- printf("\nPlayer wins.");
- return;
- }
- }while(roll_2 != 7);
- printf("\nPlayer loses.");
- }
- }
- // Function to generate randon output of dice
- int rand_int(int a, int b)
- {
- return rand()%(b-a+1)+a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement