Trouble with C++ Go Fish game

I believe I have most of the code working for a Go Fish program. Right now there are two functions that I believe to be the problem. Any help would be appreciated.

The first function (stealCards) is supposed to take away the other person's card if they have the same one you are asking for. The problem occurs when the other person has more than one of those cards as I want all of that type of card to transfer but only one of them does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize )
{
	int i;
	for ( i = 0; i < fromSize; i++ )
	{
		if ( from[i] == rank )
		{
			toSize++;
			to[toSize-1] = rank;
			from[i] = from[fromSize-1];
			fromSize--;
		}
	}
}


The other function I am having problems with (checkSet) is supposed to check if a player has 4 of the same type of card. If they do it, should remove those four cards and add 1 to the players score. When the function is called and there are four types of the card, it only removes three of them. I'm not sure if it is related but when tested, the one it didn't remove was the first entry in the hand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] )
{
	int count = countRank( rank, hand, handSize );
	int i;
	if ( count == 4 )
	{
		for ( i = 0; i < handSize; i++ )
		{
			if ( hand[i] == rank )
			{
				hand[i] = hand[handSize - 1];
				handSize--;
			}
		}
	score++;
	cout << pronoun << " scored a set!" << endl;
	}
}


I believe the problem lies in these two functions but I am not sure so i attached the entire code as well

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Go Fish
// Written by Neil Munroe
// 4/8/2013

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int SIZE = 32;
int ocean[SIZE];
int oceanPos;

void shuffle( int deck[], int& position );
void initialize( int deck[] );
void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn );
int countRank( int rank, int hand[], int handSize );
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize );
void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] );
void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score );
void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score );

int main()
{
	char yesOrNo = 'y';
	while ( yesOrNo == 'y' || yesOrNo == 'Y' )
	{
		// initialize deck
		initialize ( ocean );

		// shuffle deck
		shuffle( ocean, oceanPos );

		// initial deal
		int humanHand[50];
		int humanHandSize = 0;
		int computerHand[50];
		int computerHandSize = 0;
		int drawn;
		int humanScore = 0;
		int computerScore = 0;
		int i;

		for ( i = 0; i < 5; i++ )
		{
			drawCard( ocean, oceanPos, humanHand, humanHandSize, drawn );
		}

		for ( i = 0; i < 5; i++ )
		{
			drawCard( ocean, oceanPos, computerHand, computerHandSize, drawn );
		}

		for ( i = 0; i < humanHandSize; i++ )
		{
			cout << humanHand[i] << " ";
		}

		for ( i = 0; i < computerHandSize; i++ )
		{
			cout << computerHand[i] << " ";
		}

		bool breaking = false;
		while ( oceanPos != 3 && humanHandSize != 0 && computerHandSize != 0 && !breaking )
		{
			// human turn
			humanTurn ( ocean, humanHand, computerHand, oceanPos, humanHandSize, computerHandSize, humanScore );
			if ( computerHandSize == 0 || oceanPos == 31 )
			{
				breaking = true;
				cout << "Game Over." << endl;
			}
			else
			{
				//computer turn
				computerTurn ( ocean, computerHand, humanHand, oceanPos,computerHandSize, humanHandSize, computerScore );
			}
			if ( humanHandSize == 0 || oceanPos == 31 )
			{
				breaking = true;
				cout << "Game Over." << endl;
			}
		}
		cout << "*Final Score*" << endl;
		cout << "You: " << humanScore << endl;
		cout << "Computer: " << computerScore << endl;
		cout << "Would you like to play again? (y/n) ";
		cin >> yesOrNo;
	}


}

void initialize ( int deck[] )
{
	int i;
	for ( i = 0; i < SIZE; i++ )
	{
		deck[i] = 2 + ( i % 8 );
	}
}

void shuffle ( int deck[], int& position )
{
	int i;
	int j;
	int temp;
	for ( i = 0; i < SIZE; i++ )
	{
		j = 1 + rand() % 32;
		temp = deck[i];
		deck[i] = deck[j];
		deck[j] = temp;
	}
	position = 0;
}

void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn )
{
	handSize++;
	hand[handSize-1] = deck[position];
	drawn = hand[handSize-1];
	position++;
}

int countRank ( int rank, int hand[], int handSize )
{
	int i;
	int count = 0;
	for ( i = 0; i < handSize; i++ )
	{
		if ( hand[i] == rank )
		{
			count++;
		}
	}

	return count;
}

