确定远离其他Point的最远点

时间:2012-07-18 09:15:11

标签: java grid point

我正在创建一个简单的游戏,在一些游戏计算机操舵玩家身上使用一些简单的AI实现。

我有一个Point列表,代表玩家的可能动作。我需要编写一种方法,将玩家移动到距离该列表中可能的敌人最远的Point。我用图片说明了它:

数字代表List

中的Points poistion

我想要的是让玩家(4)移动到距离任何敌人最远的位置2或6的Point。如果有一个敌人通过迭代列表并使用distance() Point方法确定哪个点距离最远,我就设法解决了这个问题。但即使网格中有几个敌人,代码也必须正常工作。

1 个答案:

答案 0 :(得分:1)

嗯,你怎么样呢:

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.

早期出局有很多潜力:

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]:如果你正在使用上面的网格,你可以

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.