Problem with Game Class Project

The final project in my computer programming class is to create a simple game using C++. Unfortunately most of the semester was spent on a program called Alice instead of C++. This make me a beginner, and I really need some help.

My game is basically a simpler version of Yahtzee. Instead of adding in things like straights and full houses, it just adds up the number of repeated dice values you get after each roll. I tried to create two arrays, one for each player's set of dice, and have them generate random values. I then tried to print out said values, but that isn't working. I tried doing research and getting help from my older brother studying to be a computer engineer, but the internet searches weren't helpful and my brother is having trouble making time to help me.

This is what I have so far. All help will be appreciated.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>



using namespace std;

int main ()
{

	int diceRoll1 [5];
	int diceRoll2 [5];

	int ctr1 = 0;
	int ctr2 = 0;


    string y;
    string r;
   

   

    cout << "Do you want to play? Type in Yes or No: ";
    cin >> y;

    while ( y != "No" )
    {

    if ( y != "No" )
    {
    cout << "-*-*-*-*-Welcome to Dice Poker!-*-*-*-*-" << endl;
    cout << "The object of this game is similar to 5 card draw and Yahtzee:" << endl;
    cout << "Try to get as many dice with the same" << endl;
    cout << "number face up as you can. Type Roll to play: " << endl;
    cin >> r;
    }
    if ( r == "Roll" )
    {
		    for ( ctr1=0, ctr1 <=5, ctr1++;; )
		{		
			srand ((unsigned)time(0));
			diceRoll1[ctr1] = (rand()%6)+1;
        }
			cout << diceRoll1[0] << endl;
			cout << diceRoll1[1] << endl;
			cout << diceRoll1[2] << endl;
			cout << diceRoll1[3] << endl;
			cout << diceRoll1[4] << endl;          

    }



    }

    system("PAUSE");
    return 0;
}
To start, your srand() is getting called more then once. You want to just seed the random function at the beginning of the program and leave it at that. Also your for loop syntax is slightly off. I fixed it for you, look at 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
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>



using namespace std;

int main ()
{
	srand ((unsigned)time(0));

	int diceRoll1 [5];
	int diceRoll2 [5];

	int ctr1 = 0;
	int ctr2 = 0;


    string y;
    string r;
   

   

    cout << "Do you want to play? Type in Yes or No: ";
    cin >> y;

    while ( y != "No" )
    {

    if ( y != "No" )
    {
    cout << "-*-*-*-*-Welcome to Dice Poker!-*-*-*-*-" << endl;
    cout << "The object of this game is similar to 5 card draw and Yahtzee:" << endl;
    cout << "Try to get as many dice with the same" << endl;
    cout << "number face up as you can. Type Roll to play: " << endl;
    cin >> r;
    }
    if ( r == "Roll" )
    {
		    for ( ctr1=0; ctr1 <=5; ctr1++ )
		{		
			
			diceRoll1[ctr1] = (rand()%6)+1;
        }
			cout << diceRoll1[0] << endl;
			cout << diceRoll1[1] << endl;
			cout << diceRoll1[2] << endl;
			cout << diceRoll1[3] << endl;
			cout << diceRoll1[4] << endl;          

    }



    }

    system("PAUSE");
    return 0;
}

Your for loop runs 6 times instead of 5. Your while loop is currently an infinite loop because there is no way to change y inside the loop.
Thanks kartracer, that fixed THAT problem...but now that I've continued, I'm having another. I guess I'm not too good at this.

I wrote some code that counts the number of similar dice values, and now I'm trying to get it to determine a winner. I wrote three if statements, one for if player one's doubles value is greater, one for if player two's doubles value is greater, and one for if they are equal. Because of some fault on my part (obviously), all three events happen every time. Help would be appreciated!

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>



using namespace std;

