2

I want my Player objects to inherit the sum of chips in the Tournament. I get (Attempted to divide by zero.) and I think that's because it fails to inherit Chips from the parent Tournament object. Why is this not working?

public partial class Form1 : Form
{
    public Tournament Tournament { get; set; }

    public Form1()
    {
        InitializeComponent();

        Tournament = new Tournament();
        Tournament.PlayerList.Add(new Player("Hero", 5000));
        Tournament.PlayerList.Add(new Player("Villain1", 3000));
        Tournament.PlayerList.Add(new Player("Villain2", 4000));

        MessageBox.Show(Tournament.PlayerList[0].StackPercentage.ToString());
    }
}

public class Tournament
{
    public List<Player> PlayerList { get; set; }
    public int Chips
    {
        get
        {
            return PlayerList.Sum(S => S.Stack);
        }
    }

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

public class Player : Tournament
{
    public string ID { get; set; }
    public int Stack { get; set; }
    public double StackPercentage
    {
        get
        {
            return Stack / Chips;
        }
    }

    public Player(string _ID, int _Stack)
    {
        ID = _ID;
        Stack = _Stack;
    }
}
3
  • Why do you think Player does not inherit Chips from Tournament? I believe it does.
    – Oded
    Dec 26, 2011 at 14:26
  • I don't believe this code can compile -- you have a variable with the same name as a type here public Tournament Tournament { get; set; }
    – Hogan
    Dec 26, 2011 at 14:28
  • Just because something is inherited does not make it global / static.
    – Hogan
    Dec 26, 2011 at 14:29

3 Answers 3

6

You should not derive Player from Tournament. By doing so, you're saying each Player IS a Tournament.

Try something like this:

public interface IPlayer {}

public abstract class Player : IPlayer 
{
  public ITournament CurrentTournament { get; set; }
}

public class PokerPlayer : Player 
{
  public PokerPlayer() {}
  public int CurrentChips { get; set; }

  public double StackPercentage 
  {
    get { return this.CurrentChips / (PokerTournament)this.CurrentTournament.StartingChips; }
  }
}

public interface ITournament {}

public abstract class Tournament : ITournament
{
  public List<Player> Players { get; set; }
  public int PlayerCount { get { return this.Players.Count; } }
}

public class PokerTournament : Tournament 
{
  private PokerTournament() {}
  public PokerTournament(int startingChips)
  { 
    if(startingChips < 1) throw new ArgumentException("Invalid starting amount.", "startingChips");
    this.StartingChips = startingChips; 
  }

  public int StartingChips { get; set; }
}
1

Problem in this code is you are assuming Tournament.PlayerList[0] will point to the same player list as instance Tournament is doing but this won't be the case. The PlayerList[0]'s PlayerList collection is empty.

 public double StackPercentage
    {
        get
        {
            //Chips here is 0 because the PlayerList of Player instance is empty
            return Stack / Chips;
        }
    }

The Player class has it's own PlayerList.

4
  • 1
    So every time I instantiate a new Player it will also trigger the constructor of Tournament and create a new empty playerlist. How do I make a property (in the Player class) that is dependent of the stack size of the other players then?
    – Pizzaguru
    Dec 26, 2011 at 15:43
  • 1
    Basically Player is inheriting from Tournament so whenever a new instance of Player will be created, first constructor of Tournament will be called and then constructor of Player. Constructor of Tournament will initialize PlayerList for Player instance Dec 26, 2011 at 15:47
  • 1
    If you want to share information between instances/classes you can either pass this information through parameters or you can declare some global values which are accessible to all classes/instances Dec 26, 2011 at 15:49
  • 1
    Good job finding the "0 count" issue, but that's not the real problem. The inheritance is really what is wrong here which is causing the zero count issue. Dec 26, 2011 at 16:01
1

Inheritance seems fine.

I think you'r problem is that PlayerList is probably empty or the chips of every Player in the List are equal to zero and therefore the sum is zero too.

Make StackPercentage take into account the possibility of Chips being equal to zero if it makes sense or fix the logic that populates PlayerList so that Chips can not be zero ever.

EDIT: You have editted your question but the code you have written wouldn't even compile...I'm not sure what your issue is unless you clarify your code.

Try this:

public Form1()
{
    ....
    Tournament tournament = new Tournament();
    tournament.PlayerList.Add(new Player("Hero", 5000));
    tournament.PlayerList.Add(new Player("Villain1", 3000));
    tournament.PlayerList.Add(new Player("Villain2", 4000));
    int chips = tournament.PlayerList[0].StackPercentage.ToString();
}

Anyway this wont work. See Haris Hasan's answer for details. Basically tournament is not the same object as (Tournament)PlayerList[0]. Keep in mind that everytime you create a Player instance a new Tournament base class instance is created whith its correponding emtpy PlayerList.

You should not be using inheritance here at all. A Player and a Tournament have no inheritance relationship at all. A Player is not a Tournament. Get rid of the inheritance relationship and simply move StackPercentage to the Tournament class and convert it into a method that takes a Player instance as an argument:

  public double StackPercentage(Player player)
  {
       return player.Stack/this.Chips;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.