I have been writing this code for a little while now and have most if it working correctly except for one. I have add the actual assignment below but what i'm having a problem with is on the first role if it is a 2,3,7,11,12 then the game ends in either a win or loss with those number. However mind keeps going. Please help me out
Assignment: In DrA’s dice game, you use two dice. On the first roll of the dice the player immediately wins when the roll is 7 or 11, and loses when the roll is 2, 3, or 12. (See below for code to doll a die.)
If 4, 5, 6, 8, 9, or 10 is rolled that number becomes the “key."
The player keeps rolling the dice until either 7 or the key is rolled. If the key is rolled first, then the player loses. If the player rolls a 7 first, then the player wins. (There is a loop here.)
Write a program that plays the game once, using the rules stated above. There is no user input. Run it a few times watching the behavior in the debugger until you are convinced that it is playing by the rules.
Instead of asking for a wager, the program should just determine if the player would win or lose. The program should simulate rolling the two dice and calculate the sum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main() {
int rollADie1, rollADie2, totalDice, key = 0, key2 = 0, end = 0;
srand(time(NULL)); // call this only once – seed the random generator
do
{
rollADie1 = rand() % 6 + 1; // in the range of 1 - 6
rollADie2 = rand() % 6 + 1; // in the range of 1 - 6
printf("Dice #1 is %i\n", rollADie1);
printf("Dice #2 is %i\n", rollADie2);
totalDice = rollADie1 + rollADie2;
printf("The total of both dice is %i\n", totalDice);
switch (totalDice) {
case 2:
end = 2;
break;
case 3:
end = 3;
break;
case 4:
key = 4;
break;
case 5:
key = 5;
break;
case 6:
key = 6;
break;
case 7:
end = 7;
break;
case 8:
key = 8;
break;
case 9:
key = 9;
break;
case 10:
key = 10;
break;
case 11:
end = 11;
break;
case 12:
end = 12;
break;
}
} while (totalDice != key);
do
{
rollADie1 = rand() % 6 + 1; // in the range of 1 - 6
rollADie2 = rand() % 6 + 1; // in the range of 1 - 6
printf("Dice #1 is %i\n", rollADie1);
printf("Dice #2 is %i\n", rollADie2);
totalDice = rollADie1 + rollADie2;
printf("The total of both dice is %i\n", totalDice);
switch (totalDice) {
case 2:
printf("Roll Again\n");
key2 = 2;
break;
case 3:
printf("Roll Again\n");
key2 = 3;
break;
case 4:
key = 4;
break;
case 5:
key = 5;
break;
case 6:
key = 6;
break;
case 7:
printf("You Win\n");
break;
case 8:
key = 8;
break;
case 9:
key = 9;
break;
case 10:
key = 10;
break;
case 11:
printf("Roll Again\n");
key2 = 11;
break;
case 12:
printf("Roll Again\n");
key2 = 12;
break;
}
} while (totalDice == key2);
if (totalDice == 7) {
printf("You are the Winner\n");
}
else
{
if (totalDice == key);
printf("You are a Loser\n");
}
printf("Thank you for Playing\n");
system("pause");
}
|