0

I have found similar issues on this forum but none that quite answer my question. I'm trying to write my first basic program, a simple blackjack game.

My idea is to use an enum for the suits and values;

private enum CardValue    //enum for card values
    {
        Two = 2,
        Three,Four,Five, Six, Seven,
        Eight,Nine,Ten,
        Jack = 10,
        Queen = 10,
        King = 10,
        Ace
    }

I then make a 52 element array of this type to represent the deck and shuffle it. The problem is that when I print out the shuflled values 'Jack' 'Queen' and 'King' all print out as 'jack', I'm assuming because they all have the same underlying INT value. I want to be able to do it this way to cast the values back to INT to add up the totals in the game.

5
  • 3
    Logically, you cannot do this. How can your program magically know that one 10 is a Jack, and another 10 is a King? It's the same pattern of bits, they cannot be distinguished. Sounds like you need to use a class instead of an enum. Or give your enum values unique integers, and define a Dictionary<Card, int> to record their values. Commented Aug 7, 2013 at 3:16
  • Just make your enum int values unique and while casting you can check if the value is greater than 10 assign it 10. Commented Aug 7, 2013 at 3:17
  • 1
    @Jatin patil: that would be terrible unreadable and error prone Commented Aug 7, 2013 at 3:17
  • @Jatinpatil - That would introduce extra unnecessary code and defeat the purpose of an enum for what OP is trying to do, IMO. Commented Aug 7, 2013 at 3:17
  • 1
    Also, have you thought about how you will handle Ace? It's either 1 or 11.. Commented Aug 7, 2013 at 3:18

2 Answers 2

4

I'm assuming because they all have the same underlying INT value.

Yes, that is exactly what is happening.

I want to be able to do it this way to cast the values back to INT to add up the totals in the game.

No, you shouldn't do that. Separate the point values from the card representations. The main reason for this is because of the implementation problem that you are running into. But conceptually, points are function of the rules of the game that you are playing, not of the cards themselves. Thus, they should be conceptually separated.

Thus, if you want to sum the point values just have a method:

public int GetPoints(CardValue card) { }

and then if you have a sequence of CardValues:

int total = cards.Sum(card => GetPoints(card));

Note that your method might not be as simple as merely taking a CardValue as a parameter: think of the dual role of Ace having a low and a high value in some games. Now do you see again why assigning the values directly to the cards is not a good idea?

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, this would be the expected behavior. You should give each card a unique enum number (Jack = 11, Queen = 11, King = 12), and then have a translation process to get their numerical value when you need it.

static class CardExtensions
{
   public static int GetValue(this CardValue card)
   {
       switch(card)
       {
           case Jack:
           case Queen:
           case King:
             return 10;
           default:
             return (int)card;
       }
   }
}

CardValue.Jack.GetValue() // Returns 10
CardValue.King.GetValue() // Also returns 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.