C++ Edit code

Hi guys,

I am learning C++ on my own and i wanna redit the code using classes and heritance to revamp the code below.
example class for the card attributes -suit , - rank, - face
and also class deck to contain shuffle
and a class player with the function to setcard and a function handonecard from the deck.
do share your knowledge with me thanks :slight_smile:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <windows.h>

using namespace std;

struct Card {
    char *face;
    char *pix;
    char *suit;
    };

typedef struct Card card;

void fillDeck(card *, char *[], char *[], char *[]);
void shuffle(card *, int);
void deal(card *);
void WaitKey();

int main()
{
    int shuffled = 1250;
    card deck[52];
    char *face[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    char *pix[] = {"\x03","\x05","\x04","\x06"}; // - Added to original program
    char *suit[] = {"Hearts","Clubs","Diamonds","Spades"};
    time_t t;
    srand((unsigned) time(&t)); // Randomize using time
    cout << "PokerGame Options:" << endl;
    fillDeck(deck, face, suit, pix);
    shuffle(deck, shuffled);
    deal(deck);
    printf("\n\n\t");
    WaitKey();
    return 0;
}

void fillDeck(card *wDeck, char *wFace[], char *wSuit[], char *wPix[])
{
    int i;

    for (i = 0; i < 52; i++)
    {
        wDeck.face = wFace[i % 13];
        wDeck.pix  = wPix[i / 13]; // Added
        wDeck.suit = wSuit[i / 13];
    }
}

void shuffle(card *wDeck, int shuffled)
{
    int i, j, x;
    Card temp;
    for (x = 0; x < shuffled ; x++) // A big shuffle
    {
        for (i = 0; i < 52; i++)
        {
            j=rand() % 52;
            temp = wDeck;
            wDeck = wDeck[j];
            wDeck[j] = temp;
        }
    }
}
 
void deal(card *wDeck)
{
    int i;
        for (i = 0; i <= 4; i++)
        {
            if ( wDeck.pix == "\x03"||wDeck.pix== "\x04" )
                SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 4);
            else
                SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 8);
                printf("%5s%s %c", wDeck.face, wDeck.pix, (i + 1) % 5 ? '\b' : '\n');
        }
}

void WaitKey()
{
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 8);
cout << "\t\t   Press ENTER to continue...\n\t\t";
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}

I see no point giving a different class to each kind of card when it could just as easily be done using a class variable. Adds so much extra code and complication to a simple problem. But as a teaching example, I suppose:

enum suit { HEART, CLUB, DIAMOND, SPADE, NONE };

class card
{
public:
        card(const char *_name, int _suit):name(_name), suit(_suit) { }

        const char *Name() { return(name); }
        int Suit() { return(suit); }
        // Has to be virtual if we want it to be overloaded
        virtual bool FaceCard() { return(false); }
private:
        const char *name;
        int suit;
};

class facecard
{
public:
        facecard(const char *_name, int _suit):card(_name,_suit) { }
        // Virtual, so this overloads the one in 'card'
        virtual bool FaceCard() { return(true); }
};

class deck
{
public:
        add(card *c) { cards.push_back(c); }
        card *pick(void) { cards.pop_back(); }
        void shuffle(int shuffled=1250)
        {
          int i, j, x;
          card *temp;
          for (x = 0; x < shuffled ; x++) // A big shuffle
          {
              for (i = 0; i < deck.size(); i++)
              {
                  j=rand() % 52;
                  temp = deck;
                  deck = deck[j];
                  deck[j] = temp;
              }
          }
        }
private:
    vector<card *> cards;
};

int main(void)
{
        deck d;
        d.add(new facecard("King", CLUB));
}