Page 1 of 1

PostPosted: Sun Jan 28, 2007 7:27 pm
by sangahm
What is the best way to create a Card and Deck object for a card game? I'm stumped with the limitation of multidimensional arrays, and have tried several different ways to do it, none successful. I'm sure it's just my understanding at this point. Can someone see where I'm going wrong and help out?

object Card {
// public interface
int suit; // card suit SPADES thru CLUBS
int value; // value of card from 1 to 13
void init(int theValue, int theSuit);
// private interfaces

}; // end Card object definition

// global card instance
Card card;

// The Deck object handles the complete deck of cards
object Deck {
// public interface
void init();
void shuffle();
// put all the used cards back into the deck
// and shuffle it into a random order

// private methods

// global deck instance
Deck deck;

//set up instance of deck
void Deck.init() {
Card deck[52];
int cardCt = 0; //keeps count of how many cards have been created

//set up all of the cards in the deck
for (suit = 0; suit <= 3; suit++)
for (value = 1; value <= 13; value++)
{ deck[cardCt] = card(value,suit);

When I try and compile, I get an error "cannot call a non-funcptr type" at the line above in Deck.init at deck[cardCt]=card(value,suit)

Any assistance?


PostPosted: Sun Jan 28, 2007 11:00 pm
by jobie
Looks like the line should read:

{ deck[cardCt] = card<b>.init</b>(value,suit);


PostPosted: Mon Jan 29, 2007 12:34 am
by jobie
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by jobie</i>
<br />Looks like the line should read:

{ deck[cardCt] = card<b>.init</b>(value,suit);

<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">

Follow up to my own reply:

There seems to be some other issues with the code, I should have
looked a little closer before responding.

First of all, the line in question should be:

deck[cardCt].init( value, suit );

You do not need a global Card instance, as far as I can tell.

You should not declare a local variable "deck" and a global variable "deck". I use a Hungarian-style notation for globals, in your case it would be g_Deck for your global deck. That's just a personal preference, but definitely do yourself a favor and make them different.

Aside from this, the object definitions look reasonable.


PostPosted: Mon Jan 29, 2007 1:08 am
by sangahm
You've given me some things to think about. Thanks. Now I'm off to see if I can figure it out.

PostPosted: Mon Jan 29, 2007 11:23 pm
by sangahm
OK, back for more if you can still help out.

How do I call the method to initialize the deck?
How do I reference an individual card in the deck?

I thought it would be something like this:

deck.init(); //initialize the cards in the deck by putting 52 cards in

// loop through and print out the cards to proof that they are there
for (loopAllCards = 0; loopAllCards <= 51; loopAllCards++)
{ iValue = deck[loopAllCards].value;
iSuit = deck[loopAllCards].suit;
alert("Posn "+loopAllCards+"Suit "+iSuit+" Value "+iValue); //show each card

This obviously doesn't work, and it shows that I really don't understand this topic of objects too well. But I'm gonna keep struggling through....

PostPosted: Tue Jan 30, 2007 1:23 am
by sangahm
Cancel that request.

I reread your comments about global instance and fixed the problem. I think I'm getting it.