3

I've an Enum for 52 playing cards, as mentioned below,

public enum Card
{
        AceClubs = 11,
        AceDiamonds = 11,
        AceHearts = 11,
        AceSpades = 11,
        DeuceClubs = 2,
        DeuceDiamonds = 2,
        DeuceHearts = 2,
        DeuceSpades = 2,
        EightClubs = 8,
        EightDiamonds = 8,
        EightHearts = 8,
        EightSpades = 8,
        FiveClubs = 5,
        FiveDiamonds = 5,
        FiveHearts = 5,
        FiveSpades = 5,
        FourClubs = 4,
        FourDiamonds = 4,
        FourHearts = 4,
        FourSpades = 4,
        JackClubs = 11,
        JackDiamonds = 11,
        JackHearts = 11,
        JackSpades = 11,
        KingClubs = 13,
        KingDiamonds = 13,
        KingHearts = 13,
        KingSpades = 13,
        NineClubs = 9,
        NineDiamonds = 9,
        NineHearts = 9,
        NineSpades = 9,
        QueenClubs = 12,
        QueenDiamonds = 12,
        QueenHearts = 12,
        QueenSpades = 12,
        SevenClubs = 7,
        SevenDiamonds = 7,
        SevenHearts = 7,
        SevenSpades = 7,
        SixClubs = 6,
        SixDiamonds = 6,
        SixHearts = 6,
        SixSpades = 6,
        TenClubs = 10,
        TenDiamonds = 10,
        TenHearts = 10,
        TenSpades = 10,
        ThreeClubs = 3,
        ThreeDiamonds = 3,
        ThreeHearts = 3,
        ThreeSpades = 3
  }

I want to create a list from enum

 var cards = Enum.GetValues(typeof(Card));

but it returns duplicate keys in list.

enter image description here

M.S.
  • 4,283
  • 1
  • 19
  • 42
  • 5
    It seems that you want *two* enums: one for suits (`Clubs..Spades`) and another one for *values* (`Ace..King`) – Dmitry Bychenko Apr 14 '16 at 12:46
  • 2
    Your enum will work like this because you have used duplicate values - there was a post like this about cards earlier - homework? You must be in the same class as this guy http://stackoverflow.com/questions/36622788/c-sharp-index-number-randomly-generated-and-put-into-array-to-not-occur-again-s/36622956#36622956 – BugFinder Apr 14 '16 at 12:46
  • Maybe refer to this question: [*Non-unique enum values*](http://stackoverflow.com/q/8043027/1364007) – Wai Ha Lee Apr 14 '16 at 12:47

4 Answers4

2

use Enum.GetNames instead of Enum.GetValues

Yiming
  • 321
  • 2
  • 10
  • I had the same problem as OP, but couldn't change the enum because it was part of a third party library. Since what I wanted in the end was a list of strings with the enum keys, this answer was the correct one for me – Master_T Nov 24 '21 at 10:36
1

I suggested using two enums for suit and value:

  public enum CardSuit {
    Clubs = 1,
    Diamonds = 2,
    Hearts = 3,
    Spades = 4,
  };

  public enum CardValue {
    Ace = 1,
    Deuce = 2, 
    ...
    King = 13,
  };

then implement an extension method to get actual card value (since both Jack and Ace corresponds to 11):

  public static class ValueExtensions() {
    public static int ActualValue(this CardValue value) { 
      if (value == CardValue.Ace)
        return 11; // Ace is 11
      else
        return (int) value;
    } 
  }

Finally

  public class Card {
    public Card (CardSuit suit, CardValue value) {
      Suit = suit;
      Value = value;  
    }

    public CardSuit Suit {get; private set;} 
    public CardValue Value {get; private set;} 
  }

  ...

  Card[] pack = Enum
    .GetValues(typeof(CardSuit))
    .OfType<CardSuit>()
    .SelectMany(suit => Enum
      .GetValues(typeof(CardValue))
      .OfType<CardValue>()
      .Select(value => new Card(suit, value)))
    .ToArray();

  //TODO: Shuffle the pack here
  Card[] hand = pack.Take(5).ToArray();

  int handValue = hand.Sum(card => card.Value.ActualValue()); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Your mistake is to use the same value for different things. Enums are not intended to have duplicate values (unless they mean the same thing).

When the program is running, enums are held as integers - not names.

The debugger then takes an integer from the program (4) and displays it as the first enum it can find (FourClubs). This is just for your convenience when debugging - the program still uses the integer (4).

-1

You can remove the duplicates from the array using Linq:

var uniqueCards = cards.Distinct().ToArray();
Henningsson
  • 1,275
  • 1
  • 16
  • 23
  • I think the OP wants the collection to *say*, for instance, Ace of spaces (=1), Ace of Clubs (=1), etc. Doing a `Distinct()` would just show 13 values, which isn't what's wanted. – Wai Ha Lee Apr 14 '16 at 12:49
  • there would be 13 records available then. – M.S. Apr 14 '16 at 12:49