匿名枚举类型作为类属性

时间:2014-07-27 00:26:39

标签: c# enums

是否可以创建匿名类型的公共枚举类属性?

我正在浏览OOD场景列表并开始基本的纸牌游戏。每个游戏都是一个特定的类,其中包含" PlayerActions"。每个类都有自己特定的枚举值,但是我想在玩家对象初始化后将游戏的动作枚举传递给每个玩家。

这可能还是我完全偏离基地?

public class Player{
    //take set of actions based on the game you're playing
    public enum <T> Actions {get;set;}

    //Hold deck of cards approximate to rules of game
    public List<Card> hand {get;set;}

    public bool IsTurn {get;set;}

    public Player(Game gameType){
        hand = new List<Card>(gameType.HandSize);
        Actions = gameType.GameActions; 
        IsTurn = false;
    }

    public void AssignCard(Card card){
        hand.Add(card);
    }
}

public enum GameType{
    BlackJack,
    TexasHoldEm
}

public abstract class Game{
    public enum<T> GameActions {get; set;}
    public GameType gameType {get;set;}
    public Card[] River {get;set;}
    public Player[] UserList {get;set;}
    public Dealer dealer = new Dealer();
    public int HandSize { get; set; }

} 

public class BlackJack : Game{
    private enum Actions
    {
        Hit,
        Stay
    }

    private const int handSize = 2;
    private const int totalUsers = 5;

    public BlackJack()
    {
        this.gameType = GameType.BlackJack;
        this.River = new Card[handSize];
        this.UserList = new Player[totalUsers];
        this.GameActions = Actions;
    }
}

public class TexasHoldEm : Game{
    enum Actions
    {
        Hit,
        Keep,
        Fold,
        Call,
        AllIn
    }

    public Actions myActions { get; set; }

    public const int HANDSIZE = 3;
    public const int TOTALUSERS = 7;

    public TexasHoldEm()
    {
        this.GameActions = Actions;
        this.gameType = GameType.BlackJack;
        this.River = new Card[HANDSIZE];
        this.UserList = new Player[TOTALUSERS];
    }

}

1 个答案:

答案 0 :(得分:2)

我认为你想要一个Action枚举数组,而不是为每个类重新声明枚举,例如在类之外声明你的枚举,并将所有动作放入其中:

enum Action
{
    Hit,
    Keep,
    Fold,
    Call,
    AllIn,
    Hit,
    Stay
}

然后有一个Action[]数组并在你的构造函数中初始化它:

private Action[] GameActions;
public BlackJack()
{
    this.GameActions = new [] { Action.Hit, Action.Stay };
    this.gameType = GameType.BlackJack;
    this.River = new Card[HANDSIZE];
    this.UserList = new Player[TOTALUSERS];
}

您可能还想GameActions字段readonly ..