Using standard libraries such as stdio and stdlib and such a

Using standard libraries such as stdio and stdlib and such. and other things such as functions and arrays ---->

Problem Description Many assignments for novice programmers involve card games. Central to the development of these games is the use of a virtual deck of playing cards that accurately emulates a real deck. A deck of playing cards is composed of 52 cards with thirteen ascending face values of 2, 3, 10, Jack, Queen, King and Ace, and four suites of Clubs, Spades, Diamonds and Hearts 13 faces x4suites 52 cards. To represent a card deck in our code, we will use an array of 52 integers, where the face value and the suite are encoded into the stored number and extracted using integer math. This can be done in two ways: (i) Assume our array counts from 0 to 51, and our interpretation is that this ascending set of numbers represents each face value in all four suites, followed by the next face value in all four suites, etc. Thus, we may obtain the face value by integer division of 4 and adding 2. The suite is obtained by arbitrarily assigning 0 to Clubs, 1 to Spades, 2 to Diamonds and 3 to Hearts. The suite is then found as the remainder when dividing by 4 (using the modulo operator) (ii) Assume our array assigns the 52 cards the following numbers with all aces being low: 0-12 (diamonds), 13-25 (hearts), 26-38 (clubs), and 39-51 (spades). So the ace of diamonds is assigned the value 0, the jack of clubs is assigned the value 36 etc. In this case, face and suite are determined via integer math using 13 divide by 13 for suite and modulus 13 to get face.

Solution

#ifndef StdCardDeckH

#define StdCardDeckH

#include <string>

#include <sstream>

#include <algorithm>

const char HEART   = 0x03;   // May Not Work on

const char DIAMOND = 0x04;   // certain systems

const char CLUB    = 0x05;   // These are DOS standard

const char SPADE   = 0x06;   // ASCII characters

struct Card {

    char Suit;

    int trueVal;

    std::string faceVal;

    std::string fullName;

};

class Deck

{

public:

    Deck(int DeckSize);

   ~Deck(void);

    void Shuffle(int passes);

    Card Deal(void);

    void Collect(void);

private:

    Card *CardDeck;

    int CurrCard;

    const int DECK_SIZ;

};

Deck::Deck(int DeckSize = 1) : DECK_SIZ( DeckSize ) {

std::ostringstream sConv;

int CurrSuit, CurrDeck;

CardDeck = new Card[52 * DECK_SIZ];

for (CurrDeck = 0; CurrDeck < DECK_SIZ; CurrDeck++) {

    for (CurrSuit = 0; CurrSuit < 4; CurrSuit++) {

      for (CurrCard = 0; CurrCard < 13; CurrCard ++) {

        if ((CurrCard + 1) % 13 == 1) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].faceVal = \'A\';

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName = \"Ace of \";

           }

        else if ((CurrCard + 1) % 13 == 11) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].faceVal = \'J\';

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName = \"Jack of \";

           }

        else if ((CurrCard + 1) % 13 == 12) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].faceVal = \'Q\';

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName = \"Queen of \";

           }

        else if ((CurrCard + 1) % 13 == 0) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].faceVal = \'K\';

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName = \"King of \";

           }

        else {

           sConv << (CurrCard + 1) % 13;

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].faceVal = sConv.str();

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName = sConv.str() + \" of \";

           sConv.str(\"\");

        }

        if (CurrSuit == 0) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].Suit = HEART;

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName += \"Hearts\";

           }

        else if (CurrSuit == 1) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].Suit = DIAMOND;

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName += \"Diamonds\";

           }

        else if (CurrSuit == 2) {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].Suit = CLUB;

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName += \"Clubs\";

           }

        else {

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].Suit = SPADE;

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].fullName += \"Spades\";

           }

        if ((CurrCard + 1) % 13 < 10)

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].trueVal = (CurrCard + 1) % 13;

        else

           CardDeck[CurrCard + (CurrSuit * 13) + (CurrDeck * 52)].trueVal = 10;

      }

    }

}

CurrCard = 0;

}

Deck::~Deck(void) {

delete[] CardDeck;

}

void Deck::Shuffle(int passes = 1) {

int currShuffle;

for (currShuffle = 0; currShuffle < passes; currShuffle++)

      std::random_shuffle(CardDeck, CardDeck+DECK_SIZ*52);

}

Card Deck::Deal(void) {

if ( CurrCard < DECK_SIZ * 52)

     return CardDeck[CurrCard++];

}

void Deck::Collect(void) {

   CurrCard = 0;

}

#endif

Using standard libraries such as stdio and stdlib and such. and other things such as functions and arrays ----> Problem Description Many assignments for novi
Using standard libraries such as stdio and stdlib and such. and other things such as functions and arrays ----> Problem Description Many assignments for novi
Using standard libraries such as stdio and stdlib and such. and other things such as functions and arrays ----> Problem Description Many assignments for novi
Using standard libraries such as stdio and stdlib and such. and other things such as functions and arrays ----> Problem Description Many assignments for novi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site