Question

HELP NEED!! Can some modified the program Below in C++ So that it can do what...

HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today????

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h> /* time */
#include <sstream> // for ostringstream

using namespace std;
// PlayingCard class
class PlayingCard{

int numRank;
int numSuit;
string rank;
string suit;
public:
PlayingCard();
PlayingCard(int numRank,int numSuit);
void setRankString();
void setSuitString();
void setRank(int numRank);
void setSuit(int numSuit);
string getRank();
string getSuit();
int getRankNum();
int getSuitNum();
string getCard();
};

PlayingCard::PlayingCard()
{ numRank = 1;
numSuit = 1;
setRankString();
setSuitString();
}

PlayingCard::PlayingCard(int numRank, int numSuit)
{ this->numRank = numRank;
this->numSuit = numSuit;
}

void PlayingCard::setRank(int numRank)
{ this->numRank = numRank;
setRankString();
}

void PlayingCard::setSuit(int numSuit)
{ this->numSuit = numSuit;
setSuitString();
}

void PlayingCard::setRankString()
{ if(numRank == 1)
rank="Ace";
else if(numRank >=2 && numRank <=10)
{//rank = to_string(numRank);
ostringstream ss;
ss<<numRank;
rank=ss.str();}
else if(numRank == 11)
rank = "Jack";
else if(numRank == 12)
rank = "Queen";
else
rank = "King";
}

void PlayingCard::setSuitString()
{ switch(numSuit)
{
case 1: suit="spade";
break;
case 2: suit="club";
break;
case 3:suit="heart";
break;
case 4:suit="diamond";
break;
}
}

string PlayingCard:: getRank()
{ return rank;}

string PlayingCard::getSuit()
{ return suit;}

int PlayingCard::getRankNum()
{ return numRank;}

int PlayingCard::getSuitNum()
{ return numSuit;}

string PlayingCard::getCard()
{ return(rank + " of "+suit+"s");}
// function to deal and return a card at random from the deck
PlayingCard dealOneCard(PlayingCard deck[],int numCards);
// function to count and return the number of matching cards in a hand
int matching_cards(PlayingCard hand[]);

int main()
{ srand(time(NULL));
PlayingCard deck[52];
int numCards =52;
PlayingCard computerHand[5];
PlayingCard humanHand[5];
int n=0;
// loop to initialize the 52 cards
for(int i=0;i<4;i++)
{ for(int j=0;j<13;j++)
{ deck[n].setRank(j+1);
deck[n].setSuit(i+1);
n++;
           }
}
// deal the cards one by one for both the hands
for(int i=0;i<5;i++)
{ humanHand[i] = dealOneCard(deck,numCards);
numCards--;
computerHand[i] = dealOneCard(deck,numCards);
numCards--;
}
// display both hands
cout<<"Player's cards: "<<endl;
for(int i=0;i<5;i++)
cout<<humanHand[i].getCard()<<endl;

cout<<"\nComputer's cards: "<<endl;
for(int i=0;i<5;i++)
cout<<computerHand[i].getCard()<<endl;

int playerMatch = matching_cards(humanHand);
int computerMatch = matching_cards(computerHand);
// display the output
if(playerMatch > 1)
cout<<"\nPlayer has "<<playerMatch<<" cards of same value "<<endl;
else
cout<<"\nPlayer has no pairs"<<endl;

if(computerMatch > 1)
cout<<"Computer has "<<computerMatch<<" cards of same value "<<endl;
else
cout<<"Computer has no pairs"<<endl;

if(playerMatch > computerMatch)
cout<<"Player wins"<<endl;
else if(playerMatch < computerMatch)
cout<<"Computer wins"<<endl;
else
cout<<"It's a tie"<<endl;
return 0;
}

PlayingCard dealOneCard(PlayingCard deck[],int numCards)
{ int ind = rand()%numCards;
PlayingCard card = deck[ind];
deck[ind] = deck[numCards-1];
deck[numCards-1] = card;
return card;
}

int matching_cards(PlayingCard hand[])
{ int numCards = 5;
int countFaces[numCards];

for(int i=0;i<numCards;i++)
{ countFaces[i] = 1;
for(int j=i+1;j<numCards;j++)
{ if(hand[i].getRank() == hand[j].getRank())
countFaces[i]++;
}
}

int max = countFaces[0];
for(int i=1;i<numCards;i++)
if(countFaces[i] > max)
max = countFaces[i];

return max;
}

Use inheritance and classes to represent a deck of playing cards. Create a Card class that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators.

Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to:

• Print every card in the deck

• Shuffle the cards in the deck. You can implement this by randomly swapping every card in the deck.

