我正在创建一个简单的游戏,在一些游戏内计算机操控玩家上实现一些简单的 AI。
我有一个 Point
列表,代表玩家可能的移动。我需要编写一种方法,将玩家移动到距离该列表中可能的敌人最远的 Point
处。我用图片说明了:
数字代表点在列表中的位置
我想要的是让玩家 (4) 移动到距离任何敌人最远的位置 2 或 6 中的 Point
。如果有一个敌人,我已经设法通过迭代列表并使用 Point
的 distance()
方法来确定最远的点来解决这个问题。但即使网格中有多个敌人,代码也必须正常工作。
最佳答案
嗯,你反过来做怎么样:
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/