简体   繁体   中英

get Object created current Object

Let's assume

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

How to access NUMBER OF PLAYERS from Table Object without redundant passing ?


EDIT I
You misunderstood my question, I'm sorry.
I want to access the maximum number that can join this Table from its game type.
So if It's a Poker table I want to get NUMBER OF PLAYERS which is equal 10


EDIT II
Different Game types: Hearts, Spades, Poker, Estimation, ... etc
Max number of players are : 4, 4, 10, 4, etc respectively.


EDIT III
Again, misunderstood my question, I want to be able to do the following :

When player attempt to join a table, I compare target table current players number with its game type max number of players, so I decide if the player can or can't join it !

I think that the following relationship needs to be modelled:

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

Inject the Game being played when the Table is instanced:

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

var myTable = new Table(pokerGame);

To get the max number of players allowed in a Table instance:

var maxAllowed = Table.CurrentGame.MaxPlayers;

Using LINQ to objects you can do this very easily.

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
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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