50 points Ideas: create a Card class with an internal \'Rank\' (A, 2, 3, 4, ..)
ID: 3697956 • Letter: 5
Question
50 points
Ideas:
create a Card class with an internal 'Rank' (A, 2, 3, 4, ..) and 'Suit' (spades, diamonds ...)
Here's one possible idea for a card class (Card.h)
#define CARD_H
#include <string>
using namespace std;
class Card
{
public:
Card();
// set and get the rank, 2=2, 3=3, ... 10=10, 11=J, 12=Q, 13=K, 14=A
void setRank(int r);
int getRank();
// set and get the suit, 1=Spades, 2=Hearts, ...
void setSuit(int r);
int getSuit();
// for debugging, print the suit and rand as integers
void print();
// return the rank and suit as a 'nice' looking string
string toString();
// if you want to use sort() in the STL
bool operator<(const Card &c) const {return myRank < c.myRank;}
private:
int myRank;
int mySuit;
};
Explanation / Answer
#include #include #include struct card { const char *face; const char *suit; const char *color; }; typedef struct card Card; typedef unsigned char pairs; void fillDeck( Card * const, const char *[], const char *[] ,const char *[]); void shuffle( Card * const ); void print( const Card * const ); pairs findpairs(card *hand); /* finds any pairs in a hand */ int main() { int hand,cd,winner; card hands[5][5],handssorted[5][5]; pairs numpairs[5],highest; Card deck[52]; const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven", "Eight", "Nine", "Ten","Jack", "Queen", "King"}; const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"}; const char *color[]= {"Black","Red"}; srand( time( NULL ) ); fillDeck( deck, face, suit, color ); print( deck ); printf(" ---------------------------------------------------------- "); shuffle( deck ); print( deck ); for(cd=0;cdRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.