• Add a new card to the deck. This function should take a Card object as a parameter and add it to the vector.

• Remove a card from the deck. This removes the first card stored in the vector and returns it.

• Sort the cards in the deck ordered by name.

Next, create a Hand class that represents cards in a hand. Hand should be derived from Deck. This is because a hand is like a more specialized version of a deck; we can print, shuffle, add, remove, or sort cards in a hand just like cards in a deck. The default constructor should set the hand to an empty set of cards.

Finally, write a main function that creates a deck of cards, shuffles the deck, and creates two hands of 5 cards each. The cards should be removed from the deck and added to the hand. Test the sort and print functions for the hands and the deck. Finally, return the cards in the hand to the deck and test to ensure that the cards have been properly returned.

You may add additional functions or class variables as desired to implement your solution.

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

// Task1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for sorting.
using namespace std;

// Card Class. Holds Information of each of the 52 cards
// those 52 cards will be added in a Deck of cards class.
class Card
{
public:
   Card(); // default constructor.
   Card(string suit, string name); // parameterized constructor.
   Card(const Card& Obj); // copy constructor.
   void setSuit(string suit); // setter.
   string getSuit(); // getter.
   void setName(string name); // setter.
   string getName(); // getter.
   ~Card(); // destructor.
private:
   string suit;
   string name;
};

Card::Card()
{
   this->suit = "";
   this->name = "";
}

Card::Card(string suit, string name)
{
   this->suit = suit;
   this->name = name;
}

Card::Card(const Card& Obj)
{
   this->suit = Obj.suit;
   this->name = Obj.name;
}

void Card::setSuit(string suit)
{
   this->suit = suit;
}

string Card::getSuit()
{
   return suit;
}

void Card::setName(string name)
{
   this->name = name;
}

string Card::getName()
{
   return name;
}

Card::~Card()
{
   this->suit = "";
   this->name = "";
}

// Deck class, holds a deck of 52 cards.
class Deck
{
public:
   Deck(); // sets name and suit of each card and adds them to a vector.
   void printDeck(); // prints all the cards in the deck.
   void shuffleDeck(); // shuffles the cards in the deck, shuffle method described in function defination.
   void addCardToDeck(Card Obj); // add a card to the deck.
   Card removeCardFromDeck(); // remove a card from the deck and return to main.
   void sortDeck(); // sorts a deck by name, explanation in function defination.
   ~Deck(); // destructor.
private:
   vector<Card> deckOfCards;
};

