Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys ,

May be this is a newbie question but here it is.

I am developing a card game and I have a class named Player.

Player has a property called Partner ( another Player ) and another property called Points.
C#
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:
C#
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<
Posted
Updated 13-Dec-11 0:26am
v4

I would maintain a points object that is used by both players

C#
public class Points
{
    public int Value { get; set; }
    public Points() {}
}

public class Player
{
    public Points PointsValue { get; set; }
    public Player Partner { get; set; }

    public Player(Points points)
    {
        this.PointsValue = points;
        if (Partner != null)
        { 
            Partner.PointsValue = points;
        }
    }

    public Player(Player partner)
    {
        Partner = partner;
        PointsValue = Partner.PointsValue;
    }
}


I did the code above off the top of my head, but you should get the idea. When the point value changes, BOTH players will have access to the new value.
 
Share this answer
 
v3
Comments
[no name] 13-Dec-11 6:47am    
Using Constructor is best here. 5.
It seems that you need to implement Observer design pattern :
http://www.dofactory.com/Patterns/PatternObserver.aspx[^]

So you can implement it in this way :

C#
    public class PlayersObserver
    {
        public List<player> Players =new List<player>();

        private int _points;
        public int Points
        {
            get { return _points; }
            set
            {
                _points = value;
                foreach (var player in Players)
                {
                    player.Points = value;
                }
            }
        }
    }
    
    
    public class Player
    {
        public Player()
        {
        }
        public Player Partner { get; set; }

        public int Points { get; set; }
    }
</player></player>



And it usage is :
C#
Player p1 = new Player();
Player p2 = new Player();
PlayersObserver playersObserver = new PlayersObserver();
playersObserver.Players.Add(p1);
playersObserver.Players.Add(p2);

playersObserver.Points = 10;

int p = p2.Points;


p will have the value of 10 at this point.

By this way you will do whatever you want with observer and it will apply that to those objects in its List so you are not confined only to two partners.

Hope it helps.
 
Share this answer
 
Although I prefer JSOP's answer, you can do it without the extra class, but it feels a bit more of a hack as it is less clear what is happening:


C#
public int Points
{
   get { return points;}
   set 
   { 
      if(points == value)
         return; //This will stop a infinitely recurive set, 
                 //but not suitable if multi-threaded access is needed.
      points = value;
      if (Partner != null) 
         Partner.Points = value;
   }
}


Just another option, but as I say go for JSOP's in preference.
 
Share this answer
 
When you set the points for one partner when you are setting points for the other.

Something like -

C#
public int Points { get; 

set
{
this.myPoints = value;
if (this.Partner != null)
  {
    this.Partner.Points = value;
  }
}
 
Share this answer
 
Comments
Pelluso Coder 13-Dec-11 6:11am    
this.Partner.Points = value;

will cause circular reference bacause p1.Partner = p2 and p2.Partner = p1
Abhinav S 13-Dec-11 6:33am    
That cricular reference would exist since you would want both partners to point to each otehr.
Setting an int value to both instances will not create a circular reference.

p1.Partner = p2;
p2.Partner = p1;
would create the circular reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900