Question: create blackjack game in c++ I'm missing something I just don't know what. please add comment lines too please Here is the code for Card.h

create blackjack game in c++ I'm missing something I just don't know what. please add comment lines too please

Here is the code for Card.h:

#include //Required for toupper() #include //Required for string #include //Required for ostream #include //Required for Vector #include //Required for time() #include //Required for random_shuffle() using namespace std; class Card { public: //Constructors Card(); //default Card(char asuit, int aRank); //parameterized //Accessors int getRank() const; char getSuit() const; void displayCard(ostream& outstream) const; //Comparator bool operator<(const Card& c) const; bool operator>(const Card& c) const; bool operator==(const Card& c) const; friend ostream &operator<<(ostream &output, const Card &c) { output << "Player 1 draws "; output << "Player 2 draws "; return output; } private: char suit; int rank; };

Here is the code for Card.cpp:

#include "Card.h" /*------------------------------------------------------------------*/ //Card.cpp - Card class implementation// /*-----------------------------------------------------------------*/ //Constructors Card::Card() :rank(2), suit('S') { } Card::Card(char ch, int i) : rank(i) { suit = toupper(ch); } //Accessor Methods int Card::getRank() const { return rank; } char Card::getSuit() const { return suit; } //Formatted Display Method void Card::displayCard(ostream& out) const { string suitString; switch (suit){ case 'S': suitString = "Spades"; break; case 'H': suitString = "Hearts"; break; case 'D': suitString = "Diamonds"; break; case 'C': suitString = "Clubs"; break; default: suitString = "Invalid Suit"; } if (rank >= 2 && rank<11){ out << rank << "of" << suitString; } else{ switch (rank){ case 11: out << "JACK of " << suitString; break; case 12: out << "Queen of " << suitString; break; case 13: out << "KING of " << suitString; break; case 14: out << "ACE of " << suitString; break; } } return; } bool Card::operator==(const Card& c) const { Card card1; Card card2; if (card1 == card2) { return true; } else { return false; } } bool Card::operator>(const Card& c) const { Card card1; Card card2; return card1 > card2; } bool Card::operator<(const Card& c) const { Card card1; Card card2; return card1 < card2; }

Here is the code for CardDeck.h:

#include "Card.cpp" /*----------------------------------------------------------------*/ //CardDeck.h /*-----------------------------------------------------------------*/ class CardDeck{ public: CardDeck(); void shuffleDeck(); bool isEmpty() const; //return true if empty Card draw(); private: vector theDeck; vector deltCards; };

Here is the code for CardDeck.cpp:

#include #include "CardDeck.h" using namespace std; /*------------------------------------------------------------------------*/ /*CardDeck.cpp */ /*------------------------------------------------------------------------*/

CardDeck::CardDeck(){ /*13 cards in each suit */ /*52 new card */ for (int i = 2; i<15; ++i) { theDeck.push_back(Card('S', i)); theDeck.push_back(Card('H', i)); theDeck.push_back(Card('D', i)); theDeck.push_back(Card('C', i)); } srand(time(NULL)); //Must seed RNG 1time! } Card CardDeck::draw(){ /*Draw and return one card. */ if (theDeck.empty()) { exit(1); } Card aCard = theDeck.back(); //Draw card. theDeck.pop_back(); //Remove card //Retain card for shuffle. deltCards.push_back(aCard); return(aCard); } void CardDeck::shuffleDeck() { /*Replace drawn cards */ for (int i = 0; i

And the Main.cpp is:

class Player { public: Player(CardDeck&); Card draw(); void addpoints(int); int score() const; void replaceCard(CardDeck&); private: Card myCards[3]; int myScore; int removedCard; }; Player::Player(CardDeck &aCardDeck) { myScore = 0; for (int i = 0; i < 3; i++) myCards[i] = aCardDeck.draw(); removedCard = 0; } Card Player::draw() { removedCard = rand() % 3; return myCards[removedCard]; } void Player::addpoints(int howMany) { myScore += howMany; } int Player::score() const { return myScore; } void Player::replaceCard(CardDeck& aCardDeck) { myCards[removedCard] = aCardDeck.draw(); }

int main() { CardDeck theDeck; theDeck.shuffleDeck(); Player player1(theDeck); Player player2(theDeck); while (! theDeck.isEmpty()) { Card card1 = player1.draw(); cout << "Player 1 draws " << card1 << endl; Card card2 = player2.draw(); cout << "Player 2 draws " << card2 << endl; if (card1.getRank()==card2.getRank()) { player1.addpoints(1); player2.addpoints(1); cout << "Players tie" << endl; } else if (card1.getRank()>card2.getRank()) { player1.addpoints(2); cout << "Player 1 wins round" << endl; } else { player2.addpoints(2); cout << "Player 2 wins round" << endl; }

player1.replaceCard(theDeck); player2.replaceCard(theDeck); } cout << "Player 1's score is " << player1.score() << endl; cout << "Player 2's score is " << player2.score() << endl; getchar(); //getchar(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!