我们假设
public abstract class Game
{
// Base
}
public class Poker : Game
{
// Lobby Object
// NUMBER OF PLAYERS ( Max ) ----------
} '
'
public class Lobby '
{ '
// List<Tables> '
} '
'
public class Table '
{ '
// List<Player> <---------'
}
如何从表对象访问玩家数量而没有多余传递?
编辑我
你误解了我的问题,对不起
我想从其游戏类型中访问可以加入此表的最大数量
所以,如果这是一张扑克牌桌,我希望获得相当于10的玩家数量
编辑II
不同的游戏类型:心,黑桃,扑克,估计,等等
最多玩家分别为:4人,4人,10人,4人等。
编辑III
再次,误解了我的问题,我希望能够做到以下几点:
当玩家尝试加入表格时,我会比较目标表格当前 玩家数量与其游戏类型最大玩家数量,所以我决定 如果玩家能够或不能加入它!
答案 0 :(得分:3)
我认为以下关系需要建模:
public abstract class Game
{
// force all Game descendants to implement the property
public abstract int MaxPlayers { get; }
}
public class Poker : Game
{
// Lobby Object
public List<Lobby> Lobbies { get; set; }
// NUMBER OF PLAYERS ( Max )
// the abstract prop needs to be overridden here
public override int MaxPlayers
{
get { return 4; }
}
}
public class Lobby
{
public List<Table> Tables { get; set; }
}
public class Table
{
public Game CurrentGame { get; set; }
public List<Player> Players { get; set; }
// force the Game instance to be provided as ctor param.
public Table(Game gameToStart)
{
CurrentGame = gameToStart;
}
}
在实例Game
时注入正在播放的Table
:
var pokerGame = new Poker();
// more code here, etc etc
var myTable = new Table(pokerGame);
获取Table
个实例中允许的最大玩家数量:
var maxAllowed = Table.CurrentGame.MaxPlayers;
答案 1 :(得分:1)
使用LINQ到对象,您可以非常轻松地完成此任务。
public abstract class Game
{
}
public class Poker : Game
{
private Lobby lobby = new Lobby();
public int MaxPlayers
{
get
{
int count = lobby.tableList.Sum(t => t.playerList.Sum(c => t.playerList.Count));
return count;
}
}
public class Lobby
{
public List<Table> tableList { get; set; }
}
public class Table
{
public List<Player> playerList { get; set; }
}
public class Player
{
}