I need someones help! Write a C++ program that plays the game of “Go Fish”. Your
ID: 3768321 • Letter: I
Question
I need someones help!
Write a C++ program that plays the game of “Go Fish”. Your code must contain Game class, Deck Class, Player class, and Hand class.
Put them into separate files such as;
main.cpp
Player.cpp Player.h
Game.cpp Game.h
Deck.cpp Deck.h
Hand.cpp Hand.h
There are many variations of Go Fish, but we are going to play with the following rules:
Players: 2 – 7
Supplies: 1 Deck of cards (52 total)
Objective: Collect as many groups of 4 cards of the same rank/value, and discard the groups from your hand, until you do not have any cards left. The first player to discard all of his/her cards wins!!! Playing: Start by dealing 7 cards to each player. Each player takes turns asking a specific player for a card of some rank. The player asked either has to give all of his/her cards of that rank and draw that many new cards from the top of the remaining cards or tell the player to “Go Fish” (pick up one card from the top of the remaining cards) because he/she isn’t holding any cards of that rank. This continues until a player discards all of his/her cards from collecting groups of 4 of the same rank.
What data will you have? There is the individual card, which has a suit (spade, heart, club, or diamond) and a rank (Ace, 2-10, Jack, Queen, or King). As there are no functions for a card you will use a struct:
struct card
{
string suit;
string rank;
};
There will be a Game class. This class has-a Deck (of cards) and has-a number (2-7) of Players. Each Players has-a Hand of cards. Each Player can give card(s) or receive cards. The player must also “lay down” any sets of 4 in its Hand. If its Hand is empty then that Player wins and the game is over!
How are these classes/structs used? A Game must shuffle and then deal 7 cards to each Player. Whenever a Player must draw 1 or more cards the Game must “deal” them to that player. Does the game do anything else?
A Deck has 52 cards: consisting of 4 suits for each 13 ranks. A deck of cards has activities associated with it. We can shuffle and deal the cards by rank. We deal cards by randomizing their order. To shuffle you will randomly put the cards into a stack. You will implement your own stack (using your code from previous projects is acceptable). You will NOT use a stack container from the STL or
other library. To deal you will take the top card and give it to each player in turn.
A Player receives the cards from the Game or another Player. New cards must be placed in their Hand. The player must lay down any sets of 4 in its hand. The Player also requests cards from another player. They either receive the card(s) requested, or they go fish and draw another card.
Does the Hand do anything other than hold the cards? If no then it should be a struct. If yes, it should be a class with the necessary functions. You should use an array or a vector to hold the cards. You will sort them by rank. You will need a function to determine what rank to ask for. Should you put that function in the Hand, or the Player, or some other class?
Where do you put the go_fish operation? Remember it must request cards of a player. If the player has cards of the requested rank she must hand them all over, AND draw that maney cards into her hand. The player making the request puts the cards into their hand. If that makes four of a kind then they must be laid down. If she does not have any cards of that rank the requesting player must go fish and draw a new card into his hand. All of this must be somewhere in your design, but not necessarily in one function.
You must ensure that all classes have the data and functions required by your analysis. The only stipulation is that you have to keep functions under 20 lines of code.
AI: You will have a queue that will keep a record of all requests made. The queue will have maximum length of twice the number of players in the current game. You will implement your own queue (using your code from previous projects is acceptable). You will NOT use a queue container from the STL or other library. Unlike a normal queue this one has a set maximum size, twice the number of players. If you add an item to the back that exceeds the size it automatically removes the front item. Also unlike a normal queue you must be able to iterate through the nodes to look at the previous requests.
You will need a struct that indicates the Player and what they asked for. Part of the “strategy” in playing Go Fish is remembering who asked for what. You can determine how far back they can “remember”. Notice that Reptiles have a different problem. If there are 4 players they do not “remember” the last 4 plays. They have a sliding window that is always N plays behind.
You must have one class per source file. You must have your class definitions in a .h file and your implemented classes in .cpp files. Place the structrs where it makes sense. You must also have your main function in a play_game.cpp file, separated from the class implementations. Create a Makefile for you project.
create the Game class and object (10%)
create the Deck class and objects, using the card struct (10%)
create the Player class and object (10%)
create the Hand class and object (10%)
your program correctly plays a game of Go Fish, with or without a human player (10%)
Explanation / Answer
Answer :
Player.h
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
enum Suit {spades, hearts, diamonds, clubs};
Player();
Player(int rank, Suit s);
string toString() const;
bool SameSuitAs(const Card& c) const;
int GetRank() const;
string suitString(Suit s) const;
bool IsJoker() const;
private:
string rankString(int r) const;
int myRank;
Suit mySuit;
};
ostream& operator << (ostream& out, const Card& c);
bool operator == (const Card& lhs, const Card& rhs);
#endif
Player.cpp
#include "player.h"
Player::Player()
: myRank(1),
mySuit(Player::spades)
{
}
Player::Player(int rank, Suit s)
: myRank(rank),
mySuit(s)
{
}
string Player::suitString(Suit s) const
{
if (s == spades) return "spades";
else if (s == hearts) return "hearts";
else if (s == diamonds) return "diamonds";
else return "clubs";
}
string Player::rankString(int r) const
{
if (1 == r) return "ace";
else if (2 == r) return "two";
else if (3 == r) return "three";
else if (4 == r) return "four";
else if (5 == r) return "five";
else if (6 == r) return "six";
else if (7 == r) return "seven";
else if (8 == r) return "eight";
else if (9 == r) return "nine";
else if (10 == r) return "ten";
else if (11 == r) return "jack";
else if (12 == r) return "queen";
else if (13 == r) return "king";
else return "joker";
}
string Player::toString() const
{
if (1 <= myRank && myRank <= 13)
{
return rankString(myRank) + " of " + suitString(mySuit);
}
return "zero of doughnuts";
}
bool Player::SameSuitAs(const Player& c) const
{
return mySuit == c.mySuit;
}
int Player::GetRank() const
{
return myRank;
}
bool Player::IsJoker() const
{
return myRank < 1 || 13 < myRank;
}
ostream& operator <<(ostream& out, const Player& c)
{
out << c.toString();
return out;
}
bool operator == (const Player& lhs, const Player& rhs)
{
if (lhs.IsJoker() || rhs.IsJoker())
{
return false;
}
return lhs.GetRank() == rhs.GetRank() && lhs.SameSuitAs(rhs);
}
Game.cpp Including Main.cpp :
#include <iostream>
#include "deck.h"
#include "card.h"
#include "hand.h"
void Print_Content(const Deck& d)
{
Deck copy(d);
int count = d.Size();
int k;
for(k=0; k < count; k++)
{
cout << copy.GetCard() << endl;
}
}
void Print_Content(const apvector<Card> & hand)
{
int k;
cout << "------------" << endl;
for (k=0; k< hand.length(); k++)
{
cout << hand[k] << endl;
}
cout << "------------" << endl;
}
int main()
{
const int HAND_SIZE = 5;
Deck d;
int k;
Print_Content(d);
cout << " ---after shuffling--- " << endl;
d.Shuffle();
Print_Content(d);
apvector<Card> player(HAND_SIZE);
for(k=0; k < HAND_SIZE; k++)
{
player[k] = d.GetCard();
}
Print_Content(player);
return 0;
}
Deck.h
Deck.cpp
#include "deck.h"
#include "randomgeneration.h"
const int SIZE = 52;
Deck::Deck()
: myCards(SIZE),
myIndex(0)
{
int rank;
int suit;
int num = 0;
for(rank=1; rank <= SIZE/4; rank++)
{
for (suit = Player::spades; suit <= Player::clubs; suit++)
{
myCards[num] = Card(rank,Card::Suit(suit));
num++;
}
}
}
void Deck::Shuffle()
{
myIndex = 0;
RandomGeneration gen;
int k;
for(k=0; k < SIZE-1; k++)
{
int swapIndex = gen.RandInt(k,SIZE-1);
Player temp = myCards[swapIndex];
myCards[swapIndex] = myCards[k];
myCards[k] = temp;
}
}
Player Deck::GetCard()
{
Palyer c;
if (0 <= myIndex && myIndex < SIZE)
{
c = myCards[myIndex];
myIndex++;
}
else
{
c = Player(0,Player::spades);
}
return c;
}
int Deck::Size() const
{
return SIZE - myIndex;
}
Hand.h
#ifndef _HAND_H
#define _HAND_H
#include "deck.h"
#include "namecards.h"
class Hand
{
public:
Hand();
void DealFrom(Deck& d);
void Print_Content() const;
private:
namecards<Player> myCards;
};
#endif
Hand.cpp
#include "hand.h"
const int SIZE = 5;
Hand::Hand()
: myCards(SIZE)
{
}
void Hand::DealFrom(Deck& d)
{
int k;
for(k=0; k < SIZE; k++)
{
myCards[k] = d.GetCard();
}
}
void Hand::Print_Content() const
{
int k;
for(k=0; k < SIZE; k++)
{
cout << myCards[k] << endl;
}
}