繁体   English   中英

获取对象创建当前对象

[英]get Object created current Object

假设

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

如何在没有冗余传递的情况下从Table Object访问玩家数


编辑我
对不起,您误解了我的问题。
我想从游戏类型中访问可以加入此表的最大数量。
因此,如果这是一个扑克桌,我想获得等于10的玩家人数


编辑二
不同的游戏类型:心,黑桃,扑克,估计等
最多可玩4个,4个,10个,4个,等等。


编辑III
再一次,我的问题被误解了,我希望能够做到以下几点:

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

我认为以下关系需要建模:

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

在创建Table实例时注入正在玩的Game

var pokerGame = new Poker();
// more code here, etc etc

var myTable = new Table(pokerGame);

要获取Table实例中允许的最大玩家人数:

var maxAllowed = Table.CurrentGame.MaxPlayers;

使用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
{

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM