Not sure why game is exiting

I'm creating a game of go fish, but for some reason it is exiting after the computer's turn. Could someone give a hint or solution for why the program exits? I'm including the entirety of my code- Thanks :)

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
// Daniel Gruber
// Fall 2013

// This will be a "Go Fish!" card game where one player is playing against a computer.
// The game is over when someone runs out of cards, or there is no cards to pick up from the "ocean".


#include <iostream>
using namespace std;

const int SIZE = 32; // 32 cards in the deck
int ocean[SIZE];	 // The cards to pick from
int oceanPos;		 // What number in the ocean array you are on

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& postion, int& computerHandSize, int& humanHandSize, int& score);

int main()
{
	char yesOrNo = 'y'; // allows multiple games to be played
	while (yesOrNo == 'y' || yesOrNo == 'Y') // just incase caps lock is on!
	{
		// create deck
		initialize(ocean);

		// shuffle deck
		shuffle(ocean, oceanPos);

		int humanHand[50]; // more than enough room
		int humanHandSize = 0;
		int computerHand[50];
		int computerHandSize = 0;
		int drawn;
		int humanScore = 0;
		int computerScore = 0; // initializes scores = 0
		int i;

		for (i = 0; i < 5; i ++)
			drawCard(ocean, oceanPos, humanHand, humanHandSize, drawn);			// creates human hand

		for (i = 0; i < 5; i++)
			drawCard(ocean, oceanPos, computerHand, computerHandSize, drawn);	// creates human hand
		
		//uncomment these lines to check what is in each person's hand
		//for(i = 0; i < humanHandSize; i++)
			//cout << humanHand[i] << " ";   

		//for(i = 0; i < computerHandSize; i++)
			//cout << computerHand[i] << " computer ";

		bool breaking = false;
		while (humanHandSize !=0 && computerHandSize != 0 && !breaking)
		{
			
			humanTurn(ocean, humanHand, computerHand, oceanPos, humanHandSize, computerHandSize, humanScore);

			if(computerHandSize == 0 || oceanPos == 31) // out of cards!
			{
				breaking = true;					  // exits loop
				cout << "Game Over!" << endl;
			}

			// the computer's turn
			computerTurn(ocean, computerHand, humanHand, oceanPos, computerHandSize, humanHandSize, computerScore);
			
			if (humanHandSize == 0 || oceanPos == 31) // out of cards
			{
				breaking = true;
				cout << "Game Over!" << endl;
			}
			

		}
		cout << "The final score was.. " << endl;
		cout << "You: " << humanScore << endl;
		cout << "Computer: " << computerScore << endl;
		cout << "Play again? (y/n) ";
		cin >> yesOrNo;
	}

}

// A function to create a 32 card deck
void initialize (int deck[])
{
	for (int i = 0; i < SIZE; i ++)
		deck[i] = 2 + (i % 8); // cycles through 2-9, then back again
}

// shuffles the values in the array around, so they are out of order
void shuffle(int deck[], int& position)
{
	int i;
	int j;
	int temp; // placeholder
	for (i = 0; i < SIZE; i++)
	{
		j = 1 + rand() % 32;
		temp = deck[i];
		deck[i] = deck[j];
		deck[j] = temp;
	}
	position = 0; // sets position to the "top" of the deck
}


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

// a function to add up scores
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;
}

// takes cards from one hand and places them into the other player's hand
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];
		}
	}
}

// identifies if there is a set of cards, and if so, removes those cards
// and scores a point for a player.
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 << " completed a set!" << endl;
	}
}

// The player's turn
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;						// an alternative to a "break" we learned in class
	while(count > 0 && !breaking)
	{
		int i;
		cout << "You have: ";
		for (i = 0; i < humanHandSize; i++)
			cout << humanHand[i] << " ";		// outputs all cards in the player's hand
		cout << endl;

		int guess;
		cout << "You guess if I have any ";
		cin >> guess;							// the guessed card
		cout << endl;

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

		if (count > 0)						    // they have the requested card
			cout << "Yes I do. I have " << count << "." << endl;
		
		checkSet(guess, humanHand, humanHandSize, score, noun); // checks to see if the computer has any cards

		if (computerHandSize == 0)				// the computer has no cards left
			breaking = true;					// exits the loop
	}

	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;
		}
	}
}

// Now the computer's turn, very similar to the above function
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; // if bool breaking = true, exits loop.
	while (count > 0 && !breaking)
	{
		int i;
		cout << "You have: ";
		for(i = 0; i < humanHandSize; i++)
			cout << humanHand[i] << " "; // outputs what is in the player's hand
		cout << endl;

		int guess = (2 + rand() % 8); // a random number between 2-9
		cout << "I ask if you have any " << guess << "'s?" << endl;
		cout << endl;

		count = countRank(guess, humanHand, humanHandSize); // checks the human hand
		stealCards(guess, humanHand, humanHandSize, computerHand, computerHandSize); // if it is a match, the card is transferred

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

		checkSet (guess, computerHand, computerHandSize, score, noun); // checks for 4 of a kind

		if(humanHandSize = 0)
			breaking = true; // no cards left, exits loop.
	}
	if (count == 0) // no match!
	{
		cout << "You say: 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;
			
		}
	}
}
	
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
// Now the computer's turn, very similar to the above function
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; // if bool breaking = true, exits loop.
	while (count > 0 && !breaking)
	{
		int i;
		cout << "You have: ";
		for(i = 0; i < humanHandSize; i++)
			cout << humanHand[i] << " "; // outputs what is in the player's hand
		cout << endl;

		int guess = (2 + rand() % 8); // a random number between 2-9
		cout << "I ask if you have any " << guess << "'s?" << endl;
		cout << endl;

		count = countRank(guess, humanHand, humanHandSize); // checks the human hand
		stealCards(guess, humanHand, humanHandSize, computerHand, computerHandSize); // if it is a match, the card is transferred

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

		checkSet (guess, computerHand, computerHandSize, score, noun); // checks for 4 of a kind

		if(humanHandSize = 0) /////***************************////// should be humanHandSize == 0
			breaking = true; // no cards left, exits loop.
	}
	if (count == 0) // no match!
	{
		cout << "You say: 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;
			
		}
	}
}
Last edited on
Topic archived. No new replies allowed.