int main ()
{
	srand ((unsigned)time(0));

	int diceRoll1 [5];
	int diceRoll2 [5];

	int ctr1 = 0;
	int ctr2 = 0;
	int doubles = 0;
	int doubles2 = 0;
	int i = 0;
	int j = 0;
	int i2 = 0;
	int j2 = 0;

	int wins = 0;
	int wins2 = 0;

	string s;
    string y;
    string r;
	string y2;
	string r2;
   

   

    cout << "Do you want to play? Type in Yes or No: ";
    cin >> y;

    while ( y != "No" )
    {

    if ( y != "No" )
    {
    cout << "-*-*-*-*-Welcome to Dice Poker!-*-*-*-*-" << endl;
    cout << "The object of this game is similar to 5 card draw and Yahtzee:" << endl;
    cout << "Try to get as many dice with the same" << endl;
    cout << "number face up as you can." << endl;
	cout << "Type in 'Start' to begin or 'Quit' to quit: " << endl;
    cin >> s;

	if ( s == "Start" )
	{
		cout << "Player one, type in 'Roll' to start: ";
		cin >> r;
    }
    if ( r == "Roll" )
    {
		    for ( ctr1=0; ctr1 <=5; ctr1++ )
		{		
			
			diceRoll1[ctr1] = (rand()%6)+1;
        }
			cout << diceRoll1[0] << endl;
			cout << diceRoll1[1] << endl;
			cout << diceRoll1[2] << endl;
			cout << diceRoll1[3] << endl;
			cout << diceRoll1[4] << endl;    
			cout << "Doubles found: " << endl;

			for(i = 0; i <= 4; i++)
		{
			
			for(j = 0; j <= 4; j++)
			{
				
				if (diceRoll1[i] == diceRoll1[j] && i != j)
				{		
					doubles++; 
				}
			}
		}

		cout << ("There were %d doubles.\n", doubles / 2) << endl;

    }

	cout << "Player two, type in 'Roll' to play or 'Quit' to quit: ";
	cin >> r2;

		if ( r2 == "Roll" )
		{
		
		for ( ctr2=0; ctr2 <=5; ctr2++ )
		{		
			
			diceRoll2[ctr2] = (rand()%6)+1;
        }
			cout << diceRoll2[0] << endl;
			cout << diceRoll2[1] << endl;
			cout << diceRoll2[2] << endl;
			cout << diceRoll2[3] << endl;
			cout << diceRoll2[4] << endl;    
			cout << "Doubles found: " << endl;

			for(i2 = 0; i2 <= 4; i2++)
		{
			
			for(j2 = 0; j2 <= 4; j2++)
			{
				
				if (diceRoll2[i2] == diceRoll2[j2] && i2 != j2)
				{		
					doubles2++; 
				}
			}
		}

		cout << ("There were %d doubles2.\n", doubles2 / 2) << endl;
		}

		if ( (doubles) > (doubles2) )
		{
			wins++;

			cout << "P1: "; 
			cout << wins << endl;   
			cout << "P2: ";
			cout << wins2 << endl;
		}

		if ( (doubles) < (doubles2));;
		{
			wins2++;

			cout << "P1: "; 
			cout << wins << endl;   
			cout << "P2: ";
			cout << wins2 << endl;
		}
		if ( (doubles) == (doubles2) );;
		{
			cout << "It's a tie!" << endl;
		}
	}

    }

    system("PAUSE");
    return 0;
}
You have extra ';' after your other two if statements. Also, you should use an if, else if chain instead of a bunch of ifs there (so you don't waste time checking doubles < doubles2 if doubles was already > doubles2.
Two final problems I have are keeping score. The score for player one is done correctly, but player two's score goes up whether it wins or loses.

Also, I inserted a bit of code that is supposed to determine a winner once a total score of 5 is met. I don't understand why, but it declares a winner (and not always right either) after only one round. Here is the bit of code that my problems are centered around:

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
		if ( doubles > doubles2 )
		{
			wins++;
		}

		
		
		else if ( doubles2 > doubles )
		 {
			wins2++;
		 }

		
		
		if ( doubles == doubles2 )
		{
			cout << "It's a tie!" << endl;
		}

			cout << "P1: "; 
			cout << wins << endl;   
			cout << "P2: ";
			cout << wins2 << endl;
		
		
		if ( wins == 5 )
		{
			cout << "Player one wins!" << endl;
			system("PAUSE");
		}

		else if ( wins2 == 5 )
		;{
			cout << "Player two wins!" << endl;
			system("PAUSE");
		 }
		
	}
Your else if (line 32) has a stray semicolon after it (33), so the program is always going to say 2P wins. Not sure about the wins being off though.
Last edited on
1
2
3
4
for ( ctr1=0; ctr1 <=5; ctr1++ )
{
          diceRoll1[ctr1] = (rand()%6)+1;
}
runs 6 times and writes data out of the array's boundary. Change it to < 5

1
2
3
4
5
6
7
8
9
10
for(i = 0; i <= 4; i++)
{
          for(j = 0; j <= 4; j++)
          {
                    if (diceRoll1[i] == diceRoll1[j] && i != j)
                   {
                             doubles++; 
                   }
          }
}
Runs more times than you need it to. Try this:
1
2
3
4
5
6
7
8
for(i = 0; i < 5 ; i++)
{
          for(j = i + 1 ; j < 5 ; j++)
          {
                    if (diceRoll1[i] == diceRoll1[j])
                              doubles++; //you don't need {} here since it's just a single statement
          }
}
That won't compare the same index or any previous indexes already compared. You won't need to divide doubles by 2. Adjust player 2 similarly. Also, i don't believe you need the {} with the fors either, but to me it makes loops with complex single statements a little more readable.

Make sure you set both players' doubles to 0 each round (unless your game keeps a running total of doubles and awards a win based on total doubles.)
Topic archived. No new replies allowed.