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
#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)
}