void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize )
{
	int i;
	for ( i = 0; i < fromSize; i++ )
	{
		if ( from[i] == rank )
		{
			toSize++;
			to[toSize-1] = rank;
			from[i] = from[fromSize-1];
			fromSize--;
		}
	}
}

void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] )
{
	int count = countRank( rank, hand, handSize );
	int i;
	if ( count == 4 )
	{
		for ( i = 0; i < handSize; i++ )
		{
			if ( hand[i] == rank )
			{
				hand[i] = hand[handSize - 1];
				handSize--;
			}
		}
	score++;
	cout << pronoun << " scored a set!" << endl;
	}
}

void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score )
{
	int count = 1;
	char noun[] = "You";
	bool breaking = false;
	while ( count > 0 && !breaking )
	{
		int i; 
		cout << "You have: ";
		for ( i = 0; i < humanHandSize; i++ )
		{
			cout << humanHand[i] << " ";
		}
		cout << endl;

		int guess;
		cout << "You ask if I have any ";
		cin >> guess;
		cout << endl;

		count = countRank ( guess, computerHand, computerHandSize );
		stealCards ( guess, computerHand, computerHandSize, humanHand, humanHandSize );

		if ( count > 0 )
		{
			cout << "Yes I do. I have " << count << endl;
		}

		checkSet ( guess, humanHand, humanHandSize, score, noun );
		
		if ( computerHandSize == 0 )
		{
			breaking = true;
		}
	}
	
	if ( count == 0 )
	{
		cout << "No I don't. Go Fish!" << endl;
		int drawn;
		drawCard ( deck, position, humanHand, humanHandSize, drawn );
		cout << "You draw a " << drawn << "." << endl;
		cout << endl;

		checkSet ( drawn, humanHand, humanHandSize, score, noun );

		if ( position != 0 )
		{
			cout << "My turn!" << endl;
			cout << endl;
		}
	}
}

void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score )
{
	int count = 1;
	char noun[] = "I";
	bool breaking = false;
	while ( count > 0 && !breaking )
	{
		int i; 
		cout << "You have: ";
		for ( i = 0; i < humanHandSize; i++ )
		{
			cout << humanHand[i] << " ";
		}
		cout << endl;

		int guess = 2 + rand() % 8;
		cout << "I ask: Do you have any " << guess << "'s?" << endl;
		cout << endl;

		count = countRank ( guess, humanHand, humanHandSize );
		stealCards ( guess, humanHand, humanHandSize, computerHand, computerHandSize );

		if ( count > 0 )
		{
			cout << "Yes you do. You have " << count << endl;
		}

		checkSet ( guess, computerHand, computerHandSize, score, noun );

		if ( humanHandSize == 0 )
		{
			breaking = true;
		}
	}
	if ( count == 0 )
	{
		cout << "No you don't. I will Go Fish." << endl;
		int drawn;
		drawCard ( deck, position, computerHand, computerHandSize, drawn );

		checkSet ( drawn, computerHand, computerHandSize, score, noun );

		if ( position != 0 )
		{
			cout << "Your turn" << endl;
			cout << endl;
		}
	}
}
There is a problem with the logic of your code, which may be causing those problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize )
{
	int i;
	for ( i = 0; i < fromSize; i++ )
	{
		if ( from[i] == rank )
		{
			toSize++;
			to[toSize-1] = rank;
			from[i] = from[fromSize-1];
			fromSize--;
		}
	}
}


Let's say that there are 5 cards in the "from" hand: 2 4 4 4 7
Let's say that there are 5 cards in the "to" hand: 4 5 8 8 9
And let's say that rank is 4.

So going through the loop, nothing happens when i is 0. What happens when i is 1? You can ignore the "to" hand, because there's nothing wrong with that part.

1
2
from[i] = from[fromSize-1]; //from[1] = from[4], which is 7
fromSize--; //fromSize = 4 


Now what happens when i is 2?

1
2
from[i] = from[fromSize-1]; //from[2] = from[3], which is 4
fromSize--; //fromSize = 3 


Now what happens when i is 3? The loop doesn't run, because 3 is not smaller than fromSize, they're equal.

So what are the two hands like right now?
The "from" hand: 2 7 4
The "to" hand: 4 5 8 8 9 4 4

You should change your code so that you start iterating from the last card in your hand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize )
{
	int i;
	for ( i = fromSize - 1; i >= 0; i-- )
	{
		if ( from[i] == rank )
		{
			to[toSize] = rank;
			toSize++;
			fromSize--;
			from[i] = from[fromSize];
		}
	}
}


You might want to check the loops in the other functions too.
Last edited on
Topic archived. No new replies allowed.