/// <summary> /// Gets a reference to a neighbouring Cell (if any) in the specified direction. /// The direction will be automatically rounded to the nearest Cell. /// /// If there is a wall in that direction, then null is returned. /// </summary> /// <param name="direction"> the direction in which to grab the neighbouring Cell. </param> /// <returns> the neighbouring Cell in the specified direction. </returns> public Cell GetAdjacentCell(Direction direction) { double radians = direction.GetRadians(); int relativeRow = -(int)Math.Round(Math.Cos(radians)); int relativeColumn = (int)Math.Round(Math.Sin(radians)); int targetRow = position.Row + relativeRow; int targetColumn = position.Column + relativeColumn; Cell target = pigWorld.GetCell(new Position(targetRow, targetColumn)); if (target == null) { return(null); } // If there's a wall between this cell and the target cell ... if (pigWorld.IsWallBetweenCells(this, target)) { return(null); } return(target); }