Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use multiple Vectors or Enum states

Tags:

c++

enums

vector

So I'm working on a simple card game. Right now I'm using about 4 different Vector<Card> containers to hold cards in the Deck, Hand, InPlay, and Discard. I was about to start writing the code to move cards from the InPlay Vector to the Discard Vector when necessary and I realized that I could probalby use 2 vectors and Enumerate a set of Card States to differentiate where the cards are in the game field.

I was thinking something like:

class Card
{
    Enum class CardState
    {
        InHand,
        InPlay,
        Discarded
    };

    //other card class data
};

then I could iterate through a single vector<Card> InPlay and check against those flags. The deck would still remain its own vector container.

Would this be easier then trying to manage 3 different containers throughout the game?

like image 889
Prototype958 Avatar asked Dec 07 '25 00:12

Prototype958


2 Answers

Welcome to cpu time vs memory space vs code maintenance.

Lucky for you, the problems with cpu time and memory space are negligible in this case.

Code maintenance, however, is not.

Trying to maintain three different "stacks" of cards is not the best way to go about the design. This will give you a very, i.e. not noticeable, boost in speed but the increased costs of code maintenance make it not even close to worth it.

What you are doing is correct.

Keep an enum of the states a card can be in. If you need a list of all of the cards in that state, iterate over your vector to find the cards in that state.

like image 180
Captain Skyhawk Avatar answered Dec 08 '25 14:12

Captain Skyhawk


If i was to implement it, i would rather create a deck object, a player object and create methods for any action possible in the game. do a little more work at the beginning and then it would be much easier to handle.

P.s: the data structure should be chosen by what actions do u do with the cards.. for example if in your game u have many queries of "does the deck has this card?" i would say use a map instead of iterating over 50 cards everytime... so the answer is it depends.

like image 26
Tom Avatar answered Dec 08 '25 14:12

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!