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?