簡體   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