Home > Blockchain >  How do I get properties for each object in the array?
How do I get properties for each object in the array?

Time:11-23

I'm trying to write a card game and I have this question.

I have a List<int> address1Player; which contains a set of cards with parameters.

I need to pull the parameter of each card from this sheet.

For example, List<int> address1Player contains cards with id 1, 2, 3. I need to find out what colors of cards are in List<int> address1Player. Colors are stored in int.

The color getting parameter looks like this

public int PropertyColor(int address){
    return allProperties[address].GetComponent<Plot>().GetColor();
}

How do I make sure that I end up with an array with the colors of each card?

CodePudding user response:

A List<int> only contains a list of integers - in your case, IDs. You want to store a data structure of colors (and ostensibly, some other values about the cards), so a List is not the collection you want. First, let's think about the data structure that our collection will hold, then we'll come back to the collection.

Our card in our game has at least two properties: ID and an integer based Color. In C#, we write classes or structs to group up logical bundles of properties into an object. It would look (pretty simply) like this:

public struct KorsunsCard
{
   public int Id;
   public int CardColor
}

Now, we have a "card" object that has properties that we can check and set like so:

KorsunsCard greenCard = new KorsunsCard() { Id = 1, CardColor = 6 };
greenCard.CardColor = 5; // change the color to "5"
if (greenCard.Id == 2) { .. do some stuff }

Then, we can have methods just return the entire card:

public KorsunsCard GetCardWithID(int Id) 
{
    KorsunsCard returnCard = ...
    ... we'll get to this part in a moment ...
    return returnCard;
}

Now, about those collections. Selecting a data structure to use is the heart of C#. Collections are "groups" of objects - in our case, KorsunsCards. Each collection can do different things well - Lists can get access to a card by "index" (not Id), iterate over the whole list, sort themselves, etc. Dictionarys are meant for looking up a card by a key, and while they can iterate over the whole dictionary, they aren't meant for that typically so the syntax is a little more involved. Not difficult, just not as easy as a List. You also might want a HashSet, a collection that can only have one unique item - but isn't sorted, like a dictionary.

I can't suggest the best solution, because it depends on your game's rules (will your deck always have the same number of cards? one and only of each kind of card? does a user build their own deck from a pool of available cards?).

Let's start with some cards:

KorsunsCard ace = new KorsunsCard() { Id = 1, Color = 1 };
KorsunsCard deuce = new KorsunsCard() { Id = 2, Color = 2 };
KorsunsCard trey = new KorsunsCard() { Id = 3, Color = 3 };

If you wanted a List, you could declare it and add some values to it like so:

List<KorsunsCard> myDeck = new List<KorsunsCard>();
myDeck.Add(ace);
myDeck.Add(deuce);
myDeck.Add(trey)

int deuceColor = deuce.Color; // deuce's color
return myDeck[0]; // ace, but be aware the list can be shuffled/sorted!

foreach (KorsunsCard kc in myDeck) // iterate on the whole deck
{
    kc.Color = 4; // set the entire decks color to 4 , one at a time
}

The generic collection types Dictionary, HashSet, Queue, Stack may all be relevant for your game, depending on how you typically interact with the deck and the game rules. Hopefully I've given you enough from List that you can go and read up on these other collection types and put them to use.

  • Related