Advertisement
XxZajoZzO

C spare game

Nov 19th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4.  
  5. int rollDice() {
  6.     return (rand() % 6) + 1;
  7. }
  8.  
  9. int rollAndAdd() {
  10.     return rollDice() + rollDice();
  11. }
  12.  
  13. int main() {
  14.     int sum, wins = 0, loses = 0;
  15.     char cont = 'y';
  16.     srand(time(NULL));
  17.     printf("Press Enter to Start");
  18.     getchar();
  19.     while (cont == 'y' || cont == 'Y')
  20.     {
  21.         printf("Rolling dice!\n");
  22.         sum = rollAndAdd();
  23.         printf("The sum is:%d\n", sum);
  24.         if (sum == 6 || sum == 9) {
  25.             printf("You Won!\n");
  26.             wins++;
  27.         }
  28.         else if (sum == 3 || sum == 12) {
  29.             printf("Game Over!\n");
  30.             loses++;
  31.         }
  32.         else {
  33.             printf("Spare!\n");
  34.             while (1) {
  35.                 printf("Press Enter to Continue");
  36.                 getchar();
  37.                 sum = rollAndAdd();
  38.                 printf("The sum is:%d\n", sum);
  39.                 if (sum == 6 || sum == 9) {
  40.                     printf("Game Over!\n");
  41.                     loses++;
  42.                     break;
  43.                 }
  44.                 else if (!(sum == 3 || sum == 12)) {
  45.                     printf("Spare!\n");
  46.                     printf("You Won!\n");
  47.                     wins++;
  48.                     break;
  49.                 }
  50.                 else {
  51.                     printf("Try Again!");
  52.                 }
  53.             }
  54.         }
  55.         printf("Do you want to play again (Y/y:Yes, N/n:No):");
  56.         scanf_s(" %c", &cont);
  57.         getchar();
  58.         while (cont != 'y' && cont != 'Y' && cont != 'n' && cont != 'N') {
  59.             printf("Wrong Input!\nTry Again:");
  60.             scanf_s(" %c", &cont);
  61.             getchar();
  62.         }
  63.     }
  64.     printf("You Won %d Times And Lost %d Times\nThanks for playing!", wins, loses);
  65.     getchar();
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement