Wow, either you really like unsigned char, or you're coding
for a very memory constrainted machine.
int
s are
normally the preferred arithmetic type. unsigned chars are generally
used for characters which include ASCII values greater than 127.
You need to learn about caasting. Casting allows you to change
the type of a variable or expression. We can shorten your program
considerably by using the properties of card number, a little
arithmetic, and casting. In setFace() we can use the modulo operator
to get the face value and then cast it to your enum. Likewise, in
setSuit() all we have to do is divide card number by 4 then cast it
to the Suit enum.
the binary % operator yields the remainder from the division
of the first expression by the second. |
I think i may have figured a way to keep the pointers instead
of using a vector. |
Why? A vector should always be preferred over pointers.
card.h
L4-5: enum class should be preferred.
L19-20,22: These should be const.
deck.h
L8-11: We don't need these variables at all.
L9: We don't need cards to be dynamically allocated. We can use a
vector. I've renamed this variable to cards.
L15: If you're going to derive another class from Deck, your destructor
should be virtual.
deck.cpp
L8: Do not call srand() multiple times. srand() sets the RNG to a
particular starting point. Calling srand() repeatedly can cause the
RNG to return the same random numbers. srand() should be called ONCE
at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Better yet, use the facilities in <random>.
L21-25: If you don't use dynamic memory, the destructor is not needed.
L42-64: Why are you repeating this logic? You already have the logic to
display a card in the Card classs. Use it.
main.cpp
L9-11: use
constexpr
for constants.
card.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#ifndef CARD_H
#define CARD_H
enum class Face { A, F2, F3, F4, F5, F6, F7, F8, F9, F10, J, Q, K };
enum class Suit { S, C, H, D };
class Card
{
Face face;
Suit suit;
void setFace(int cn);
void setSuit(int cn);
public:
Card();
Card(int cn); //take in counter (cardNum) to set each card
Face getFace() const;
Suit getSuit() const;
int getValu() const;
void Display() const;
};
#endif
card.cpp
[code]
#include <algorithm>
#include <string>
#include <iostream>
#include "Card.h"
using namespace std;
Card::Card()
{}
Card::Card(int cn)
{
setFace(cn);
setSuit(cn);
}
void Card::setFace(int cn)
{ // cn is 1 - 52
face = (Face)(cn % 13);
}
void Card::setSuit(int cn)
{ // cn is 1-52
suit = (Suit)(cn / 4);
}
Face Card::getFace() const
{
return face;
}
Suit Card::getSuit() const
{
return suit;
}
// Return the Blackjack value of the card
int Card::getValu() const
{
return min((int)face, 10);
}
void Card::Display() const
{
const string face_char = "A23456789xJQK";
const string suit_char = "SCHD";
if ((int)face == 10)
cout << "10";
else
cout << face_char[(int)face];
cout << suit_char[(int)suit];
}
|
deck.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef DECK_H
#define DECK_H
#include <vector>
#include "Card.h"
constexpr int CARDS_PER_DECK{ 52 };
class Deck {
protected:
std::vector<Card> cards;
public:
Deck();
virtual ~Deck();
void display();
Face getFace(int crdNum) const;
Suit getSuit(int crdNum) const;
Card deal();
};
#endif
|
deck.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
#include "Card.h"
#include "Deck.h"
Deck::Deck() {
for (int i = 0; i < CARDS_PER_DECK; i++) {
cards.push_back(Card(i));
}
random_shuffle(cards.begin(), cards.end());
}
Deck::~Deck()
{}
Face Deck::getFace(int crdNum) const
{
return cards[crdNum].getFace();
}
Suit Deck::getSuit(int crdNum) const
{
return cards[crdNum].getSuit();
}
void Deck::display() {
for (int i = 0; i < CARDS_PER_DECK; i++)
cards[i].Display();
}
Card Deck::deal()
{
Card card;
card = cards[0];
cards.erase(cards.begin());
return card;
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include "Deck.h"
using namespace std;
int main()
{
constexpr int CARDS_TO_DEAL{ 2 };
Deck deck;
vector<Card> hand;
Card temp;
srand(static_cast<unsigned int>(time(0)));
// Deal two cards to the hard
for (int i = 0; i < CARDS_TO_DEAL; i++)
{
temp = deck.deal();
hand.push_back(temp);
}
//Display hand
for (int i = 0; i < hand.size(); i++)
{
hand[i].Display();
}
//Display
//deck.display();
return 0;
}
|