I am making a simple blackjack game. I have now made the deck which i can draw cards from. But I don't know how to get the values from each card so that i can add them together.

I have a card class named Kort with the enum:

public enum KortVerdi { To, Tre, Fire, Fem, Seks, Sju, Åtte, Ni, Ti, Knekt, Dame, Konge, Ess };
public enum KortFarge { Hjerter, Kløver, Ruter, Spar };

And when i draw two cards with my draw method from my main i would like to get the values of the two cards so that i can add them together.

My main looks like this now:

static void Main(string[] args)
        {
            Kortstokk mineKort =new Kortstokk();

            string kort1 = Convert.ToString(mineKort.Trekk());
            string kort2 = Convert.ToString(mineKort.Trekk());

            Console.WriteLine("Du har fått dette: " + kort1 + " og " + kort2);
            Console.WriteLine(kort1 + " " + kort2);
            Console.ReadLine();
        }

Recommended Answers

All 5 Replies

You can set the values of the enums so you never have a zero value.
You can give each card a value that you want.
You can have repeating values.
You can cast the enums as integers or other numeric types.

Thanks for the reply!
I have changed the values for the cards like this:

public enum KortVerdi { To=2, Tre=3, Fire=4, Fem=5, Seks=6, Sju=7, Åtte=8, Ni=9, Ti=10, Knekt=10, Dame=10, Konge=10, Ess=11 };

But when i draw the cards in my main i get output like "Two of Diamonds". How can I use this output to get the values from the enum?

Perhaps this will help you out:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enumtest
{
    class Program
    {
        public enum KortVerdi { To = 2, Tre = 3, Fire = 4, Fem = 5, Seks = 6, Sju = 7, Åtte = 8, Ni = 9, Ti = 10, Knekt = 10, Dame = 10, Konge = 10, Ess = 11 };
        static void Main(string[] args)
        {
             
            System.Array intAr = Enum.GetValues(typeof(KortVerdi));
            string str = intAr.GetValue(9).ToString(); //the variable str will contain "Knekt"
            
            int KortVal = (int)KortVerdi.Knekt;         // KortVal will contain the int value 10
           
            Console.ReadLine();
        }
    }
}

Success!

I like @ddanbe's example and it might work for your needs.

You could also use a dictionary depending on how much data you will have.
Since your "key" might have repeating values, I recommend an ILookup or a List of KeyValuePairs.
Here's an example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_409885_CS_CON
{
   using KVP_I2S = KeyValuePair<int, string>;
   using LST_KVP_I2S = List<KeyValuePair<int, string>>;
   class Program
   {
      static void Main(string[] args)
      {
         LST_KVP_I2S lst_kvp_i2sCards = new LST_KVP_I2S()
         {
            new KVP_I2S(2, "Two of Spades"),
            new KVP_I2S(2, "Two of Hearts"),
            new KVP_I2S(2, "Two of Clubs"),
            new KVP_I2S(2, "Two of Diamonds"),
            new KVP_I2S(3, "Three of Spades"),
            new KVP_I2S(3, "Three of Hearts"),
            new KVP_I2S(4, "Three of Clubs"),
            new KVP_I2S(4, "Three of Diamonds"),
            new KVP_I2S(11, "Ace of Spades"),
            new KVP_I2S(11, "Ace of Hearts"),
            new KVP_I2S(11, "Ace of Clubs"),
            new KVP_I2S(11, "Ace of Diamonds")
         };
         // Show the names of the cards that are less than an Ace
         foreach (KVP_I2S kvp in lst_kvp_i2sCards.Where(i => i.Key < 11))
         {
            Console.WriteLine(kvp.Value);
         }
      }
   }
}

If this is too complex, please let me know.

You could also use a class to hold each card and the properties of the class will hold the necessary strings and values.

Thanks for all the help! I figured it out now. I got the values i needed and added them together like this:

Kort.KortType kort1 = mineKort.Trekk();
Kort.KortType kort2 = mineKort.Trekk();

int verdi1 = Convert.ToInt32(kort1.Verdi);
int verdi2 = Convert.ToInt32(kort2.Verdi);
int sum = verdi1 + verdi2;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.