views:

46

answers:

1
public class NCell
{
    private var _player: NPlayer;
    public function set player(value: NPlayer):void
    {
        this._player = value;
    }
    public function get player():NPlayer {return this._player}
}

public class NPlayer
{
    private var _cell: NCell;
    public function set cell(value: NCell):void
    {
        if (this._cell != null)
        { 
            this._cell.player = null;
        }

        this._cell = value;

        if (this._cell != null)
        { 
            this._cell.player = this;
            this.position     = this.cell.position;
        }  
    }
}

So, I have a field of NCells (size 20x15) and several players on it. To move player I write

player.cell = field[4][4];

After it player knows his cell (and position) and cell has a link to player. Thus I have everything to calculate available moves. Sometimes there are situations when player has "cell", but "player" in the cell == null. It's bad. It becomes so because when I calculation moves I store players position then move players and then continue searching and when game points become 0 I restore players positions and continue searching. E.g. I have c1 == field[3][4], c2 == field[4][4], c3 == field[5][4], p1.cell == c1, p2.cell == c2. p2 moves to c3 and then p1 moves to c1. Then I restore position:

//c2.player == p1  
p2.cell = c2;//now c2.player == c2 but p1.cell == c2
p1.cell = c1;//and here c2.player == null

and it doesn't depends of restoring sequence. How to avoid link wiping?

A: 

Well, I am not so sure the way you store your data is the best. If you really want this kind of linkage, you may want to consider making a cell capable of holding multiple players. This would resolve your current problem and would be more flexible, later on.

Basically, you problem can be solved like this:

public class NPlayer
{
    private var _cell: NCell;
    public function set cell(value: NCell):void
    {
        if (this._cell != null && this._cell.player == this) //only the owner may remove a link!
        { 
            this._cell.player = null;
        }

        this._cell = value;

        if (this._cell != null)
        { 
            this._cell.player = this;
            //this.position     = this.cell.position; <-- no need for that, see below
        }  
    }
}

Unless you access NPlayer::position a gazillion times per frame, there is no point in storing it. This only introduces a potential source of errors. The property should be calculated, by simply returning this.cell.position or a meaningful default value in case the player has no cell (or raising an exception, if that is not supposed to happen).

greetz
back2dos

back2dos
>Well, I am not so sure the way you store your data is the best.Could you give an example? I use it to know has a cell player and where player is. I have a time to rewrite this part of the game.I have tried your hint already but it didn't help. Perhaps I have a bug somewhere else. To difficult too locate it.
ChessMax