Question

using C++ // the Deck of the Card is for Go fish Card game Specification for...

using C++ // the Deck of the Card is for Go fish Card game

Specification for building your Deck

For this part of your card game you will need to create a specification for your card deck.

This specification will include functions for populating cards to your deck and shuffling the deck (randomizing the order of the cards).

For the specification I do expect a list of each operation with preconditions and postconditions

You must implement the code for your deck. This implementation will require a test menu that can be easily used to demonstrate and test the functionality thus far.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <time.h>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>

// Defines string for card color and value
const std::string cardColor = "CDHS", cardValue = "A23456789TJQK";
// Initializes card in hand and draw cards
const int cardsInHand = 9, drawCards = 3;

// Defines class MyCard
class MyCard
{
private:
char suit, value;
public:
// Overrides << operator
friend std::ostream& operator<< (std::ostream& ostr, const MyCard &c )
{
ostr << cardValue[c.value] << cardColor[c.suit];
return ostr;
}// End of function

// Function to return true if value of the card is greater than -1
// otherwise returns false
bool isValid()
{
return value > -1;
}// End of function

// Function to set color and card value
void set(char color, char val)
{
suit = color;
value = val;
}// End of function

// Function to return card value
char getRank()
{
return cardValue[value];
}// End of function

// Function to compare card value with parameter card value
// Returns true if matches otherwise returns false
bool operator == ( const char other )
{
return cardValue[value] == other ;
}// End of function

// Function to override < operator
// Returns true if parameter card object suit is equals or less than the card suit
// Otherwise return false
bool operator < ( const MyCard &currentCard )
{
if( value == currentCard.value )
return suit < currentCard.suit;
return
value < currentCard.value;
}// End of function
};// End of class MyCard

// Creates a class MyDeck
class MyDeck
{
// Declares a pointer type object of MyDeck
static MyDeck* currentDeck;
// Declares a vector of MyCard
std::vector<MyCard> myCards;
public:

// Function to return an instance of MyDeck
static MyDeck* createDeck()
{
if( !currentDeck)
currentDeck = new MyDeck();
return currentDeck;
}// End of function

// Function to destroy a deck
void destroyDeck()
{
delete currentDeck;
currentDeck = 0;
}// End of function

// Function to return MyCard object
MyCard drawDeck()
{
MyCard currentCard;
if( myCards.size() > 0 )
{
currentCard = myCards.back();
myCards.pop_back();
return currentCard;
}// End of if condition
currentCard.set( -1, -1 );
return currentCard;
}// End of function
private:
// Default constructor
MyDeck()
{
newDeck();
}// End of constructor

// Function to create a new deck
void newDeck()
{
MyCard currentCard;
// Loops 4 times for suit
for(char su = 0; su < 4; su++ )
{
// Loops 13 times for card values
for( char va = 0; va < 13; va++ )
{
currentCard.set( su, va );
myCards.push_back( currentCard );
}// End of inner for loop
}// End of outer for loop
random_shuffle( myCards.begin(), myCards.end() );
random_shuffle( myCards.begin(), myCards.end() );
}// End of function
};// End of class MyDeck

