Hi,
First some ideas about naming, it's important in C++ - it can affect the design:
The Cards class should be IMO
Card
, because it is a single card.
Name your files after the class, as in Card.hpp (header file with cpp code) and Card.cpp.
Try to avoid single char names for variables, choose something meaningful, as in
FaceValue
,
SuitValue
. You need to change your class declaration in the header file to alter this. If you can choose something better as a name than that then go for it !
IMO, you don't need a separate function to initialise a card. You can do it with the constructor. Funnily enough this will be named
Card::Card
.
In the constructor, you need to set the values for all the member variables. It's a bit confusing to name the arguments to the constructor the same as the class members. I like to append "Arg" to them, as in:
1 2 3 4 5 6
|
Card::Card(const Face FaceArg, const Suit SuitArg)
// set member variable values with member initialization list
: // colon introduces member initialization list
FaceValue(FaceArg),
SuitValue(SuitArg)
{}
|
So that code you can get rid of all your switch statements in the constructor.
However, you will need them in your
toString
function. If a function doesn't alter the state of a class (i.e alter the value of member variables) then the function should be marked
const
:
1 2
|
string Cards::toString() const
{
|
Your switch statements are incorrect at the moment, in each case, you should assign a value to a string variable for the face value, then append another string to it for the Suit, then return the whole string.
in main, what is the value of the variable
s
on line 15? Always initialise your variables to something, the compiler should have warned about that.
deck
is not a good name for a variable on line 15, it's just a single card.
In an
enum
, one can specify values which are different to the default ones:
1 2
|
enum Face { three=3, four, five, six, seven, eight, nine, ten, jack, queen, king, joker }; // joker is 14
enum Suit { diamonds=20, spades, hearts, stars, clubs }; // clubs is 24
|
Does
star mean a wild card, as in any suit? If so, generate one with a random number. Also need to select any suit if the joker is the current card.
A good way to initialise the whole deck, is to loop from 0 to 52, then use integer division to get the
SuitValue
, and modulus to get the
FaceValue
Good Luck !!