4

我正在创建一个简单的游戏,其中一些游戏计算机引导玩家使用一些简单的 AI 实现。

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

数字代表列表中的点位置

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

4

1 回答 1

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

[编辑]:如果您使用上述网格,您也可以

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.
于 2012-07-18T09:31:27.230 回答