[英]c# How to transform multiple structs into 1 bigger
我为玩家和5个机器人提供了6种结构。 每个人都有一些不同的变量,有些变量与其他变量相等。我这样声明它们
public struct Player
{
public static int Chips;
public static int Type;
public static int Power;
public static bool bot1Turn;
public static bool bot1FoldTurn;
public static AnchorStyles playerCardsAnchor = AnchorStyles.Bottom;
}
public struct Bot1
{
public static int bot1Chips;
public static int bot1Type;
public static int bot1Power;
public static bool bot1Turn;
public static bool bot1FoldTurn;
public static AnchorStyles bot1CardsAnchor = AnchorStyles.Left;
}
public struct Bot2
{
public static int bot2Chips;
public static int bot2Type;
public static int bot2Power;
public static bool bot2Turn;
public static bool bot2FoldTurn;
public static AnchorStyles bot2CardsAnchor = AnchorStyles.Right;
}
public struct Bot3
{
public static int bot3Chips;
public static int bot3Type;
public static int bot3Power;
public static bool bot3Turn;
public static bool bot3FoldTurn;
public static AnchorStyles bot3CardsAnchor = AnchorStyles.Top;
}
public struct Bot4
{
public static int bot4Chips;
public static int bot4Type;
public static int bot4Power;
public static bool bot4Turn;
public static bool bot4FoldTurn;
public static AnchorStyles bot4CardsAnchor = AnchorStyles.Bottom | AnchorStyles.Right;
}
public struct Bot5
{
public static int bot5Chips;
public static int bot5Type;
public static int bot5Power;
public static bool bot5Turn;
public static bool bot5FoldTurn;
public static AnchorStyles bot5CardsAnchor = AnchorStyles.Top | AnchorStyles.Left;
}
稍后我将值添加到静态构造函数中:
static MainPoker()
{
Player.Chips = 100000;
Player.Power = 0;
Player.Type = -1;
Player.playerTurn = true;
Player.playerFoldTurn = false;
}
现在我应该像这样保留所有6种结构,还是有其他方法将它们全部组合在一起? 我在寻找类似接口的东西,但它也应该能够容纳静态变量。
结构是值类型,而类是引用类型。 您几乎肯定要对此类事情使用类。
在玩家和机器人之间以及不同的“机器人编号”之间,您有许多共同的属性(已实现为字段)。 您决定为所有这些属性指定不同的名称,这使得简化代码变得很困难。
您的字段被声明为静态。 我建议使它们成为实例字段(或可能是实例属性)。
如果进行了这些更改,则可以使用继承将相似的内容放入通用的基本类型中。
public class Agent
{
public int Chips;
public int Type;
public int Power;
public bool Turn;
public bool FoldTurn;
public AnchorStyles CardsAnchor;
}
public class Player : Agent
{
public Player() { CardsAnchor = AnchorStyles.Bottom; }
// Anything that makes a player different here
}
public class Bot : Agent
{
// Anything that makes a bot different here
public Bot(AnchorStyles style)
{
CardsAnchor = style;
}
}
Player player = new Player();
Bot bot1 = new Bot(AnchorStyles.Left);
Bot bot2 = new Bot(AnchorStyles.Right);
您在代码而不是字段中使用属性。 它们在使用该类的代码中似乎表现出相似的行为,但是属性提供了更大的灵活性,因为它们在事物的值和其在幕后的存储方式之间提供了一层(例如,可以根据其他属性的值来计算属性属性或多个后备字段)。 使用属性,您应该写
public class Agent
{
public int Chips { get; set; }
public int Type { get; set; }
public int Power { get; set; }
public bool Turn { get; set; }
public bool FoldTurn { get; set; }
public AnchorStyles CardsAnchor { get; set; }
}
您不想要结构,想要类(除了您确实想要结构,但您随后会知道),然后将类与类(对象)的实例混合在一起。
仅生成一个Player类,然后从中创建实例:
public class Player
{
public int Chips { get; set; }
public int Type { get; set; }
public int Power { get; set; }
public bool BotTurn { get; set; }
public bool BotFoldTurn { get; set; }
public AnchorStyles PlayerCardsAnchor { get; }
public Player(AnchorStyles playerCardsAnchor, more parameters for properties)
{
PlayerCardsAnchor = playerCardsAnchor;
// set other properties here
}
}
MainPoker()
{
var player = new Player(AnchorStyles.Bottom, more parameters);
var bot1 = new Player(AnchorStyles.Left, more parameters);
//more bots
}
如果您需要使用静态方式访问这些实例,请创建一个静态类,其中包含对这些实例的引用。
public static class PokerTable
{
public static Player Player { get; }
public static Player Bot1 { get; }
// more bots
static PokerTable()
{
Player = new Player(AnchorStyles.Bottom, more parameters);
Bot1 = new Player(AnchorStyles.Left, more parameters);
//more bots
}
}
然后,您可以使用以下方式以静态方式访问实例
PokerTable.Player.Chips = 10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.