Question
How can I change the value of each card string to an int rummy value deckOfCards.h: #ifndef H_deckOfCards #define H_deckOfCards #include card.h #include #include #include
How can I change the value of each card string to an int rummy value
deckOfCards.h:
#ifndef H_deckOfCards #define H_deckOfCards #include "card.h" #include
using namespace std;
const int CARDS_PER_DECK = 52;
class deckOfCards { public: deckOfCards(); void shuffle(); card dealCard(); void printDeck() const; private: card *deck; int currentCard; };
void deckOfCards::printDeck() const { cout << left; for (int i = 0; i < CARDS_PER_DECK; i++) { cout << setw(19) << deck[i].print(); if ((i + 1) % 4 == 0) cout << endl; } }
deckOfCards::deckOfCards() { string faces[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; string suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; deck = new card[CARDS_PER_DECK]; currentCard = 0; for (int i = 0; i < CARDS_PER_DECK; i++) //populate deck in order deck[i] = card(faces[i % 13], suits[i / 13]); }
void deckOfCards::shuffle() { currentCard = 0; for (int first = 0; first < CARDS_PER_DECK; first++) { int second = (rand() + time(0)) % CARDS_PER_DECK; card temp = deck[first]; deck[first] = deck[second]; deck[second] = temp; // } } card deckOfCards::dealCard() { if (currentCard > CARDS_PER_DECK) shuffle(); if (currentCard < CARDS_PER_DECK) return (deck[currentCard++]); return (deck[0]); }
#endif
card.h:
#ifndef H_card #define H_card #include
using namespace std;
class card { public: card(string cardFace, string cardSuit); string print() const; card(); private: string face; string suit; }; card::card() { }
card::card(string cardFace, string cardSuit) { face = cardFace; suit = cardSuit; } string card::print() const { return (face + " of " + suit); } #endif // !H_card
main.cpp:
#include "deckOfCards.h" #include
using namespace std;
int main() { deckOfCards deck; card currentCard; card compcurrentCard; card stackcurrentCard; string playerHand[10]; string computerHand[10]; string stack[32]; deck.shuffle(); deck.dealCard(); for (int i = 0; i < 3; i++) { if (i == 0) { for (int j = 0; j < 10; j++) { currentCard = deck.dealCard(); string arrayCard = currentCard.print(); playerHand[j] = arrayCard; cout << playerHand[j] << endl;
} cout << endl; } else if (i == 1) { for (int r = 0; r < 10; r++) { currentCard = deck.dealCard(); string compArrayCard = currentCard.print(); computerHand[r] = compArrayCard; cout << computerHand[r] << endl; } cout << endl; } else { for (int k = 0; k < 32; k++) { currentCard = deck.dealCard(); string stackCard = currentCard.print(); stack[k] = stackCard; cout << stack[k] << endl; } } } system("Pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started