Card Shuffler

Hi guys, i know there are a thousand posts regarding card games with various implementations but i cant find a solution for the problem in my code after browsing most of the information out there.

my problem lies in the instantiation of the deck class. i step through the code and the loop structures behave as expected and desired. however, for some reason, the card suit is not being passed to the card.

The program is supposed to work as follows: upon execution, you see the GUI form, with a fresh sorted deck of 52 playing cards face up (4x13, two-d array).
there are 2 buttons. one is shuffle. the other is "find card". after you hit find card, it should display a card (card being 2 characters. ex: AS = Ace of Spades. ex: D4 = 4 of Diamonds). the find card button will now say "stop timer". hence a timer is started until you can find and click on the desired card in the shuffled deck.

Ill post code. Ive got some things commented as ive been trying several approaches to instantiate the deck of cards.


globals.h snippet

enum cardSuit {SPADE, CLUB, HEART, DIAMOND};
const int SUITS = 4;
const int VALUES = 13;

Deck.h class snippet:

private:
Card *myCards[SUITS][VALUES]; // our professor made us do it this way.
int r;
int c;

Deck.cpp class snippet:
Deck::Deck()
{
r=0;
c=0;
for(cardSuit curSuit = cardSuit::SPADE; curSuit <=cardSuit::DIAMOND; curSuit++)
{
for(cardNum=1; cardNum <= VALUES; cardNum++)
{
// Card newCd = new Card(curSuit, cardNum);
myCards[r][c] = new Card(curSuit, cardNum);
//newCd.setSuit(curSuit);
//newCd.setValue(cardNum);
myCards[r][c]->setSuit(curSuit);
myCards[r][c]->setValue(cardNum);
//Card::Card(curSuit, cardNum);
c++;
}
c=0;
r++;
}
}

card.h snippet

// Define a postfix increment operator for Suit.
inline cardSuit operator++( cardSuit &rs, int )
{
return rs = cardSuit(rs + 1);
}


It builds but i am getting warnings about the operator++ overload which behaves appropriately upon stepping through. When i examine the deck array when stepping through, i notice the value is being passed but for the suit field it says, error: cannot obtain value.

Incidentally when the program is executed instead of showing the entire deck (each card) face up, it shows me 52, 2 of SPADES.

Also I hope these methods are right:

Deck.cpp snips


Card Deck::GetCard(int row, int col)
{
//Card c; //c = myCards[row][col]; //return c;
return *myCards[row][col];
}
void Deck::setCard(Card inCard, int row, int col)
{
r = row;
c = col;
myCards[r][c] = new Card(inCard.getSuit(), inCard.getValue());
}


Pgrm2.cpp code is called from the form class. you can see the deck object is declared here and various operations performed. again, im not 100% sure on any of this code as the program is not behaving as desired. please feel free to comment/critique anywhere.


Pgrm2.cpp snippet


Card c2;
Deck d1;


//Our prototypes for the functions.
void getCard( char cardString[20], int row, int col);
void shuffleCards();
bool compareCards(char cardString[], int row, int column);


void getCard(char cardString[], int row, int col)
{
d1.setCard(c2, row,col);
c2 = d1.GetCard(row,col);
c2.toCString(cardString);
//call the toCString function on the specified card object.
}

Form1.h snippet

void getCard(char cardString[], int row, int column);
void shuffleCards();
bool compareCards(char cardString[], int row, int column);

namespace Pgrm2 {
.....blah blah blah}

Also, im new to c++ and pointers/references. from what i understand, these concepts can be very useful to this solution. All feedback is welcome and appreciated!

russell

other methods you may find helpful:

Card.cpp snippet

void Card::toCString(char cardString[]) {
static char* suitstring = "SCHD";
static char* facestring = "A23456789TJQK";
cardString[0] = suitstring[suit];
cardString[1] = facestring[value];
cardString[2] = '\0';
}
string Card::Write() const {
static string suitString[4] = { "Spade", "Club", "Hearts", "Diamonds" };
static string faceString[13] = {"Ace", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string outString;
outString = faceString[value] + " of " + suitString[suit];
return (outString);
}
Topic archived. No new replies allowed.