Advertisement
pedrolemoz

q1

Sep 16th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <time.h>
  3.  
  4. int dado(int);
  5.  
  6. int main(){
  7.     int i, sum = 0, count = 0;
  8.  
  9.     while (sum == 0){
  10.         printf("\nPress any key to roll dice.\n");
  11.         getch();
  12.         count += 1;
  13.        
  14.         for (i = 0; i < 2; i++)
  15.             sum += dado(i);
  16.            
  17.         printf("Sum: %d\n\n", sum);
  18.        
  19.         if (sum == 7 || count > 21)
  20.             printf("You lose!\n\n");
  21.             break;
  22.  
  23.         switch (sum){
  24.             case 7:
  25.                 printf("You won!\n\n");
  26.                 break;
  27.            
  28.             case 11:
  29.                 printf("You won!\n\n");
  30.                 break;
  31.            
  32.             case 2:
  33.                 printf("You lose!\n\n");
  34.                 sum = 0;
  35.                 break;
  36.                
  37.             case 3:
  38.                 printf("You lose!\n\n");
  39.                 sum = 0;
  40.                 break;
  41.                
  42.             case 12:
  43.                 printf("You lose!\n\n");
  44.                 sum = 0;
  45.                 break;
  46.                
  47.             default:
  48.                 printf("Try again!\n\n");
  49.                 sum = 0;
  50.                 break;
  51.         }
  52.     }
  53.    
  54.     printf("\nPress any key to exit...\n\n");
  55.     getch();
  56.        
  57.     return 0;
  58. }
  59.  
  60. int dado(offset){
  61.     srand(time(NULL) + offset);
  62.     return 1 + rand() % 6; 
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement