For this assignment, we will be making blackjack. Code is provided. You will need to only implement DeckQueue.cpp. You a
Posted: Thu May 05, 2022 12:43 pm
For this assignment, we will be making blackjack. Code is
provided. You will need to only implement
DeckQueue.cpp. You are given the Card class and a game
driver program.
//Card.h
//DeckQueue.h
//DeckQueue.cpp
//Game.cpp
1 HNm in 10E CÓ GIỌN 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 BSFWNHOS 31 32 33 34 35 36 37 #ifndef CARD_H #define CARD_H #include <string> class Deck; Eclass Card { friend class Deck; friend std::ostream& operator<<(std::ostream& output, const Card* card) { return output << card->toString(); public: Card (int suit, int rank) : suit{ suit }, rank{ rank }{} ~Card() {} 8 int getSuit() { return suit; } 8 int getRank() { return rank; } std::string getSuitString() const { switch (suit) { case 1: return "hearts"; break; case 2: return "diamonds"; break; case 3: return "clubs"; break; case 4: return "spades"; break; default: return "error"; break; +0-0-
std::string getRankString() const { switch (rank) { case 1: return "ace"; break; case 13: return "king"; break; case 12: return "queen"; break; case 11: return "jack"; break; case 10: case 9: case 8: case 7: case 6: case 5: case 4: case 3: case 2: default: return std::to_string(rank); break; return "error"; break; std::string toString() const { return getRankString() + " of " + getSuitString(); int suit; int rank; Card* nextCard{ nullptr }; private: }; #endif
1 2 3 4 5 #ifndef DECKQUEUE_H #define DECKQUEUE_H #include <iostream> #include <string> #include <ctime> #include <random> 7 #include <deque> #include <algorithm> #include <iterator> #include "Card.h" Eclass DeckQueue { public: 10 11 12 13 bool isEmpty(); 14 void addCard (int suit, int rank); 15 void addAllCards (); 16 void removeCard(); 17 ~DeckQueue (); 18 void shuffle(); void print(); 19 Card* drawCard(); int getCount(); std::deque<Card*> myDeck; std::deque<Card*>::iterator int count{0}; std::default_random_engine HNMIST89 6 20 21 22 23 24 25 26 27 28 private: |}; #endif drawPtr { myDeck.begin() }; engine{ static_cast<unsigned int>(time(0)) };
#include <iostream> #include <string> #include <ctime> #include <random> #include "DeckQueue.h" #include "Card.h" 7 using namespace std; 8 Ebool DeckQueue::isEmpty() { return myDeck.empty(); 9 10 11 Evoid DeckQueue::addCard (int suit, int rank) { 12 //TODO 13 | } 14 Evoid DeckQueue::addAllCards () { //TODO 15 16 17 Evoid DeckQueue:: removeCard() { //TODO 18 19 | } 20 DeckQueue::~DeckQueue () { //TODO 21 22 23 Evoid DeckQueue::shuffle() { //TODO 24 25 26 Evoid DeckQueue: :print() { //TODO 27 28 | } 29 ECard* DeckQueue::drawCard() { 30 //TODO 31 return nullptr; 32 } 33 int DeckQueue::getCount() { 34 return count; 35 LEGFWNH 3 4 5 6
#include <iostream> #include <string> #include <vector> #include "Card.h" #include "DeckQueue.h" using namespace std; Bint calculateScore(vector<Card*> hand) { int score = 0; for (Card* card : hand) { if (card->getRank() > 10) { //face cards are 10 score += 10; }| else if (card->getRank() == 1) { //ace is 1 or 11 if (score <= 10) { score += 11; else { score += 1; score += card->getRank(); -0-0 20-0 else { return score;
enum class Win { Player, Dealer, Push }; int main() { int programChoice = 1; DeckQueue myDeck; cout << "For deck testing enter 1 \nFor Blackjack enter 2\n?"; cin >> programChoice; if (programChoice == 1) { cout << "The deck is empty? " << boolalpha << myDeck.isEmpty() << endl; cout << "The deck has " << myDeck.getCount() << " cards" << endl; Card printCard1 = Card(2, 4); //just for printing cout << "Adding the " << printCard1.toString() << " card to the deck" <<< endl; myDeck.addCard(2, 4); //actually add the card Card printCard2 = Card(4, 5); //just for printing cout << "Adding the " << printCard2.toString() << " card to the deck" <<< endl; myDeck.addCard(4, 5); //actually add the card Card printCard3 = Card(3, 7); //just for printing cout << "Adding the " << printCard3.toString() << " card to the deck" <<< endl; myDeck.addCard(3, 7); //actually add the card cout << "The deck has " << myDeck.getCount() << " cards" << endl; cout << "printing deck" << endl; myDeck.print(); cout << "drawing cards from the deck" << endl; Card* drawOne = myDeck.drawCard(); Card* drawTwo = myDeck.drawCard(); Card* drawThree = myDeck.drawCard(); if (drawOne != nullptr && drawTwo != nullptr && drawThree != nullptr) { cout << "drawing " << drawOne->toString() << " from the deck" << endl; cout << "drawing " << drawTwo->toString() << " from the deck" << endl; cout << "drawing " << drawThree->toString() << " from the deck" << endl << endl;
myDeck.shuffle(); cout << "drawing cards from the deck" << endl; drawOne = myDeck.drawCard(); drawTwo = myDeck.drawCard(); drawThree = myDeck.drawCard(); if (drawOne != nullptr && drawTwo != nullptr && drawThree != nullptr) { cout << "drawing " << drawOne->toString() << " from the deck" << endl; cout << "drawing " << drawTwo->toString() << " from the deck" << endl; cout << "drawing " << drawThree->toString() << " from the deck" << endl<< endl; cout << endl<<< "Deleting cards drawn from the deck" << endl; myDeck.removeCard(); myDeck.removeCard(); myDeck.removeCard(); cout << "Making a full deck" << endl; cout << "adding all cards" << endl; myDeck.addAllCards(); cout << "printing deck" << endl; myDeck.print(); myDeck.shuffle(); cout << "printing deck" << endl; myDeck.print(); else { //blackjack int playing = 1; int hit = 1; Win winner = Win::Player; vector<Card*> playerHand; vector<Card*> cpuHand; myDeck.addAllCards(); while (playing == 1) { myDeck.shuffle(); playerHand.clear(); cpuHand.clear(); winner = Win::Player; Card* firstCard = myDeck.drawCard(); if (firstCard == nullptr) { cout << "Implement drawCard and get the first part running before playing blackjack" << endl; exit(0);
playerHand.push_back(firstCard); cout << "Your first card is the " << playerHand.at (0)->toString() << endl; cpuHand.push_back(myDeck.drawCard()); cout << "Dealer's first card is hidden" << endl; playerHand.push_back(myDeck.drawCard()); cout << "Your second card is the " << playerHand.at (1)->toString() << endl; cpuHand.push_back (myDeck.drawCard()); cout << "Dealer's second card is the " cpuHand.at (1)->toString() << endl; int playerScore = calculateScore (playerHand); cout << "Player has " << playerScore << endl; if (playerScore = 21) { winner = Win::Player; cout << "BlackJack! Player wins!" << endl; else { //player's turn while (true) { cout << "Would you like a card? 1 = yes, 2 = no: I cin >> hit; if (hit == 1) { playerHand.push_back(myDeck.drawCard()); cout << "Your card is the " << playerHand.back() -> toString() << endl; playerScore = calculateScore (playerHand); cout << "Player has " << playerScore << endl; if (playerScore > 21) { cout << "Player busts" << endl; winner = Win::Dealer; break; playerScore = calculateScore (playerHand); cout << "Player stays at " << playerScore << endl; break; } else {
Dealer's turn if (winner == Win::Dealer) { 1 cout << "Dealer wins" << endl; } else { cout << "Dealer's hidden card was the " << cpuHand.at (0) -> toString() << endl; int cpuscore = calculateScore (cpuHand); cout << "Dealer has " << cpuScore << endl; while (cpuScore <= 16) { cpuHand.push_back(myDeck.drawCard()); cout << "Dealer draws a " << cpuHand.back() << endl; cpuscore = calculateScore (cpuHand); cout << "Dealer has " << cpuScore << endl; } if (cpuScore > 21) { cout << "Dealer busts" << endl; winner = Win::Player; cout << "Player wins!" << endl; } else if (cpuScore > playerScore) { winner = Win::Dealer; cout << "Dealer wins" << endl; else if (playerScore > cpuScore) { winner = Win::Player; cout << "Player wins!" << endl; else { //playerScore == cpuScore winner = Win::Push; cout << "Push, no one wins" << endl; cout << "Play again? (1 for yes, 2 for no): "; cin >> playing; if (playing != 1) { cout << "Thanks for playing!" << endl;
provided. You will need to only implement
DeckQueue.cpp. You are given the Card class and a game
driver program.
//Card.h
//DeckQueue.h
//DeckQueue.cpp
//Game.cpp
1 HNm in 10E CÓ GIỌN 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 BSFWNHOS 31 32 33 34 35 36 37 #ifndef CARD_H #define CARD_H #include <string> class Deck; Eclass Card { friend class Deck; friend std::ostream& operator<<(std::ostream& output, const Card* card) { return output << card->toString(); public: Card (int suit, int rank) : suit{ suit }, rank{ rank }{} ~Card() {} 8 int getSuit() { return suit; } 8 int getRank() { return rank; } std::string getSuitString() const { switch (suit) { case 1: return "hearts"; break; case 2: return "diamonds"; break; case 3: return "clubs"; break; case 4: return "spades"; break; default: return "error"; break; +0-0-
std::string getRankString() const { switch (rank) { case 1: return "ace"; break; case 13: return "king"; break; case 12: return "queen"; break; case 11: return "jack"; break; case 10: case 9: case 8: case 7: case 6: case 5: case 4: case 3: case 2: default: return std::to_string(rank); break; return "error"; break; std::string toString() const { return getRankString() + " of " + getSuitString(); int suit; int rank; Card* nextCard{ nullptr }; private: }; #endif
1 2 3 4 5 #ifndef DECKQUEUE_H #define DECKQUEUE_H #include <iostream> #include <string> #include <ctime> #include <random> 7 #include <deque> #include <algorithm> #include <iterator> #include "Card.h" Eclass DeckQueue { public: 10 11 12 13 bool isEmpty(); 14 void addCard (int suit, int rank); 15 void addAllCards (); 16 void removeCard(); 17 ~DeckQueue (); 18 void shuffle(); void print(); 19 Card* drawCard(); int getCount(); std::deque<Card*> myDeck; std::deque<Card*>::iterator int count{0}; std::default_random_engine HNMIST89 6 20 21 22 23 24 25 26 27 28 private: |}; #endif drawPtr { myDeck.begin() }; engine{ static_cast<unsigned int>(time(0)) };
#include <iostream> #include <string> #include <ctime> #include <random> #include "DeckQueue.h" #include "Card.h" 7 using namespace std; 8 Ebool DeckQueue::isEmpty() { return myDeck.empty(); 9 10 11 Evoid DeckQueue::addCard (int suit, int rank) { 12 //TODO 13 | } 14 Evoid DeckQueue::addAllCards () { //TODO 15 16 17 Evoid DeckQueue:: removeCard() { //TODO 18 19 | } 20 DeckQueue::~DeckQueue () { //TODO 21 22 23 Evoid DeckQueue::shuffle() { //TODO 24 25 26 Evoid DeckQueue: :print() { //TODO 27 28 | } 29 ECard* DeckQueue::drawCard() { 30 //TODO 31 return nullptr; 32 } 33 int DeckQueue::getCount() { 34 return count; 35 LEGFWNH 3 4 5 6
#include <iostream> #include <string> #include <vector> #include "Card.h" #include "DeckQueue.h" using namespace std; Bint calculateScore(vector<Card*> hand) { int score = 0; for (Card* card : hand) { if (card->getRank() > 10) { //face cards are 10 score += 10; }| else if (card->getRank() == 1) { //ace is 1 or 11 if (score <= 10) { score += 11; else { score += 1; score += card->getRank(); -0-0 20-0 else { return score;
enum class Win { Player, Dealer, Push }; int main() { int programChoice = 1; DeckQueue myDeck; cout << "For deck testing enter 1 \nFor Blackjack enter 2\n?"; cin >> programChoice; if (programChoice == 1) { cout << "The deck is empty? " << boolalpha << myDeck.isEmpty() << endl; cout << "The deck has " << myDeck.getCount() << " cards" << endl; Card printCard1 = Card(2, 4); //just for printing cout << "Adding the " << printCard1.toString() << " card to the deck" <<< endl; myDeck.addCard(2, 4); //actually add the card Card printCard2 = Card(4, 5); //just for printing cout << "Adding the " << printCard2.toString() << " card to the deck" <<< endl; myDeck.addCard(4, 5); //actually add the card Card printCard3 = Card(3, 7); //just for printing cout << "Adding the " << printCard3.toString() << " card to the deck" <<< endl; myDeck.addCard(3, 7); //actually add the card cout << "The deck has " << myDeck.getCount() << " cards" << endl; cout << "printing deck" << endl; myDeck.print(); cout << "drawing cards from the deck" << endl; Card* drawOne = myDeck.drawCard(); Card* drawTwo = myDeck.drawCard(); Card* drawThree = myDeck.drawCard(); if (drawOne != nullptr && drawTwo != nullptr && drawThree != nullptr) { cout << "drawing " << drawOne->toString() << " from the deck" << endl; cout << "drawing " << drawTwo->toString() << " from the deck" << endl; cout << "drawing " << drawThree->toString() << " from the deck" << endl << endl;
myDeck.shuffle(); cout << "drawing cards from the deck" << endl; drawOne = myDeck.drawCard(); drawTwo = myDeck.drawCard(); drawThree = myDeck.drawCard(); if (drawOne != nullptr && drawTwo != nullptr && drawThree != nullptr) { cout << "drawing " << drawOne->toString() << " from the deck" << endl; cout << "drawing " << drawTwo->toString() << " from the deck" << endl; cout << "drawing " << drawThree->toString() << " from the deck" << endl<< endl; cout << endl<<< "Deleting cards drawn from the deck" << endl; myDeck.removeCard(); myDeck.removeCard(); myDeck.removeCard(); cout << "Making a full deck" << endl; cout << "adding all cards" << endl; myDeck.addAllCards(); cout << "printing deck" << endl; myDeck.print(); myDeck.shuffle(); cout << "printing deck" << endl; myDeck.print(); else { //blackjack int playing = 1; int hit = 1; Win winner = Win::Player; vector<Card*> playerHand; vector<Card*> cpuHand; myDeck.addAllCards(); while (playing == 1) { myDeck.shuffle(); playerHand.clear(); cpuHand.clear(); winner = Win::Player; Card* firstCard = myDeck.drawCard(); if (firstCard == nullptr) { cout << "Implement drawCard and get the first part running before playing blackjack" << endl; exit(0);
playerHand.push_back(firstCard); cout << "Your first card is the " << playerHand.at (0)->toString() << endl; cpuHand.push_back(myDeck.drawCard()); cout << "Dealer's first card is hidden" << endl; playerHand.push_back(myDeck.drawCard()); cout << "Your second card is the " << playerHand.at (1)->toString() << endl; cpuHand.push_back (myDeck.drawCard()); cout << "Dealer's second card is the " cpuHand.at (1)->toString() << endl; int playerScore = calculateScore (playerHand); cout << "Player has " << playerScore << endl; if (playerScore = 21) { winner = Win::Player; cout << "BlackJack! Player wins!" << endl; else { //player's turn while (true) { cout << "Would you like a card? 1 = yes, 2 = no: I cin >> hit; if (hit == 1) { playerHand.push_back(myDeck.drawCard()); cout << "Your card is the " << playerHand.back() -> toString() << endl; playerScore = calculateScore (playerHand); cout << "Player has " << playerScore << endl; if (playerScore > 21) { cout << "Player busts" << endl; winner = Win::Dealer; break; playerScore = calculateScore (playerHand); cout << "Player stays at " << playerScore << endl; break; } else {
Dealer's turn if (winner == Win::Dealer) { 1 cout << "Dealer wins" << endl; } else { cout << "Dealer's hidden card was the " << cpuHand.at (0) -> toString() << endl; int cpuscore = calculateScore (cpuHand); cout << "Dealer has " << cpuScore << endl; while (cpuScore <= 16) { cpuHand.push_back(myDeck.drawCard()); cout << "Dealer draws a " << cpuHand.back() << endl; cpuscore = calculateScore (cpuHand); cout << "Dealer has " << cpuScore << endl; } if (cpuScore > 21) { cout << "Dealer busts" << endl; winner = Win::Player; cout << "Player wins!" << endl; } else if (cpuScore > playerScore) { winner = Win::Dealer; cout << "Dealer wins" << endl; else if (playerScore > cpuScore) { winner = Win::Player; cout << "Player wins!" << endl; else { //playerScore == cpuScore winner = Win::Push; cout << "Push, no one wins" << endl; cout << "Play again? (1 for yes, 2 for no): "; cin >> playing; if (playing != 1) { cout << "Thanks for playing!" << endl;