// Defines a class Player
class Player
{
protected:
std::string name;
std::vector<MyCard> cardHand;
std::vector<char> books;
public:
// Parameterized constructor to assign name
Player( std::string nm ) : name( nm )
{
// Loops till number of cards in hand
for(int counter = 0; counter < cardsInHand; counter++ )
cardHand.push_back( MyDeck::createDeck()->drawDeck() );
sort( cardHand.begin(), cardHand.end() );
}// End of constructor

// Function to display cards in hand
void displayCardsInHand()
{
for( std::vector<MyCard>::iterator itr = cardHand.begin(); itr != cardHand.end(); itr++ )
std::cout << ( *itr ) << " ";
std::cout << "\n";
}// End of function

// Function add a card and return true if card is not in hand
// otherwise returns false
bool addCard( MyCard currentCard)
{
cardHand.push_back( currentCard );
return checkForBook();
}// End of function

// Function to return player name
std::string getName()
{
return name;
}// End of function

// Function to return true if card is in hand otherwise returns false
bool holds( char ch )
{
return( cardHand.end() != find( cardHand.begin(), cardHand.end(), ch ) );
}// End of function

// Function to pick a card and returns MyCard object
MyCard pickCard( char cg )
{
std::vector<MyCard>::iterator itr = find( cardHand.begin(), cardHand.end(), cg );
std::swap( ( *itr ), cardHand.back() );
MyCard tempCard = cardHand.back();
cardHand.pop_back();
hasCards();
sort( cardHand.begin(), cardHand.end() );
return tempCard;
}// End of function

// Returns size
size_t getBooksCount()
{
return books.size();
}// End of function

// Function to display books
void listBooks()
{
for( std::vector<char>::iterator it = books.begin(); it != books.end(); it++ )
std::cout << ( *it ) << "'s ";
std::cout << "\n";
}// End of function

// Function to return if card book is possible otherwise returns false
bool checkForBook()
{
bool retStatus = false;
std::map<char, int> countMap;
for( std::vector<MyCard>::iterator itr = cardHand.begin(); itr != cardHand.end(); itr++ )
countMap[( *itr ).getRank()]++;
for( std::map<char, int>::iterator itr = countMap.begin(); itr != countMap.end(); itr++ )
{
if( ( *itr ).second == 4 )
{
do
{
pickCard( ( *itr ).first );
} while( holds( ( *itr ).first ) ); // End of do - while loop

books.push_back( ( *itr ).first );

( *itr ).second = 0;
retStatus = true;
}
}// End of for loop
sort( cardHand.begin(), cardHand.end() );
return retStatus;
}// End of function

// Function to return if card is available otherwise returns false
bool hasCards()
{
if( cardHand.size() < 1 )
{
MyCard currentCard;
for( int counter = 0; counter < drawCards; counter++ )
{
currentCard = MyDeck::createDeck()->drawDeck();
if( currentCard.isValid() )
addCard( currentCard );
else
break;
}
}// End of if condition
return( cardHand.size() > 0 );
}// End of function
};// End of class Player

// Creates a class Computer derived from Player class
class Computer : public Player
{
private:
std::vector<char> askedCard;
char nextToAskCard, lastAskedCard;
int askedIndex;
public:
// Parameterized constructor to set player name and index
Computer( std::string na ) : Player( na ), askedIndex( -1 ), lastAskedCard( 0 ), nextToAskCard( -1 ) { }
void rememberCard( char ch )
{
if( askedCard.end() != find( askedCard.begin(), askedCard.end(), ch ) || !askedCard.size() )
askedCard.push_back( ch );
}// End of function

// Function to make a move for computer
char makeMove()
{
if( askedIndex < 0 || askedIndex >= static_cast<int>( cardHand.size() ) )
{
askedIndex = rand() % static_cast<int>( cardHand.size() );
}// End of if condition

char ch;
if( nextToAskCard > -1 )
{
ch = nextToAskCard;
nextToAskCard = -1;
}// End of if condition
else
{
while( cardHand[askedIndex].getRank() == lastAskedCard )
{
if( ++askedIndex == cardHand.size() )
{
askedIndex = 0;
break;
}// End of if condition
}// End of while loop
ch = cardHand[askedIndex].getRank();
if( rand() % 100 > 25 && askedCard.size() )
{
for( std::vector<char>::iterator it = askedCard.begin(); it != askedCard.end(); it++ )
{
if( holds( *it ) )
{
ch = ( *it );
break;
}// End of inner if condition
}// End of for loop
}// End of if condition
}// End of else
lastAskedCard = ch;
return ch;
}// End of function

// Function to clear cards for computer
void clearMemory( char ch )
{
std::vector<char>::iterator it = find( askedCard.begin(), askedCard.end(), ch );
if( askedCard.end() != it )
{
std::swap( ( *it ), askedCard.back() );
askedCard.pop_back();
}// End of if condition
}// End of function

// Function to pick a card for computer
bool addCard( MyCard currentCard )
{
if( !holds( currentCard.getRank() ) )
nextToAskCard = currentCard.getRank();
return
Player::addCard( currentCard );
}// End of function
};// End of class Computer

