Homework Help Question & Answers

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();
};

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
Next > < Previous
ReportAnswer #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! :)

Know the answer?
Add Answer of:
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...
Your Answer: Your Name: What's your source?
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...

    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(); };...

  • Help fix datastructure code solitaire game

    I need help getting my code to work. It half works but I can figure out how to make it completely work. If you run it as is it will compile and run but I'm stuck as tohow to make it work per the rules.Here are the rules for the code. :In each round the player draws four cards.Each card is placed face-up on top of one of four columns.A card can be discarded when it is one of the...

  • C++ cards dealt to 4 players (was asked to improve from this code)

    - after card was dealt, each player can select one card and put in the table.- These four card will be compared and the highest card wins:Example: A>K>Q>J>10>9>8>7>6>5>4>3>2#include <iostream>#include <string>#include <sstream>#include <time.h>using namespace std;struct PlayingCard{char suit;string rank;};PlayingCard deck[52];PlayingCard playerN[13];PlayingCard playerE[13];PlayingCard playerS[13];PlayingCard playerW[13];void fillDeck(){for (int i=0; i<13; i++){deck[i].suit = 6;//Spadesdeck[i+13].suit = 3;//Heartsdeck[i+26].suit = 4;//Diamondsdeck[i+39].suit = 5;//Clubsif (i==0){deck[i].rank = "A";deck[i+13].rank = "A";deck[i+26].rank = "A";deck[i+39].rank = "A";}else if (i==12){deck[i].rank = "K";deck[i+13].rank = "K";deck[i+26].rank = "K";deck[i+39].rank = "K";}else if (i==12){deck[i].rank = "Q";deck[i+13].rank = "Q";deck[i+26].rank...

  • BIG JAVA POKER SIMULATOR-DESPERATE, PLEASE HELP, MOST OF THE CODE WRITTEN ALREADY

    Poker Simulator. Implement a simulation of a popular casino game called video poker. The card deck contains 52 cards, 13 of each suit. At the beginning of the game,thedeck is shuffled. You need to devise a fair method for shuffling. (It does not have to be efficient.) Then the top five cards of the deck are presented to the player.The player can reject none, some, or all five of the cards. The rejected cards are replaced from the top of...

  • c++ class

    I need help with this program here is the problem:Write a C++ program which will play a card game called “Hearts”. Hearts is a trick taking gamein which the objective is to avoid winning tricks containing hearts; the queen of spades is evenmore to be avoided.The program plays “Hearts” with four players: computer 1, computer 2, computer 3, and the user.The winner of the game is the person who has the lowest number of points after the game is over.Points...

  • cards C++. I've done some coding and wasn't sure to finish it up. Please Help!!

    I've finished up with the Card.h i believe. :-). im having trouble with the rest. Below are rest of the instructions i wasn't able to finish up.1. Create a deck of cards. you can use one of the container class to model the deck. eg. vector<card>deck1.ive create the deck already but im not sure if its correct.2. Initialize the deck. add all the possible cards into the deck. this can be done by exhaustion or by using nested loop to...

  • C sharp program

    Write a program that deals a five-card poker hand. The write functions to accomplish each of the following:Determine whether the hand contains a pair.Determine whether the hand contains two pairs.Determine whether the hand contains three of a kind (e.g., three jacks).Determine whether the hand contains four of a kind (e.g., four aces).The code must be in C#This is what I have so far:using System;using System.Windows.Forms;namespace DeckOfCards{public partial class DeckOfCardsForm : Form{private Card[] deck = new Card[ 52 ]; // deck...

  • Java Program Three Card Poker

    Here is the demo codeimport java.util.ArrayList; public class CardDeck {private ArrayList <Card> cards;public CardDeck (){cards = new ArrayList<CardDeck.Card>();loadCards();}public Card deal (){//write code}public Card [] dealHand (int n){//write code}public static int compareHands (Card[] dealer, Card[] player){//return -1 if dealer is better//return +1 if dealer}private static boolean isSFlush (Card[] hand){//same suite sequence of values}private void loadCards() {for (int s =1; s <=4; s++)for (int r=1; r <= 13; r++)cards.add(new Card (r, s));}public String toString (){return cards.toString();}public class Card {private int rank, suite;public...

  • This is a C program for Blackjack. The program crashes just after I start the game and I don't...

    #include <stdio.h>#include <stdlib.h>#include <process.h>#include <time.h>#define DECK_SIZE 53#define SPADES 6#define HEARTS 3#define DIAMONDS 4#define CLUBS 5#define PEANUTS 10void rules ();void mainMenu();void initializeDeck(int[]);int RandomNumber (int,int);void Shuffle(int[]);void swap (int[],int,int);void displayCard(int);int scoreCard(int);int dealer (int[]);void checkDeck(int[]);void gamePlay();int main(){mainMenu();}void mainMenu() // Function for asking player if they want to continue{int choice;system("cls");printf("nEnter 1 to play Blackjack.");printf("nEnter 2 to See the Rules.");printf("nEnter 3 to Exit Game.");printf("nChoice: ");scanf("%d", &choice); // Prompts user for choiceif((choice<1) || (choice>3)) // If invalid choice entered{printf("nIncorrect Choice. Please enter 1, 2 or 3n");scanf("%d", &choice);}switch(choice)...

  • C++ to processing

    how do you write this in processing:#include <iostream.h> //needed for cin>>#include <stdlib.h> //needed for rand() and srand()#include <time.h> //needed for time()#include <string.h> //needed for strcpy()#include <process.h> //needed for system("cls")//Global variablesint p_ace=0, d_ace=0; //counter for how many aces have been dealt. Initialized to 0int a=0; //index variable needed for card dealingint cards[53]; //array filled with card valueschar names2[53][25]; //Used to display name of cardchar names[53][25] = { //array filled with card names. Used for dealing"Zero","Ace of Spades", "Two of Spades", "Three...

Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.