Advertisement
ggorann

Challenge #2 Buckys Room - Rolling dice

Oct 24th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <windows.h>
  7. #include <time.h>
  8.  
  9. /*
  10. Challenge #2 Buckys Room
  11. Rolling dice by Goran
  12. */
  13.  
  14. int main()
  15. {
  16.     int diceA, diceB, diceC, diceSumFirstTime, diceSumSecondTime;
  17.     char answer[7]; // 7 bytes for storing user answer Yes or No. Also for storing second question answer Higher, Lower or Same.
  18.  
  19.     printf("Are you ready to roll the dice ? "); // asking user is he ready to roll the dice. User answer with Yes, Y, No or N.
  20.     scanf(" %s", answer); // gets input from user and stores it into char answer.
  21.     answer[0] = toupper(answer[0]); // making first letter user input uppercase if it's already not.
  22.  
  23.     if(answer[0] == 'Y'){
  24.         srand (time(NULL)); // this line initialize random seed.
  25.         diceA = (rand() % 6) + 1;
  26.         diceB = (rand() % 6) + 1;
  27.         diceC = (rand() % 6) + 1;
  28.  
  29.         printf("\nDice A: %d \n", diceA); // prints first random number.
  30.         Sleep(1000); // pausing here for 1 second.
  31.         printf("Dice B: %d \n", diceB); // prints second random number.
  32.         Sleep(1000); // pausing here for 1 second.
  33.         printf("Dice C: %d \n", diceC); // prints third random number.
  34.         Sleep(1000); // pausing here for 1 second.
  35.  
  36.         diceSumFirstTime = diceA + diceB + diceC; // calculating sum of all dices.
  37.         printf("Dice Sum: %d \n \n", diceSumFirstTime); // prints sum of all dices.
  38.         Sleep(1000); // pausing here for 1 second.
  39.  
  40.     printf("Will the sum of next three dices be higher, lower or same ? "); // asking user a question. User answer with Higher, H, Lower, L, Same or S.
  41.     scanf(" %s", answer); // gets input from user and stores it into char answer.
  42.     answer[0] = toupper(answer[0]); // making first letter user input uppercase.
  43.  
  44.     if(answer[0] == 'H' || answer[0] == 'L' || answer[0] == 'S'){
  45.         printf("Rolling for the second time. \n"); // prints message about rolling for the second time.
  46.         Sleep(1000); // pausing here for 1 second.
  47.  
  48.         diceA = (rand() % 6) + 1;
  49.         diceB = (rand() % 6) + 1;
  50.         diceC = (rand() % 6) + 1;
  51.  
  52.         printf("\nDice A: %d \n", diceA); // prints first random number.
  53.         Sleep(1000); // pausing here for 1 second.
  54.         printf("Dice B: %d \n", diceB); // prints second random number.
  55.         Sleep(1000); // pausing here for 1 second.
  56.         printf("Dice C: %d \n", diceC); // prints third random number.
  57.         Sleep(1000); // pausing here for 1 second.
  58.  
  59.         diceSumSecondTime = diceA + diceB + diceC; // calculating sum of all dices for the second time.
  60.         printf("Dice Sum: %d \n \n", diceSumSecondTime); // prints sum of all dices for the second time.
  61.         Sleep(1000); // pausing here for 1 second.
  62.  
  63.     if(diceSumFirstTime < diceSumSecondTime && answer[0] == 'H'){
  64.         printf("Well done. Sum is higher then first roll. \n"); // if diceSumFirstTime is lower then diceSumSecondTime and answer is Higher this message is printed.
  65.     }else if(diceSumFirstTime > diceSumSecondTime && answer[0] == 'L'){
  66.         printf("Well done. Sum is lower then first roll. \n"); // if diceSumFirstTime is higher then diceSumSecondTime and answer is Lower this message is printed.
  67.     }else if(diceSumFirstTime == diceSumSecondTime && answer[0] == 'S'){
  68.         printf("Well done. Sum is the same as first roll. \n"); // if diceSumFirstTime is same as diceSumSecondTime and answer is Same this message is printed.
  69.     }else{
  70.         printf("Just unlucky. \n");
  71.     }
  72.  
  73.     }else{
  74.         printf("Maybe try next time. \n \n"); // message that prints to user if he doesn't enter correct option.
  75.         }
  76.     }else{
  77.         printf("Maybe try next time. \n \n"); // message that prints to user if he doesn't enter correct option.
  78.         }
  79.  
  80.     system("pause"); // pausing program after executing.
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement