Advertisement
biswasrohit20

Crap

Jun 2nd, 2021
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void play_craps();
  6.  
  7. int main(void)
  8. {
  9. play_craps();
  10. return 0;
  11. }
  12.  
  13. void play_craps()
  14. {
  15.     int rand_int(int a, int b);
  16.     srand(time(NULL));
  17.    
  18.     int die_1,die_2,die_3,die_4,roll,roll_2;
  19.     // getting the outputs of two dice throw
  20.     die_1=rand_int(1,6);
  21.     die_2=rand_int(1,6);
  22.     roll=die_1 + die_2;
  23.     printf("Player rolled %d + %d = %d\n\n",die_1,die_2,roll);
  24.    
  25.     // checking the winning or loosing conditions
  26.     if(roll==7 || roll==11)
  27.         {
  28.             printf("Player wins.");
  29.             return;
  30.         }
  31.     else if(roll==2 || roll==3 || roll==12)
  32.         {
  33.             printf("Player loses.");
  34.             return;
  35.         }
  36.    
  37.     // else continuing the loop until a result is obtained
  38.     else
  39.         {
  40.             printf("Point is %d, The game continues:\n\n",roll);
  41.             do
  42.             {
  43.                 // again similarly generating outputs
  44.                 die_3=rand_int(1,6);
  45.                 die_4=rand_int(1,6);
  46.                 roll_2=die_3 + die_4;
  47.                 printf("Player rolled %d + %d = %d\n",die_3,die_4,roll_2);
  48.                 if(roll_2==roll)
  49.                  {
  50.                      printf("\nPlayer wins.");
  51.                      return;
  52.                  }
  53.             }while(roll_2 != 7);
  54.  
  55.         printf("\nPlayer loses.");
  56.         }
  57.  
  58. }
  59.  
  60. // Function to generate randon output of dice
  61. int rand_int(int a, int b)
  62. {
  63.  
  64.     return rand()%(b-a+1)+a;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement