GeraltDieSocke
GeraltDieSocke

Reputation: 1618

How can I change a property to a different value, every time I create a new object from that class?

Today I started a console project with C#. My idea is to set up a class for every card there is in a standard deck of 52 cards. I'm now at implementing my first card "Two" so the card with the value of 2. The problem is a deck contains 4 cards of "Two's" any with a different "color" (eg. club, diamond, heart, spade). So I have to keep track of what the card object I will create later on has which color on it. I absolutely don't know how to do this?

I googled a bit and my idea is to switch the color somehow everytime I create a new object to:
club --> diamond --> hearth --> spade
(then it starts from the beginning when I create a 2nd deck) because a blackjack game contains 6 x a deck of 52 cards.

This is my class so far:

class Two : ICard // thats the card with the value of "One"; 
{
    // in a deck of 52 cards there are 4 cards with "Two" in each color: club, diamond, heart, spade

    public string Name { get => Name;  private set => Name = value; }
    public int Value { get => Value;  private set => Value = value; }
    public int MaxIncludedInDeck { get => MaxIncludedInDeck; private set => MaxIncludedInDeck = value; }
    public string CardColor { get => CardColor; private set => CardColor = value; }

    public Two()
    {
        Name = "One";
        Value = 1;
        MaxIncludedInDeck = 4;

        // TODO: Figure out how to set one object to club, one to diamond, one to hearth and one to spade


    }
}

It just implements an interface with the same methods (I'm practicing the OOP by using an interface).

Maybe you can lead me into the right direction and tell me a few ideas how to tackle that problem? Thanks in advance!

Upvotes: 0

Views: 651

Answers (1)

Ricky Stam
Ricky Stam

Reputation: 2126

You can do something like that: Create a Card class with a constructor that takes the color and value as a parameter. Also create a Deck class that will contain all your 52 Cards and it will be responsible to instantiate your cards. Below you can find some sample code.

public enum CardColor
{
    Club =0,
    Diamond =1,
    Hearth  = 2,
    Spade = 3
}
public class Card
{
    public CardColor CardColor { get; set; }
    public int Value {get;set;}

    public Card(CardColor cardColor,int value) {
        CardColor = cardColor;
        Value = value;
    }
}

public class Deck
{
    public List<Card> Cards { get; set; } = new List<Card>();
    //also instead on having a method to initialize the Deck you can do that
    // in the constructor of the Deck class
    public void InitilizeDeck()
    {
        foreach (var cardColor in Enum.GetValues(typeof(CardColor))) {
            for (int i = 1; i<=10;i++)
            {
                Cards.Add(new Card((CardColor)cardColor, i));
            }
        }
    }
}

  //then use the Deck class like that
  var deck = new Deck();
  deck.InitilizeDeck();
  var cards = deck.Cards;

Upvotes: 1

Related Questions