java - 确定距离其他点最远的点

标签 java grid point

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

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

数字代表点在列表中的位置

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

最佳答案

嗯,你反过来做怎么样:

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

[编辑]:同样,如果您正在使用上面的网格,您可以

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.

关于java - 确定距离其他点最远的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11538027/

相关文章:

silverlight - 如何使用 XNA 绘制动态网格

algorithm - 使用瓦片 map 数组随机重新排列剩余瓦片位置的逻辑

postgresql - 使用 ORMLite 建模几何点

javascript - 如何沿长度为 x 的线段生成 n 个等距点的数组?

java - 根据调用方法更改返回类型

java - Maven 程序集 : DependencySet or ModuleSet

android - 可以使用 ListView 进行内联编辑吗?

java - 实现只有一个实例的类

java - Junit 5,Spring 应用程序上下文未在 @DirtiesContext 上关闭

来自 Point2D.setLocation() 的 Java NullPointerException