Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
Maevik
Chaos Rift Junior
Posts: 230 Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA
Post
by Maevik » Mon Jan 24, 2011 9:35 pm
I'm trying to create a class Deck (IE a deck of cards) that works with arrays of class Card. The part I'm having trouble with is that I want this deck to work with all kinds of cards and am therefore working with classes that inherit the class Card (In this case a class ResourceCard : public Card.) What I would like to be able to do is initialize an instance of Deck with an array of ResourceCard. Since they will have different memory sizes, I'm not sure polymorphism/casting can help me.
Code: Select all
int main()
{
ResourceCard resourceCard;
ResourceCard resourceCards[5];
//enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };
resourceCard.setType( ANY );
resourceCards[0] = resourceCard;
resourceCard.setType( WATER );
resourceCards[1] = resourceCard;
resourceCard.setType( FOREST );
resourceCards[2] = resourceCard;
resourceCard.setType( HILL );
resourceCards[3] = resourceCard;
resourceCard.setType( MOUNTAIN );
resourceCards[4] = resourceCard;
Deck deck( resourceCards , 5 );
cin.get();
return 0;
}
Code: Select all
//The constructor I'm using
Deck::Deck( Card p_newCards[] , int p_count )
{
cardsInDeck = new Card[DECK_SIZE_LIMIT];
count = 0;
discardedCards = new Card[DECK_SIZE_LIMIT];
discardCount = 0;
lastDrawnCard = NULL;
if( p_count <= DECK_SIZE_LIMIT && p_count > 0)
{
count = p_count;
for( int i = 0 ; i < count ; i++ )
{
cardsInDeck[i] = p_newCards[i]; //This line causes the program crash on it's second iteration
}
}
}
So I realize I'm accessing memory I shouldn't since I'm sending an array of ResourceCard which takes up more memory than regular old Card. My question is, how do I fix this? It seems to me there should be a way to do this (or at least something like this.)
My love is like a Haddoken, it's downright fierce !
adikid89
Chaos Rift Cool Newbie
Posts: 94 Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++
Post
by adikid89 » Tue Jan 25, 2011 5:20 am
use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
thejahooli
Chaos Rift Junior
Posts: 265 Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England
Post
by thejahooli » Tue Jan 25, 2011 12:45 pm
adikid89 wrote: use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
You could just use an array of pointers to ResourceCards, a vector is not necessarily needed. Then you could just do this.
Code: Select all
ResourceCard *resourceCards[5];
resourceCards[0] = new ResourceCard();
resourceCrads[0]->SetType(/*Insert Type*/);
// And so on for all the cards
I'll make your software hardware.
D-e-X
Chaos Rift Newbie
Posts: 39 Joined: Tue Dec 28, 2010 6:49 pm
Post
by D-e-X » Tue Jan 25, 2011 2:26 pm
thejahooli wrote: adikid89 wrote: use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
You could just use an array of pointers to ResourceCards, a vector is not necessarily needed. Then you could just do this.
Code: Select all
ResourceCard *resourceCards[5];
resourceCards[0] = new ResourceCard();
resourceCrads[0]->SetType(/*Insert Type*/);
// And so on for all the cards
Code: Select all
const int NUM_RESOURCE_CARDS = 5;
ResourceCard* resourceCards[NUM_RESOURCE_CARDS];
for(int i = 0; i < NUM_RESOURCE_CARDS; ++i)
{
resourceCards[i] = new ResourceCard;
// codez . . .
}
//moar codez . . .
Because I can
I remember when I used to be into nostalgia.
thejahooli
Chaos Rift Junior
Posts: 265 Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England
Post
by thejahooli » Tue Jan 25, 2011 3:02 pm
D-e-X wrote:
Code: Select all
const int NUM_RESOURCE_CARDS = 5;
ResourceCard* resourceCards[NUM_RESOURCE_CARDS];
for(int i = 0; i < NUM_RESOURCE_CARDS; ++i)
{
resourceCards[i] = new ResourceCard;
// codez . . .
}
//moar codez . . .
Because I can
But then they would have to be the same type, whereas in his example he uses five different types for his cards.
I'll make your software hardware.
GroundUpEngine
Chaos Rift Devotee
Posts: 835 Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK
Post
by GroundUpEngine » Tue Jan 25, 2011 3:51 pm
Your idea works fine (doesn't crash), unless i got the wrong idea here?
Code: Select all
#include <iostream>
using namespace std;
class Card
{
public:
};
enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };
class ResourceCard : public Card
{
public:
Resource res;
void setType(Resource r)
{
res = r;
}
};
#define DECK_SIZE_LIMIT 50
class Deck
{
public:
Card *cardsInDeck, *discardedCards, *lastDrawnCard;
int count, discardCount;
//The constructor I'm using
Deck( Card p_newCards[] , int p_count )
{
cardsInDeck = new Card[DECK_SIZE_LIMIT];
count = 0;
discardedCards = new Card[DECK_SIZE_LIMIT];
discardCount = 0;
lastDrawnCard = NULL;
if( p_count <= DECK_SIZE_LIMIT && p_count > 0)
{
count = p_count;
for( int i = 0 ; i < count ; i++ )
{
cout << "i:" << count << endl;
cardsInDeck[i] = p_newCards[i];
}
}
}
};
int main()
{
ResourceCard resourceCard;
ResourceCard resourceCards[5];
//enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };
resourceCard.setType( ANY );
resourceCards[0] = resourceCard;
resourceCard.setType( WATER );
resourceCards[1] = resourceCard;
resourceCard.setType( FOREST );
resourceCards[2] = resourceCard;
resourceCard.setType( HILL );
resourceCards[3] = resourceCard;
resourceCard.setType( MOUNTAIN );
resourceCards[4] = resourceCard;
Deck deck( resourceCards , 5 );
cin.get();
return 0;
}
However you can use polymorphism, and cast up. then slap a function in, so you can identify the card type;
Code: Select all
// in main function
Card *res = new ResourceCard;
// into class Card
virtual string type()
{
return "Card";
}
// into class ResourceCard
string type()
{
return "ResourceCard";
}