0

让我们假设

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>                <---------'
}

如何对象访问NUMBER OF PLAYERS而无需冗余传递


编辑我
你误解了我的问题,对不起。
我想从它的游戏类型中访问可以加入这个表的最大数量。
所以如果这是一张扑克牌桌,我想得到 NUMBER OF PLAYERS 等于 10


EDIT II
不同的游戏类型:红心、黑桃、扑克、估计等
最大玩家人数分别为:4、4、10、4 等。


编辑 III
再次误解了我的问题,我希望能够执行以下操作:

当玩家尝试加入一个表时,我会比较目标表当前玩家数与其游戏类型最大玩家数,因此我决定玩家是否可以加入它!

4

2 回答 2

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;
于 2012-06-15T19:24:25.360 回答
1

使用 LINQ to 对象可以很容易地做到这一点。

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
{

}
于 2012-06-15T18:52:04.847 回答