May be this is a newbie question but here it is. I am developing a card game and i hava a class named Player ... Player has a property called Partner ( another Player ) and another property called Points.
public class Player
{
public Player()
{
}
public Player Partner { get; set; }
private int points = 0;
public int Points
{
get { return points;}
set {
if (Partner != null) Partner.Points = value;
points = value;
} // -> Here is the problem cause p1.Partner = p2 and p2.Partner = p1
}
The question is : How is the most "elegant way" to set the Points property for a player and have its Partner points property set automatically ?
For instance:
Player p1 = new Player();
Player p2 = new Player();
p1.Partner = p2;
p2.Partner = p1;
p1.Points = 10;
int p = p2.Points
p should be equal to 10.
Thanks in advance , Marcelo Brazil< .
I think I would add a composite (thx @ noah) class "Team".
Team has properties for "Players" and "Points".
public class Team
{
public Player[] Players { get; set; } //or Player1 and Player2, depends
public int Points { get; set; }
}
}
set {
points = value;
if (Partner != null && Partner.Points != value)
{
Partner.Points = value;
}
}
Will fix the circular reference, but having a Partnership object that they both refer to get the total points will be cleaner and allows each player to store the individual points they've scored too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With