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