// Defines a class GoFish
class GoFish
{
Player *player;
Computer *computer;
bool playerStatus;
public:
// Default constructor
GoFish()
{
playerStatus = true;
std::string name;
std::cout << "Hi there, enter your name: ";
std::cin >> name;
player = new Player( name );
computer = new Computer("Computer");
}// End of constructor

// Destructor
~GoFish()
{
if( player )
delete player;
if( computer )
delete computer;
MyDeck::createDeck()->destroyDeck();
}// End of destructor

// Function to play the game
void startGame()
{
while( true )
{
if( process( getCard() ) )
break;
}// End of while loop
std::cout << "\n\n";
showBooks();
if( player->getBooksCount() > computer->getBooksCount() )
std::cout << "\n\n\t*** !!! CONGRATULATIONS !!! ***\n\n\n";

else
std::cout << "\n\n\t*** !!! YOU LOSE - HA HA HA !!! ***\n\n\n";
}// End of function
private:
// Function to show the books for the player
void showBooks()
{
if( player->getBooksCount() > 0 )
{
std::cout << "\nYour Book(s): ";
player->listBooks();
}// End of if condition
if( computer->getBooksCount() > 0 )
{
std::cout << "\nMy Book(s): ";
computer->listBooks();
}// End of if condition
}// End of function

// Function to display player cards
void showPlayerCards()
{
std::cout << "\n\n" << player->getName()<< ", these are your cards:\n";
player->displayCardsInHand();
showBooks();
}// End of function

// Function to accept a card for the player and returns it
char getCard()
{
char ch;
if( playerStatus )
{
if( !player->hasCards() )
return -1;
showPlayerCards();
std::string w;
while( true )
{
std::cout << "\nWhat card(rank) do you want? ";
std::cin >> w;
ch = toupper( w[0] );
if( player->holds( ch ) ) break;
std::cout << player->getName()<< ", Can't ask for a card which you don't have!\n\n";
}// End of while loop
}// End of if condition
else
{
if( !computer->hasCards() )
return -1;
ch = computer->makeMove();
showPlayerCards();
std::string r;
std::cout << "\nDo you have any " << ch << "'s? (Y)es / (G)o Fish ";
do
{
std::getline( std::cin, r );
r = toupper( r[0] );
}
while( r[0] != 'Y' && r[0] != 'G' );
bool hasIt = player->holds( ch );
if( hasIt && r[0] == 'G' )
std::cout << "Don't try to cheat me?\n";
if( !hasIt && r[0] == 'Y' )
std::cout << "No, you don't have it!!!\n";
}// End of else
return ch;
}// End of function

// Function to process the card
bool process( char ch )
{
if( ch < 0 )
return true;
if( playerStatus )
computer->rememberCard( ch );

Player *a, *b;
a = playerStatus ? computer : player;
b = playerStatus ? player : computer;
bool r;
if( a->holds( ch ) )
{
while( a->holds( ch ) )
r = b->addCard( a->pickCard( ch ) );

if( playerStatus && r )
computer->clearMemory( ch );
}// End of if condition
else
{
fish();
playerStatus = !playerStatus;
}// End of else
return false;
}// End of function
void fish()
{
std::cout << "\n\n\t *** GO FISH! ***\n\n";
MyCard currentCard = MyDeck::createDeck()->drawDeck();
if( playerStatus )
{
std::cout << "Your new card: " << currentCard
<< ".\n\n******** Your turn completed! ********\n" << std::string( 36, '-' ) << "\n\n";
if( player->addCard( currentCard ) )
computer->clearMemory( currentCard.getRank() );
}// End of if condition
else
{
std::cout << "\n********* My turn completed! *********\n" << std::string( 36, '-' ) << "\n\n";
computer->addCard( currentCard );
}// End of else
}// End of function
};// End of class GoFish

MyDeck* MyDeck::currentDeck = 0;
// main function definition
int main()
{
srand( static_cast<unsigned>( time( NULL ) ) );
// Creates an object of class GoFish
GoFish gf;
// Calls the function to start game
gf.startGame();
return 0;
}// End of main function

Sample Output:

Hi there, enter your name: Pyari


Pyari, these are your cards:
3D 4H 5D 5H 9D TS JD QC KH

What card(rank) do you want? 3D


*** GO FISH! ***

Your new card: 5S.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TS JD QC KH

Do you have any K's? (Y)es / (G)o Fish g
Don't try to cheat me?


Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TS JD QC

Do you have any A's? (Y)es / (G)o Fish y
No, you don't have it!!!


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TS JD QC

What card(rank) do you want? 3D


*** GO FISH! ***

Your new card: 7H.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
3D 4H 5D 5H 5S 7H 9D TS JD QC

Do you have any 7's? (Y)es / (G)o Fish y


Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TS JD QC

Do you have any 8's? (Y)es / (G)o Fish g


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TS JD QC

What card(rank) do you want? ts


Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TH TS JD QC