Deck::Deck()
{
   Card Cards[52];

   Cards[0].setSuit("Spades");
   Cards[0].setName("Ace");

   Cards[1].setSuit("Spades");
   Cards[1].setName("2");

   Cards[2].setSuit("Spades");
   Cards[2].setName("3");

   Cards[3].setSuit("Spades");
   Cards[3].setName("4");

   Cards[4].setSuit("Spades");
   Cards[4].setName("5");

   Cards[5].setSuit("Spades");
   Cards[5].setName("6");

   Cards[6].setSuit("Spades");
   Cards[6].setName("7");

   Cards[7].setSuit("Spades");
   Cards[7].setName("8");

   Cards[8].setSuit("Spades");
   Cards[8].setName("9");

   Cards[9].setSuit("Spades");
   Cards[9].setName("10");

   Cards[10].setSuit("Spades");
   Cards[10].setName("Jack");

   Cards[11].setSuit("Spades");
   Cards[11].setName("Queen");

   Cards[12].setSuit("Spades");
   Cards[12].setName("King");

   Cards[13].setSuit("Hearts");
   Cards[13].setName("Ace");

   Cards[14].setSuit("Hearts");
   Cards[14].setName("2");

   Cards[15].setSuit("Hearts");
   Cards[15].setName("3");

   Cards[16].setSuit("Hearts");
   Cards[16].setName("4");

   Cards[17].setSuit("Hearts");
   Cards[17].setName("5");

   Cards[18].setSuit("Hearts");
   Cards[18].setName("6");

   Cards[19].setSuit("Hearts");
   Cards[19].setName("7");

   Cards[20].setSuit("Hearts");
   Cards[20].setName("8");

   Cards[21].setSuit("Hearts");
   Cards[21].setName("9");

   Cards[22].setSuit("Hearts");
   Cards[22].setName("10");

   Cards[23].setSuit("Hearts");
   Cards[23].setName("Jack");

   Cards[24].setSuit("Hearts");
   Cards[24].setName("Queen");

   Cards[25].setSuit("Hearts");
   Cards[25].setName("King");

   Cards[26].setSuit("Diamonds");
   Cards[26].setName("Ace");

   Cards[27].setSuit("Diamonds");
   Cards[27].setName("2");

   Cards[28].setSuit("Diamonds");
   Cards[28].setName("3");

   Cards[29].setSuit("Diamonds");
   Cards[29].setName("4");

   Cards[30].setSuit("Diamonds");
   Cards[30].setName("5");

   Cards[31].setSuit("Diamonds");
   Cards[31].setName("6");

   Cards[32].setSuit("Diamonds");
   Cards[32].setName("7");

   Cards[33].setSuit("Diamonds");
   Cards[33].setName("8");

   Cards[34].setSuit("Diamonds");
   Cards[34].setName("9");

   Cards[35].setSuit("Diamonds");
   Cards[35].setName("10");

   Cards[36].setSuit("Diamonds");
   Cards[36].setName("Jack");

   Cards[37].setSuit("Diamonds");
   Cards[37].setName("Queen");

   Cards[38].setSuit("Diamonds");
   Cards[38].setName("King");

   Cards[39].setSuit("Clubs");
   Cards[39].setName("Ace");

   Cards[40].setSuit("Clubs");
   Cards[40].setName("2");

   Cards[41].setSuit("Clubs");
   Cards[41].setName("3");

   Cards[42].setSuit("Clubs");
   Cards[42].setName("4");

   Cards[43].setSuit("Clubs");
   Cards[43].setName("5");

   Cards[44].setSuit("Clubs");
   Cards[44].setName("6");

   Cards[45].setSuit("Clubs");
   Cards[45].setName("7");

   Cards[46].setSuit("Clubs");
   Cards[46].setName("8");

   Cards[47].setSuit("Clubs");
   Cards[47].setName("9");

   Cards[48].setSuit("Clubs");
   Cards[48].setName("10");

   Cards[49].setSuit("Clubs");
   Cards[49].setName("Jack");

   Cards[50].setSuit("Clubs");
   Cards[50].setName("Queen");

   Cards[51].setSuit("Clubs");
   Cards[51].setName("King");

   for (int i = 0; i < 52; i++)
       deckOfCards.push_back(Cards[i]);
  
}

void Deck::printDeck()
{
   // printing using iterator for vector.
   vector<Card>::iterator it;
   int i = 1;
   for (it = deckOfCards.begin(); it != deckOfCards.end(); ++it, i++)
       cout << i << ".) " << it._Ptr->getName() << " Of " << it._Ptr->getSuit() << endl;
}

void Deck::shuffleDeck()
{
   // this loop runs 52 times, it can be any number you want too.
   // it generates two random indexes and swaps them.
   // this is done 52 times to ensure a nice shuffled deck.
   for (int i = 0; i < 52; i++)
   {
       int random1 = (rand() % 52);
       int random2 = (rand() % 52);
       Card C = deckOfCards[random1];
       deckOfCards[random1] = deckOfCards[random2];
       deckOfCards[random2] = C;
   }
}

void Deck::addCardToDeck(Card Obj)
{
   deckOfCards.push_back(Obj);
}

Card Deck::removeCardFromDeck()
{
   Card ret = deckOfCards.front();
   deckOfCards.erase(deckOfCards.begin());
   return ret;
}

bool compare(Card a, Card b)
{
   // custom sort function comparison.
   return (a.getName().compare(b.getName()) < 0);
}

void Deck::sortDeck()
{
   // sort function from the algorithm library, uses compare function to sort by name of card
   sort(deckOfCards.begin(), deckOfCards.end(), compare);
}

Deck::~Deck()
{
   deckOfCards.~vector();
}

// in a game of cards, each hand holds a number of cards.
// we use vector to store those cards in the same way we used in deck.
class Hand
   : public Deck
{
public:
   Hand();
   void addCardInHand(Card Obj);
   Card removeCardFromHand();
   void printCardsInHand();
   ~Hand();

private:
   vector<Card> cardsInHand;
};

Hand::Hand()
{

}

void Hand::addCardInHand(Card Obj)
{
   cardsInHand.push_back(Obj);
}

Card Hand::removeCardFromHand()
{
   Card ret = cardsInHand.front();
   cardsInHand.erase(cardsInHand.begin());
   return ret;
}

void Hand::printCardsInHand()
{
   vector<Card>::iterator it;
   int i = 1;
   for (it = cardsInHand.begin(); it != cardsInHand.end(); ++it, i++)
       cout << i << ".) " << it._Ptr->getName() << " Of " << it._Ptr->getSuit() << endl;
}

Hand::~Hand()
{
   cardsInHand.~vector();
}

