• (2 Pages)
• 1
• 2

Reputation:

# Go fish game help

Posted 15 November 2010 - 12:18 PM

I'm trying to write a Go Fish game in C++. I'm not a compsci major, I don't have much programming experience at all. The program involves simulated the go fish card game. I need major help with my shuffle function and the passing of cards between player's hands which are represented as arrays. We started arrays ~2 weeks ago so 4 classes maybe, and I don't have much experience with them. If anyone could take a look at my program and functions so far and give me some pointers it would be much appreciated. Thanks much.

```#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void deckShuff(ocean[]){ // shuffles the deck

int position = 0;
int i = 0;
int n = 1 + rand() % 32;
while (i < 31){
ocean[i] = ocean[n];
i = i +1;
}

}

void drawCard( const int ocean[31], int& position, const int[]){ // simulates the drawing of a card from the deck
playerHand[] = playerHand[] + ocean[position]
ocean[31] = ocean[31] - ocean[position];
position = position +1;

}

int ocean[35] = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9}; // arrays for values
int playerHand[20];
int computerHand[20];
int position = 0;

void checkcards(const int[], int rank, int size) { // checks hand for how many of a specific type of card
int i = 0;
int rankcount = 0;
while (i < size)
if (playerHand[i] ==rank)
rankcount = rankcount + 1;
i = i+1;
}

}

void checkforset(const int[], int rank, int size, int points) { // checks a players hand for a set of cards
int i = 0;

int rankcount = 0;
while (i < size)
if (playerHand[i] ==rank)
rankcount = rankcount + 1;
if (rankcount == 4){
points = points + 1;
cout << "A set of " << rank << "s was in the hand.  A point has been scored" << endl;
}
i = i+1;
}
}

void askforcards(const int[], int rank, int size, const int[], int size2) { // simulates the trading of cards between players
int i = 0;
while (i < size){
if computerHand[i] == rank){
playerHand[size2] = playerHand[size2] + computerHand[i];
computerHand[size] = computerHand[size] - computerHand[i];
}
else if computerHand[i] != rank){
cout << "Go Fish!" << endl;
drawCard
}
i = i +1;

}
}

int main(){
int start = 0;
int choice = 0;
while (choice == 1){
cout << "Welcome to Go Fish!.  Press 1 to play "<< endl;
cin >> start;
while (start != 1){
cout << "Press 1 to play " << endl;
cin >> start;

}
deckShuff(ocean[]);
int n = 0; // counters for loops
int y = 0;
while (n <= 5){
drawCard(ocean[], position, playerHand[]);
drawCard(ocean[], position, computerHand[]);
n = n+1;
}

cout << "Would you like to play again? (1 = yes, 0 = no) " << endl;
cin >> choice;
}

}
```

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 15 November 2010 - 12:21 PM

Is This A Good Question/Topic? 0

## Replies To: Go fish game help

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,677
• Joined: 08-August 08

## Re: Go fish game help

Posted 15 November 2010 - 12:29 PM

```void deckShuff(ocean[]){ // shuffles the deck
int position = 0;
int i = 0;
int n = 1 + rand() % 32;
while (i < 31){
ocean[i] = ocean[n];
i = i +1;
}
}

```

You're trying to swap ocean[i] with ocean[n] but you're not retaining ocean[i] before you overwrite it. Use a temporary variable to hold it, then after copying ocean[n] to ocean[i] replace ocean[n] with your temporary variable.

Oh, and notice how I've remove the unnecessary white space. Too much is as bad as too little!

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 12:41 PM

I'm not really sure if I understand. I'm trying to shuffle the deck by generating a random number to randomize the order of my deck array (ocean). So you're saying I should first put the ordered deck into a temporary value, then switch in the randomized numbers, and then put the ordered deck back in?

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,677
• Joined: 08-August 08

## Re: Go fish game help

Posted 15 November 2010 - 12:55 PM

Imagine you have two cards on a table and you want to swap their positions, but you can only pick up one at a time. How would you do it? It seems to me that you would need a third, temporary position to put the first card so that you can move the second into the first card's original position. Then you could move the first card into the second card's original position.

One more thing:
You are only setting the random number "n" one time. I'd move that into the loop.

Decks of cards usually have 52, at least where I'm from.

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 12:57 PM

Ok that makes sense. Thanks a lot. Do my other functions look alright? They are supposed to draw cards from the deck, take cards from players hands, and check hands to see if there are any sets(4 of a kind) in the hands.

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,677
• Joined: 08-August 08

## Re: Go fish game help

Posted 15 November 2010 - 01:01 PM

Don't try to run before you can walk. In fact, when you think you can run, walk anyway! Get this function working, then think about the next.

### #7 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: Go fish game help

Posted 15 November 2010 - 01:07 PM

CTphpnwb, on 15 November 2010 - 01:55 PM, said:

Decks of cards usually have 52, at least where I'm from.

Well, Tarot decks have 56 cards in the Minor Arcana. Plays a mean solitaire, until someone dies...

