Struggling to Create Efficient Card Game Class

Hello all! I'm working on a card game and right now I'm trying to create the basic card/deck logic for a Trading Card Game (TCG) but so far I'm struggling to come up with the best way to approach it. Each card (300 unique cards in all) has a unique card ID as well as 10 other various attributes. I'm looking for guidance on the best (efficient) way to store these cards and then be able to either iterate through the full set of 300, pick a card at random, or pick a specific card along with its attributes.. So far I have tried arrays with a switch statement to select the current card within a function, I've tried vector, set, map, unorderd_map but nothing seems (in my experience of testing) to be able to handle the job without creating extremely inefficient functions, etc. Any help would be GREATLY appreciated!!!
When you say "pick a specific card", what do you mean? How are you specifying that card? By position in the deck (i.e. "show me the 7th card")? By name ("show me the Goblin card")? By ID? Something else?

Also, what makes you think your functions are inefficient? Have you profiled them?
Last edited on
Alright, let me try to elaborate a bit. The root of my problem is being unsure as to what would be a "proper" Card class. My initial thought was, within a function, assigning a string array[10] to each card then using a switch statement with a "current card" variable to set the current card to the selected switch case. Such as (going with your example of a Goblin card) something along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string currentCard[10];

// array elements are in the order: cardID, Type, Level, Name, Card Type, Race, Class, Effect, Attack, Speed, Defense
string card1[10] = {"1", "White", "1", "Goblin Card", "Creature", "Goblin", "Magi", "This Goblin Card does something", "1", "1", "1"};

void setCurrentCard(int cardNumber) {
    case 1:
        currentCard = card1;
        break;
    case 2:
        // etc.
    default:
        // default case
        break;
}


The problem I've noticed is that this way does not provide very much flexibility (if any) taking into consideration that the ultimate goal is for this to be a two-player turn-based trading card game. The biggest hurdle in my mind so far is going to be the actual card class and the deck setup. I've even looked into other libraries/card classes but they are all for games like Solitaire which is very straightforward programmatically since there are exactly four suits for each card value...in my case it's (I believe) much more complex which would require more intuitive planning on my part.

Also, above I have listed a string array as currentCard where my ultimate goal would of course be to have it be of class Card instead once I figure out how to hammer out the class structure.
@ljamison

You could create a struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
.
.
struct card
{
  int Card_ID;
  string Type;
  int Level;
  string Name;
  string Card_Type;
  string Race;
 string Class;
 string Effect;
 int Attack;
 int Speed;
 int Defense;
 }


In main(), you can specify how many cards there are, as in, card Deck[300]
read in a text file with all the attributes of each card, and fill each card. To use the information, you can cout << Deck[x].Level

Look over my Periodic Table program that uses this idea for a better understanding ..
http://www.cplusplus.com/forum/general/89585/
Last edited on
@whitenite1

OH MY WORD!!!

At first glance, this looks EXACTLY like what I would need! I had consulted a colleague of mine (he has a PhD in Computer Science so he was the first obvious go-to person) and he suggested I use a text file to feed each card but I was not exactly sure how to approach feeding the file into the program! I never thought of using a struct! I'll be trying out your program so I can get a feel for how it functions and I'll let you know. Thank you for your input!
Topic archived. No new replies allowed.