int main()
{
   Deck DeckOne; // make a deck.
   DeckOne.shuffleDeck(); // shuffle the deck.
   Hand One; // make hand 1.
   Hand Two; // make hand 2.

   cout << "---Shuffled Deck In Start.---\n\n";
   DeckOne.printDeck(); // printing the shuffled deck of cards.

   // giving each player 5 cards in the start from the top of the deck.
   for (int i = 0; i < 5; i++)
   {
       One.addCardInHand(DeckOne.removeCardFromDeck());
       Two.addCardInHand(DeckOne.removeCardFromDeck());
   }

   // printing to check if cards are distributed correctly.
   cout << "\n\n---After Adding Cards To Hand From Deck.---\n\n";
   DeckOne.printDeck();
   cout << "\n--Cards In Hand One.--\n";
   One.printCardsInHand();
   cout << "\n--Cards In Hand Two.--\n";
   Two.printCardsInHand();

   // returning all thje cards from a players hand to the deck at the end of the game.
   for (int i = 0; i < 5; i++)
   {
       DeckOne.addCardToDeck(One.removeCardFromHand());
       DeckOne.addCardToDeck(Two.removeCardFromHand());
   }

   // printing to check if all cards are correctly returned to the deck.
   cout << "\n\n---After Returning Cards From Hand To Deck.---\n\n";
   DeckOne.printDeck();
   cout << "\n--Cards In Hand One.--\n";
   One.printCardsInHand();
   cout << "\n--Cards In Hand Two.--\n";
   Two.printCardsInHand();

   // sorting the deck at the end of the game and printing.
   DeckOne.sortDeck();
   cout << "\n\n---After Sorting The Cards By Name.---\n\n";
   DeckOne.printDeck();


   return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Shuffled Deck In Start.- 1.) Ace Of Spades 2.) 10 0f Clubs 3.) 7 Of Hearts 4.) 8 Of Clubs 5.) 6 Of Hearts 6.) 4 Of Clubs 7.)

-After Adding Cards To Hand From Deck.- 1.) 7 Of Spades 2.) 5 Of Hearts 3.) Queen Of Clubs 4.) Queen Of Diamonds 5) 7 Of Diam

-After Returning Cards From Hand To Deck. 1.) 7 Of Spades 2.) 5 Of Hearts 3.) Queen Of Clubs 4.) Queen Of Diamonds 5) 7 Of Di

--Cards In Hand Two.-- - - After Sorting The Cards By Name. -- 1.) 10 0f Clubs 2.) 10 0Of Spades 3.) 10 of Diamonds 4.) 10 Of

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

Add a comment
Know the answer?
Add Answer to:
HELP NEED!! Can some modified the program Below in C++ So that it can do what...
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
  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • I wrote a card shuffle program and I am trying to see how I can add...

    I wrote a card shuffle program and I am trying to see how I can add a sorting function to the program to sort the players hands before printing them. How would I do this? #include <iostream> #include <iomanip> #include <vector> #include <algorithm> using namespace std; void printHand(int []); void deal(vector<int> &, int[], int[], int[], int[]); void unwrapDeck(vector<int> &deck) { for(int i=0; i<=51; i++) deck.push_back(i); } void shuffleDeck(vector<int> &deck) { //Status of cards before shuffling cout << "Before shuffling: "...

  • JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

    JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main point of the exercise is to demonstrate your ability to use various types of nested classes. Of course, sorting is important as well, but you don’t really need to do much more than create the class that does the comparison. In general, I like giving you some latitude in how you design and implement your projects. However, for this assignment, each piece is very...

  • Hi! I need help with a C++ program In this assignment, you will create some routines...

    Hi! I need help with a C++ program In this assignment, you will create some routines that will later be used in a "Black Jack" program. Your output on this first part will look something like: card's name is QC with a black jack value of 10 Unshuffled Deck AC/1 2C/2 3C/3 4C/4 5C/5 6C/6 7C/7 8C/8 9C/9 TC/10 JC/10 QC/10 KC/10 AD/1 2D/2 3D/3 4D/4 5D/5 6D/6 7D/7 8D/8 9D/9 TD/10 JD/10 QD/10 KD/10 AH/1 2H/2 3H/3 4H/4 5H/5...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...

    need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards in a hand. * * @author (your name) * @version (a version number or a date) */ public class CountRank { // instance variables - replace the example below with your own private int rankCount[]; private Hand hand; private int count; /** * Constructor for objects of class CountRank */ public CountRank(Hand h) { // initialise instance variables hand = h; rankCount = new...

  • Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games...

    Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

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