Right; the magic number 32, should be 52. Magic numbers are bad. In your code make a const:
```const int DECK_SIZE = 52;

```

Wherever you have 52, use DECK_SIZE. It will help avoid confusion. As you deal from your desk, it will have fewer cards. You might want something like this:

```struct Deck {
Card cards[DECK_SIZE];
int cardNum;
};

```

The type of Card is up to you. Can be just an int, or a struct with properties.

A deck really lends itself to OO programming, if you've started using classes.

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 01:07 PM

Alright I'll try to work this out and be back later. Thanks again.

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 02:54 PM

Ok I got the shuffle function working fine. The size of the deck is 32 for this program. We're using a shortened deck (only the numerical card values) to simplify the game. My problem right now is all the transactions of cards between arrays. Since I'm not that experienced with arrays I'm not really sure how you take one entry from one array and give it to another.

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 02:57 PM

Oh also for functions for arrays. Do you just set the parameter to a generic array title and then when ever that title is used within the function will the passed array be used in its place?

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,677
• Joined: 08-August 08

## Re: Go fish game help

Posted 15 November 2010 - 03:35 PM

Arrays are passed by reference.
What baavgai was saying is that you should do something like:

#define DECK_SIZE 32
or

const int DECK_SIZE = 52;

So that you can use DECK_SIZE in your code and all you need to do is change the definition in one place to change it everywhere in the code. In addition, looking at DECK_SIZE is easier to understand than some seemingly random number. That makes editing much easier.

If you've got it working, post what you have along with a description of where you're stuck.

Reputation:

## Re: Go fish game help

Posted 15 November 2010 - 03:48 PM

I have these functions working right now:

```void deckShuff (int ocean[]) {

int position = 0;
int f = 0;
int i = 0;
int n;
while (i < 31){
n = 1 + rand() % 32;
ocean[f] = ocean[i];
ocean[i] = ocean[n];
ocean[n] = ocean[f];
i = i +1;
}

}
```

```void checkcards( int data[], int rank, int size) { // checks hand for how many of a specific type of card
int i = 0;
int rankcount = 0;
while (i < size)
if (data[i] ==rank)
rankcount = rankcount + 1;
i = i+1;
}
```

I'm stuck on functions like these that need to pass information from one array to another:
```void drawCard( int ocean[], int& position, int data[], int size) { // simulates the drawing of a card from the deck
data[size] = data[size] + ocean[position];
ocean[31] = ocean[31] - ocean[position];
position = position +1;
```

```void askforcards( intdata[], int rank, int size, int data2[], int size2) { // simulates the trading of cards between players
int i = 0;
while (i < size){
if data[i] == rank){
data2[size2] = data2[size2] + data[i];
data[size] = data[size] - data[i];
}
else if data[i] != rank){
cout << "Go Fish!" << endl;
drawCard
}
i = i +1;

}
}
```

Sorry for the formatting :x

This post has been edited by JackOfAllTrades: 15 November 2010 - 04:19 PM
Reason for edit:: Fixed code tags.

### #13 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,677
• Joined: 08-August 08

## Re: Go fish game help

Posted 15 November 2010 - 03:58 PM

The code tag icon looks like: <>

### #14 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Go fish game help

Posted 15 November 2010 - 03:58 PM

radicaledward, on 16 November 2010 - 07:48 AM, said:

Sorry for the formatting :x

Don't be sorry, be better.
The code goes between the code tags. Like this
Stop putting the opening tag and closing tag at the start of the code.
One goes before the code. One goes after the code.
Not a highly challenging concept really. Give it a try.
Make use of the "Preview" button to see if you got it right before posting.

Reputation: 0
• Posts: 22
• Joined: 15-November 10

## Re: Go fish game help

Posted 15 November 2010 - 04:11 PM

well sorry
```void deckShuff (int ocean[]) {

int position = 0;
int f = 0;
int i = 0;
int n;
while (i < 31){
n = 1 + rand() % 32;
ocean[f] = ocean[i];
ocean[i] = ocean[n];
ocean[n] = ocean[f];
i = i +1;
}

}
```

```void checkcards( int data[], int rank, int size) { // checks hand for how many of a specific type of card
int i = 0;
int rankcount = 0;
while (i < size){
if (data[i] ==rank)
rankcount = rankcount + 1;
i = i+1;
}

}
```

```    void askforcards( intdata[], int rank, int size, int data2[], int size2) { // simulates the trading of cards between players
int i = 0;
while (i < size){
if data[i] == rank){
data2[size2] = data2[size2] + data[i];
data[size] = data[size] - data[i];
}
else if data[i] != rank){
cout << "Go Fish!" << endl;
drawCard(ocean, position, data2, size);
}
i = i +1;

}
}

```

```
void drawCard( int ocean[], int& position, int data[], int size) { // simulates the drawing of a card from the deck
data[size] = data[size] + ocean[position];
ocean[31] = ocean[31] - ocean[position];
position = position +1;

}
```

The top two are functions I have working
The bottom two are functions I need help with.
Any help is much appreciated.