匿名枚举类型作为类属性
本文关键字:属性 类型 枚举 | 更新日期: 2023-09-27 18:15:08
是否可以创建一个匿名类型的公共enum类属性?
我正在浏览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];
}
}
我认为你想要一个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
..