Java – determine the farthest point away from other points
I'm creating a simple game, using some simple AI implementation on some game computer guided players
I have a point list that indicates the possible movement of players I need to write a method to move the player to the point farthest from the possible enemies in the list I illustrated this with pictures:
The number represents the points position in the list
What I want is for player (4) to move to any point 2 or 6 farthest from any enemy If an enemy determines which point is the farthest away by iterating through the list and using the distance () method of point, I try to solve this problem But even if there are several enemies in the grid, the code must work properly
Solution
Well, how do you reverse:
1. Iterate over each point. 2. Find out how close it is to its closest enemy. 3. Choose the point that is furthest from its closest enemy.
Early exit has many potential:
Within the loop store the currently furthest point. If you are then inspecting another point and find out it has a closer enemy,you can immediately skip to the next point
[Edit]: if you are using the grid above, you can also
1. Check if there's an enemy on the currently processed point *before* iterating through other enemies. That way you can exclude it as early as possible. 2. If it's a densely populated grid,consider doing a breadth-first flood-fill starting at the current point. That might find the closest enemy much faster than iterating though all of them.