Static Method

Basically, tower can only shoot in small range. So we would like to know if invader is in shooting range of tower or not.

So, we need to calculate the distance between two points on the map. Therefore, we need a method to calculate this function in Point class.

public int distanceTo(int x, int y)
{
    int xDiffSquared = (X - x) * (X - x);
    int yDiffSquared = (Y - y) * (Y - y);

    return (int)Math.Sqrt(xDiffSquared + yDiffSquared);
}

To show the result, add this code in Game class.

Console.WriteLine(p.distanceTo(5, 2));

And our Game class should be like this.

class Game
{
    static void Main(string[] args)
    {
        Map map = new Map(8, 5);

        Point p = new Point(4, 2);
        bool isOnMap = map.onMap(p);
        Console.WriteLine(isOnMap);

        p = new Point(8, 5);
        isOnMap = map.onMap(p);
        Console.WriteLine(isOnMap);

        Console.WriteLine(p.distanceTo(5, 2));
        Console.Read();
    }
}

results matching ""

    No results matching ""