What card(rank) do you want? qc


Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TH TS JD QC QD QS

What card(rank) do you want? 5s


*** GO FISH! ***

Your new card: JC.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
3D 4H 5D 5H 5S 9D TH TS JC JD QC QD QS

Do you have any 3's? (Y)es / (G)o Fish g
Don't try to cheat me?


Pyari, these are your cards:
4H 5D 5H 5S 9D TH TS JC JD QC QD QS

Do you have any 3's? (Y)es / (G)o Fish y
No, you don't have it!!!


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
4H 5D 5H 5S 9D TH TS JC JD QC QD QS

What card(rank) do you want? QD


*** GO FISH! ***

Your new card: 6S.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
4H 5D 5H 5S 6S 9D TH TS JC JD QC QD QS

Do you have any 6's? (Y)es / (G)o Fish y


Pyari, these are your cards:
4H 5D 5H 5S 9D TH TS JC JD QC QD QS

Do you have any 3's? (Y)es / (G)o Fish 5H
y
No, you don't have it!!!


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
4H 5D 5H 5S 9D TH TS JC JD QC QD QS

What card(rank) do you want? 4H


*** GO FISH! ***

Your new card: 9C.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
4H 5D 5H 5S 9C 9D TH TS JC JD QC QD QS

Do you have any T's? (Y)es / (G)o Fish y


Pyari, these are your cards:
4H 5D 5H 5S 9C 9D JC JD QC QD QS

Do you have any 3's? (Y)es / (G)o Fish g


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
4H 5D 5H 5S 9C 9D JC JD QC QD QS

What card(rank) do you want? QS


*** GO FISH! ***

Your new card: 3C.

******** Your turn completed! ********
------------------------------------

Pyari, these are your cards:
3C 4H 5D 5H 5S 9C 9D JC JD QC QD QS

Do you have any 2's? (Y)es / (G)o Fish g


*** GO FISH! ***


********* My turn completed! *********
------------------------------------

Pyari, these are your cards:
3C 4H 5D 5H 5S 9C 9D JC JD QC QD QS

What card(rank) do you want?

Add a comment
Know the answer?
Add Answer to:
using C++ // the Deck of the Card is for Go fish Card game Specification for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using C++ // the Deck of the Card is for Go fish Card game Specification for building your Deck For this part of your ca...

    using C++ // the Deck of the Card is for Go fish Card game Specification for building your Deck For this part of your card game you will need to create a specification for your card deck. This specification will include functions for populating cards to your deck and shuffling the deck (randomizing the order of the cards). For the specification I do expect a list of each operation with preconditions and postconditions You must implement the code for your...

  • using C++ with Go Fish card game Card Game Project Requirements: Title Screen Title Menu: Run Game Rules – How game is played. This should display a string output explaining the game. Run Test...

    using C++ with Go Fish card game Card Game Project Requirements: Title Screen Title Menu: Run Game Rules – How game is played. This should display a string output explaining the game. Run Test Mode Object Oriented Design pieces: Building Card Objects Building a Deck of Card Objects Shuffling Card Objects in Deck Creating Player Objects Moving cards from Deck to Player Validating Card values Functional Design pieces: Test Engine – Used to test pieces of functionality separately Should allow...

  • C++ programming: Card game Can same one help me out with a example finsih and working...

    C++ programming: Card game Can same one help me out with a example finsih and working programm ? Program the basis for a card game (as a class card game): ● A card is represented by its suit/symbol (clubs, spades, hearts, diamonds) and a value (seven, eight, nine, ten, jack, queen, king, ace). ● A deck of cards consists of a pile of 32 cards that are completely connected to four players are distributed. ● Implement the following menu: ===...

  • CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As...

    CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As needed Recursion OBJECTIVES Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have a solid foundation before you can build a house). In order to allow creative freedom,...

  • Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

    Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program. 1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand. 2) Ask...

  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • Using C++ Create a Blackjack program with the following features: Single player game against the dealer....

    Using C++ Create a Blackjack program with the following features: Single player game against the dealer. Numbered cards count as the number they represent (example: 2 of any suit counts as 2) except the Aces which can count as either 1 or 11, at the discretion of the player holding the card. Face cards count as 10. Player starts with $500. Player places bet before being dealt a card in a new game. New game: Deal 2 cards to each...

  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

  • This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...

    This needs to be done in c++11 and be compatible with g++ compiling Project description: Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52 cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT