public void ManhattanDistanceTest( ) { ManhattanDistance dist = new ManhattanDistance( ); Assert.Throws <ArgumentException>(() => dist.GetDistance(p0, q4)); double result = dist.GetDistance(p0, q0); Assert.AreEqual(result, 1); result = dist.GetDistance(p1, q1); Assert.AreEqual(result, 1.5); result = dist.GetDistance(p2, q2); Assert.AreEqual(result, 0); result = dist.GetDistance(p3, q3); Assert.AreEqual(result, 0); result = dist.GetDistance(p4, q4); Assert.AreEqual(result, 4.5); result = dist.GetDistance(p5, q5); Assert.AreEqual(result, 12); }
protected void ChooseTarget(Unit[] potentialTargets) { if (potentialTargets.Length > 0) { //order units by their manhattan distance from the units coord. IEnumerable <Unit> iter = potentialTargets.OrderBy(unit => ManhattanDistance.GetDistance(cubeCoordinate, unit.CubeCoordinate)); //choose the lowest distance (first item) Unit closestTarget = iter.First(); //choose the closest unit as the target if // 1. unit currently has no target // OR 2. target has died // OR 3. current target is further away than the closest potential target if (target == null || !target.IsAlive() || ManhattanDistance.GetDistance(cubeCoordinate, closestTarget.CubeCoordinate) < ManhattanDistance.GetDistance(cubeCoordinate, target.CubeCoordinate)) { target = iter.First(); } } }