/// <summary> /// Causes this Animal to try to move one step in the direction of the specified targetCell. /// </summary> /// <param name="targetCell"> the cell to move towards. </param> /// <returns> true if the Animal successfully moved, or false otherwise. </returns> public bool Move(Cell targetCell) { return(Move(PigWorld.GetDirection(this.Cell, targetCell))); }
/// <summary> /// The radar commences or continues its clockwise sweep until either: /// (a) a target object is found of the right type; or /// (b) the sweep returns to North and null is returned. /// </summary> /// <returns> Either null or the Echo of a new object that belongs to the targetType. </returns> public Echo Ping() { Thing bestSoFarThing = null; Direction bestSoFarDirection = null; double bestSoFarAngle = 0.0; double bestSoFarDistance = 0.0; foreach (Thing thing in pigWorld.Things) { // the radar should not detect its owner if (thing == owner) { continue; } // the radar only detects instances of the right class, and its subclasses (if any). if (!Util.IsSameTypeOrSubtype(thing.GetType(), targetType)) { continue; } Direction tempDirection = pigWorld.GetDirection(owner, thing); double tempAngle = tempDirection.Degrees; if (tempAngle <= sweepAngle) { continue; } if (bestSoFarThing == null) { bestSoFarThing = thing; bestSoFarDirection = tempDirection; bestSoFarAngle = tempAngle; bestSoFarDistance = pigWorld.GetDistance(owner, bestSoFarThing); } else { if (tempAngle < bestSoFarAngle) { bestSoFarThing = thing; bestSoFarDirection = tempDirection; bestSoFarAngle = tempAngle; bestSoFarDistance = pigWorld.GetDistance(owner, bestSoFarThing); } else if (tempAngle == bestSoFarAngle) { double tempDistance = pigWorld.GetDistance(owner, thing); if (tempDistance < bestSoFarDistance) { bestSoFarThing = thing; // Don't have to change bestSoFarDirection or bestSoFarAngle. bestSoFarDistance = tempDistance; } } // if (tempAngle == bestSoFarAngle) } // if else (bestSoFarThing == null) } // end foreach if (bestSoFarThing == null) { sweepAngle = -1.0; // ready for a new sweep return(null); } else { sweepAngle = bestSoFarAngle; Type typeOfFirstThing = bestSoFarThing.GetType(); Echo echo = new Echo(typeOfFirstThing, bestSoFarDirection, bestSoFarDistance); return(echo); } } // method Ping