Hi, I have three classes public class Player , public class Computer : Player, and public class Game.
In Game I have a List: public List<Player> PlayerList and a variable Player CurrentPlayer.
Heres what happens: when I create a new Computer() I add it to the PlayerList, the CurrentPlayer acts like an Iterator and tracks the current player in the game.
Heres the Question: I want to know if(CurrentPlayer is Computer) (this by-the-way does not work).

Edited 1 Year Ago by HuePig

Yeah, so I guess my real question is, what can i implement to replace CurrentPlayer so it can store Computer too.

note that I use CurrentPlayer to iterate through the PlayerList which is a List<Player>

Edited 1 Year Ago by HuePig

I'm not really sure what you are after but I did some tinkering - might help, might not.

void Main()
{
    var game = new Game();

    Player dave = new Player { Name = "Dave" };
    Player sarah = new Player { Name = "Sarah" };
    Computer mac = new Computer { Name = "Mac" };

    game.PlayerList.Add(dave);
    game.PlayerList.Add(sarah);
    game.PlayerList.Add(mac);

    foreach (var player in game.PlayerList)
    {
        Console.WriteLine(player.Name + " is a computer = " + (player.GetType() == typeof(Computer)));
    }
}

public class Game
{
    public Game()
    {
        PlayerList = new List<Player>();
    }

    public List<Player> PlayerList { get; set; }

    public Player CurrentPlayer { get; set; }
}

public class Player
{
    public string Name { get; set; }
}

public class Computer: Player
{

}

Some more tinkerings - might also help.

Look at the DoSomething methods. In OO you shouldn't be asking if something is of a certain type and then acting accordingly, much better to use polymorphism as in the DoSomething Example.

void Main()
{
    var game = new Game();

    Player dave = new Player { Name = "Dave" };
    Player sarah = new Player { Name = "Sarah" };
    Computer mac = new Computer { Name = "Mac" };

    game.PlayerList.Add(dave);
    game.PlayerList.Add(sarah);
    game.PlayerList.Add(mac);

    foreach (var player in game.PlayerList)
    {
        Console.WriteLine(player.Name + " is a computer = " + (player.GetType() == typeof(Computer)));
    }

    Console.WriteLine();

    bool isCurrentPlayerComputer;

    game.CurrentPlayer = mac;   
    isCurrentPlayerComputer =  game.CurrentPlayer as Computer != null;  
    Console.WriteLine("Current Player {0} is a computer? {1}", game.CurrentPlayer.Name, isCurrentPlayerComputer);

    game.CurrentPlayer = dave;  
    isCurrentPlayerComputer =  game.CurrentPlayer as Computer != null;  
    Console.WriteLine("Current Player {0} is a computer? {1}", game.CurrentPlayer.Name, isCurrentPlayerComputer);

    Console.WriteLine();

    foreach (var player in game.PlayerList)
    {
        player.DoSomething();
    }   
}

public class Game
{
    public Game()
    {
        PlayerList = new List<Player>();
    }

    public List<Player> PlayerList { get; set; }

    public Player CurrentPlayer { get; set; }
}

public class Player
{
    public string Name { get; set; }

    public virtual void DoSomething()
    {
        Console.WriteLine("Doing something in the style of a player");
    }
}

public class Computer: Player
{
    public override void DoSomething()
    {
        Console.WriteLine("Doing something in the style of a computer");
    }
}

Yeah, I did polymorphism. The thing is I needed to know if the currentplayer is a Computer.. I guess my coding is at fault since, as you said, I shouldn't be asking for the object's type.
I'll look into it again. Tnx for the advice.

This question has already been answered. Start a new discussion instead.