我的Player类中会有一些如下属性:
public bool IsComputer { get; set; }
public bool IsCurrentTurn { get; set; }
public int TurnOrder { get; set; }
这样,您可以根据他们是否是人类以及是否轮到他们来编写逻辑。您将使用 TurnOrder 属性来确定下一个轮到谁。
因此,如果您要像这样创建 Player 对象:
Player player1 = new Player();
您可以在主逻辑中添加如下内容:
if (player1.IsComputer)
{
PerformTurn();
}
else //Human
{
//Logic to wait and allow human player to do his or her turn here
}
编辑:
为了确定轮到谁,我首先将我的球员列入球员名单。例子:
List<Player> players = new List<Player>
{
new Player {Name = "Player 1", IsComputer = false, TurnOrder = 0, IsCurrentTurn = true},
new Player {Name = "Player 2", IsComputer = true, TurnOrder = 1, IsCurrentTurn = false},
new Player {Name = "Player 3", IsComputer = true, TurnOrder = 2, IsCurrentTurn = false}
};
其次,我将创建一个函数,该函数使用每个玩家的 TurnOrder 属性来确定下一个轮到谁。像这样的方法可以做到这一点:
public Player GetNextPlayer()
{
Player currentPlayer = players.First(x => x.IsCurrentTurn);
//if player is last in the turn order return first player
if (currentPlayer.TurnOrder == players.Max(x => x.TurnOrder).TurnOrder)
{
return players.Min(x => x.TurnOrder);
}
else
{
return players.First(x => x.TurnOrder == (currentPlayer.TurnOrder + 1));
}
}
当然,您可以只使用列表的索引,而完全跳过使用 TurnOrder,尽管如果您想扩展您的游戏,使用索引作为回合顺序可能会在以后伤害您。我认为 TurnOrder 属性要灵活得多。
要在回合中获得下一个玩家,只需使用以下命令:
Player currentPlayer = GetNextPlayer();