Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use c++ to write a program will simulate the card game Go Fish! . The game will

ID: 3532781 • Letter: U

Question

Use c++ to write a program will simulate the card game Go Fish!. The game will be a contest between a human player and one simulated by the computer program.

In this game, each player's goal is to collect sets of four cards of the same rank (such as four 8's). During a turn, a player will ask another if they have any cards of a particular rank equal to one they already hold. (e.g. "Do you have any 5's?").

If the asked player does have those cards, they must hand over all the cards of that rank. Otherwise, they would say "Go Fish!" and the asking player must draw a card from a deck of unclaimed cards (which this document will call the Ocean).

Here, for example, is some interaction from the instructor's solution:

There seems to be no universal agreement on how long the game lasts or how long the turn lasts. For this program, I will assume these rules:

Do give the user a chance to play again after one game has ended.

For this program, also, we will use only the 2's through 9's (using only a 32-card deck instead of all 52) for a faster game. Deal each player (human and computer) an initial hand of five cards.

Arrays will be required in this assignment -- it would be nearly impossible to make a readable program without them.

Arrays may serve in various different ways:

The order of cards in the hand does not matter in this game, so you have two reasonable choices to deal with this:

Drawing a card would simply add a card to the end of the array. But removing cards (either by making a set of four, or handing them to the opponent) would also want to preserve the values consecutively at the front of the array. Since order does not matter, any time a card is withdrawn 'from the middle', just replace it with the card at the bottommost part of the data, recording that there is one fewer card in the hand.

Shuffling a deck is simply an application of a random number generator. Initialize an array to a series of known values (i.e. known to have distinct values for each card); then visit each subscript in turn and exchange that array element with some element randomly chosen from anywhere in the array. Every card will have been moved somewhere -- and no one should have any idea where.

Shuffling would be a necessary first step to this program. (If you don't believe me, see how your program runs if you don't shuffle.)

Include card descriptions in the user interface to make it feel more like a card game, instead of just a bunch of numbers.

So instead of just '7' the user would see "Seven of Hearts", and "You draw the Five of Clubs".

Use constant arrays of strings for full credit (not If or Switch statements).

Of course, you have to make sure that all 32 cards are actually different, so you don't have two cards of the same rank and suit.

A useful trick would be to somehow encode both the rank and suit of card in a single integer value, using a scheme that is both easy to encode and to decode.

Explanation / Answer

void shuffle(int ocean[]){//working

int hold=0;

int i=0;

int n;

while ( i <31){

n= 1 + rand() % 32;

ocean[hold] = ocean[i];

ocean[i] = ocean[n];

ocean[n] = ocean[hold];

i=i+1;

}

}




int drawCard (int ocean[], int hand[], int& deckPos, int& handPos)

{

hand[handPos]=ocean[deckPos];

deckPos=deckPos + 1;

handPos = handPos + 1;

return deckPos;

return handPos;


}



void pairCount (int hand1[], int hand1Pos){

int count[11]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //enough room to count all the occurences from 2 to 9

int temp;

for (int i=0; i<hand1Pos; i++){

temp=hand1[i];

count[temp]++;

}

int x=2;

while (x<9){

if (count[x]==4){

cout << "Game Over! Four occurences of " << x << " have occured " << endl;

x=10;}//ends loop because a winner was found

else {

x++;}// if no winner is found at count[x], increment the counter

}

}



int cardCheck (int guess, int hand1[], int& hand1Pos, int hand2[], int& hand2Pos, int ocean[], int& deckPos){



for (int i=0; i<hand2Pos; i++){//checking computer's hand for player's guess

if (hand2[i]==guess){

cout << "I do. I have one " << endl; //still need to figure out how to x occurences (not just one)

hand1Pos++; //if the guess is in the opponent's hand, increment player's hand to prepare for the card to be added

hand1[hand1Pos-1]=guess; //add the guess to player's hand

hand2[i]=hand2[hand2Pos-1];

hand2Pos--;

return hand1Pos, hand2Pos, deckPos;

}

}


cout << "Go fish!" << endl;

drawCard(ocean, hand1, deckPos, hand1Pos);

return hand1Pos, hand2Pos, deckPos;


}