Dice Game Program

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");
}
Last edited on
The last if..else statement should be inside your loop. You should only need one loop for this as well, and you don't need a switch.

edit: I've gotten it working for myself. It's pretty cool. I can confirm that only one loop is necessary, and I didn't use a switch.
Last edited on
With a glance I see three problems.

1. Even if a player wins on the first roll,the second loop still executes,this could turn what would be a winner into a loser.

2.The totaldice variable is not reset to zero in the second loop. So the sum of totaldice can easily exceed 12.

3.printf("You are a loser\n"); Harsh words.
Last edited on
Why do you have two do/while loops? Regardless of the outcome of the first, the second will also run which I hope wasn't your intention. You were told how to find the solution:
Run it a few times watching the behavior in the debugger until you are convinced that it is playing by the rules.

if you do that a "few times" like suggested, you should be able to figure out what's wrong like if you do this.


1
2
3
4
5
6
7
8
9
10
11
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);    comment out for debugging
                */

		totalDice = 2 //replace with 2 for debug purposes  // rollADie1 + rollADie2;
		printf("The total of both dice is %i\n", totalDice);

2 on the first roll should loose and end the turn according to the requirements you stated.
The last if..else statement should be inside your loop. You should only need one loop for this as well, and you don't need a switch.

edit: I've gotten it working for myself. It's pretty cool. I can confirm that only one loop is necessary, and I didn't use a switch.

if you did not sure the switch what did you do?
Yeah I'm still confused. Ive been debugging this thing over and over again and I'm lost

Normally Id always recommend using Switch where there's tons of IF statements but in your case I think IF's would be more suitable.

Try this..

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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	int dice1Roll, dice2Roll, key, total = 0;
	bool won = false, playing = true;

	// set seed.
	srand(time(0));

	dice1Roll = rand() % 6 + 1;	// 1 to 6
	dice2Roll = rand() % 6 + 1;	// 1 to 6
	total = dice1Roll + dice2Roll;
	printf("You rolled %d\n", total);

	if (total == 7 || total == 11)  // ive won
		won = true;
	else
		if (total == 2 || total == 3 || total == 12) // ive lost
			won = false;
		else
			if (total >= 4 && total <= 6 || total >= 8 && total <= 10) {  // store as key
				key = total;
				printf("Setting key to %d\n\n", key);
				do
				{
					dice1Roll = rand() % 6 + 1;	// 1 to 6
					dice2Roll = rand() % 6 + 1;	// 1 to 6
					total = dice1Roll + dice2Roll;
					printf("Rolling.... %d\n", total);
					if (total == key) {
						won = false;
						playing = false;
					}
					else
						if (total == 7) {
							won = true;
							playing = false;
						}
				} while (playing);
			}

	if (won)
		printf("\n\nWell Done!! - you are a winner!");
	else
		printf("\n\nBad Luck!! - you lost!");

	return 0;
}
You rolled 4
Setting key to 4

Rolling.... 9
Rolling.... 2
Rolling.... 6
Rolling.... 4


Bad Luck!! - you lost!
Topic archived. No new replies allowed.