Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <math.h>
- #include <windows.h>
- #include <time.h>
- /*
- Challenge #2 Buckys Room
- Rolling dice by Goran
- */
- int main()
- {
- int diceA, diceB, diceC, diceSumFirstTime, diceSumSecondTime;
- char answer[7]; // 7 bytes for storing user answer Yes or No. Also for storing second question answer Higher, Lower or Same.
- 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.
- scanf(" %s", answer); // gets input from user and stores it into char answer.
- answer[0] = toupper(answer[0]); // making first letter user input uppercase if it's already not.
- if(answer[0] == 'Y'){
- srand (time(NULL)); // this line initialize random seed.
- diceA = (rand() % 6) + 1;
- diceB = (rand() % 6) + 1;
- diceC = (rand() % 6) + 1;
- printf("\nDice A: %d \n", diceA); // prints first random number.
- Sleep(1000); // pausing here for 1 second.
- printf("Dice B: %d \n", diceB); // prints second random number.
- Sleep(1000); // pausing here for 1 second.
- printf("Dice C: %d \n", diceC); // prints third random number.
- Sleep(1000); // pausing here for 1 second.
- diceSumFirstTime = diceA + diceB + diceC; // calculating sum of all dices.
- printf("Dice Sum: %d \n \n", diceSumFirstTime); // prints sum of all dices.
- Sleep(1000); // pausing here for 1 second.
- 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.
- scanf(" %s", answer); // gets input from user and stores it into char answer.
- answer[0] = toupper(answer[0]); // making first letter user input uppercase.
- if(answer[0] == 'H' || answer[0] == 'L' || answer[0] == 'S'){
- printf("Rolling for the second time. \n"); // prints message about rolling for the second time.
- Sleep(1000); // pausing here for 1 second.
- diceA = (rand() % 6) + 1;
- diceB = (rand() % 6) + 1;
- diceC = (rand() % 6) + 1;
- printf("\nDice A: %d \n", diceA); // prints first random number.
- Sleep(1000); // pausing here for 1 second.
- printf("Dice B: %d \n", diceB); // prints second random number.
- Sleep(1000); // pausing here for 1 second.
- printf("Dice C: %d \n", diceC); // prints third random number.
- Sleep(1000); // pausing here for 1 second.
- diceSumSecondTime = diceA + diceB + diceC; // calculating sum of all dices for the second time.
- printf("Dice Sum: %d \n \n", diceSumSecondTime); // prints sum of all dices for the second time.
- Sleep(1000); // pausing here for 1 second.
- if(diceSumFirstTime < diceSumSecondTime && answer[0] == 'H'){
- printf("Well done. Sum is higher then first roll. \n"); // if diceSumFirstTime is lower then diceSumSecondTime and answer is Higher this message is printed.
- }else if(diceSumFirstTime > diceSumSecondTime && answer[0] == 'L'){
- printf("Well done. Sum is lower then first roll. \n"); // if diceSumFirstTime is higher then diceSumSecondTime and answer is Lower this message is printed.
- }else if(diceSumFirstTime == diceSumSecondTime && answer[0] == 'S'){
- 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.
- }else{
- printf("Just unlucky. \n");
- }
- }else{
- printf("Maybe try next time. \n \n"); // message that prints to user if he doesn't enter correct option.
- }
- }else{
- printf("Maybe try next time. \n \n"); // message that prints to user if he doesn't enter correct option.
- }
- system("pause"); // pausing program after executing.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement