[英]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.