本文介绍了使用不同的键获取具有重复值的枚举变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公开枚举卡
{
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
}

我想从枚举中创建一个列表

  var cards = Enum.GetValues (typeof运算(卡)); 

但它会在列表中返回重复的键。



解决方案

我建议使用两个

  public enum CardSuit {
俱乐部= 1,
钻石= 2,
Hearts = 3,
Spades = 4,
};

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

然后实现扩展方法以获取实际的卡值(因为 Jack Ace 对应于 11 ):

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

最后

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

public CardSuit Suit {get; private set;}
public CardValue Value {get;私人设置;}
}

...

卡[]包=枚举
.GetValues(typeof(CardSuit))
.OfType< CardSuit>()
.SelectMany(suit =>枚举
.GetValues(typeof(CardValue))
.OfType< CardValue>()
.Select => new Card(suit,value)))
.ToArray();

// TODO:在这里卸载包
卡[] hand = pack.Take(5).ToArray();

int handValue = hand.Sum(card => card.Value.ActualValue());


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.

解决方案

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()); 

这篇关于使用不同的键获取具有重复值的枚举变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 15:14