Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone make this main.cpp work properly ?? Following code need to have shuffle function in DeckOfCard. cpp / DeckOfCard.h (( A shuffle function that

Can anyone make this main.cpp work properly ??

Following code need to have shuffle function in DeckOfCard.cpp / DeckOfCard.h

(( A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the array of Cards. For each Card, randomly select another Card in the deck and swap the two Cards. ))

And then the main.cpp need to show 52 random cards

=======================

//main.cpp #include #include #include "DeckOfCards.h" // DeckOfCards class definition

int main() { DeckOfCards myDeckOfCards; // print all 52 Cards in the order in which they are dealt for (int i = 1; myDeckOfCards.moreCards() && i < 25; ++i) { // deal and display a Card std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 0) // output newline every 4 cards std::cout << std::endl; } // end for std::cout << std::endl; DeckOfCards newDeckOfCards; for (int i = 1; newDeckOfCards.moreCards() && i < 55; ++i) { // deal and display a Card std::cout << std::left << std::setw(19) << newDeckOfCards.dealCard().toString(); if (i % 4 == 0) // output newline every 4 cards std::cout << std::endl; } // end for } // end main --------------------------------- //Card.cpp #include "Card.h"

Card::Card(int cardFace, int cardSuit) { face = cardFace; suit = cardSuit; }

std::string Card::toString() const { std::string SC = ""; SC = SC + faceNames[face] + " of " + suitNames[suit]; return SC; }

const char* Card::faceNames[totalFaces] = { "Ace", "Deuce", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

const char* Card::suitNames[ totalSuits ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

int Card::getFace() const { return face; }

int Card::getSuit() const { return suit; } ---------------------------------------- //Card.h #ifndef CARD_H #define CARD_H

#include

class Card { public: static const int totalFaces = 13; // total number of faces static const int totalSuits = 4; // total number of suits Card(int cardFace, int cardSuit); // initialize face and suit std::string toString() const; // returns a string representation // of a Card // get the card's face int getFace() const; // get the card's suit int getSuit() const; private: int face; int suit; static const char* faceNames[totalFaces]; static const char* suitNames[totalSuits]; }; // end class Card

#endif ------------------------------------------------------- //DeckOfCards.cpp #include "DeckOfCards.h"

DeckOfCards::DeckOfCards() { currentCard = 52; for (int i = 3; i >= 0; i--) { for (int j = 12; j >= 0; j--) { deck.push_back(Card(j, i)); } } }

bool DeckOfCards::moreCards() const { return (currentCard > 0); }

Card DeckOfCards::dealCard() { return deck[--currentCard]; } ------------------------------------------------- //DeckOfCards.h #ifndef DECK_OF_CARDS_H #define DECK_OF_CARDS_H

#include #include "Card.h"

// DeckOfCards class definition class DeckOfCards { public: DeckOfCards(); // constructor initializes deck Card dealCard(); // deals cards in deck bool moreCards() const; // are there any more cards left private: std::vector< Card > deck; // represents deck of cards unsigned currentCard; // index of next card to be dealt }; // end class DeckOfCards

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

Students also viewed these Databases questions