2012-07-18 35 views
4

我正在創建一些簡單的遊戲,其中一些簡單的AI實現在一些遊戲計算機控制的玩家中。確定遠離其他點的最遠點

我有一個Point列表,代表玩家可能的動作。我需要編寫一個方法將玩家移動到距離該列表中可能的敵人最遠的地方Point。我示出它與該圖片:

數字代表點poistion在列表

我想是爲播放器(4)移動到要麼Point在那些最遠從位置2或6任何敵人。我設法解決這個問題,如果有一個敵人迭代列表並使用方法Point來確定哪個點距離最遠。但即使網格中有多個敵人,代碼也必須工作。

+0

那麼,它的工作? – HumanCatfood 2012-07-26 13:14:41

回答

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. 
+0

另外:爲有趣的圖表投票! – HumanCatfood 2012-07-18 09:34:20