c# 如何将多个结构转换为 1 个更大的结构

本文关键字:结构 转换 | 更新日期: 2023-09-27 18:32:59

我有 6 个结构供播放器和 5 个机器人使用。它们每个人都有一些不同的变量,有些变量与其他变量相等。我这样宣布他们

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 个结构,还是有其他方法将它们放在一起?我正在寻找类似接口的东西,但它也应该能够容纳静态变量..有什么建议吗?

c# 如何将多个结构转换为 1 个更大的结构

结构是值类型,而类是引用类型。你几乎肯定想把类用于这类事情。

玩家和机器人之间以及不同的"机器人编号"之间有许多共同的属性(已实现为字段(。您决定为所有这些属性指定不同的名称,这使得简化代码变得困难。

您的字段将声明为静态字段。我建议将它们设置为实例字段(或者可能是实例属性(。

如果进行了这些更改,则可以使用继承将类似内容放在公共基类型中

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;