我正在創建一個遊戲,玩家需要將屏幕上的對象分類到正確的目標位置。我正在尋找一種方法來洗滌物體,以便沒有物體在正確的位置開始。因此,我們不會陷入一個雙重否定的瘋狂世界,我要稱之爲「正確答案」的地點「避免」地點,以及「不正確答案」地點的「有效」地點。如何在某些對象必須避免被配對在一起時,將一個數組的元素隨機映射到另一個數組的元素?
的陣列可能是這樣的:
var sort_items = [
{"avoid": ["target1", "target2"]},
{"avoid": ["target1", "target2"]},
{"avoid": ["target3"]},
{"avoid": ["target4", "target5"]},
{"avoid": ["target4", "target5"]},
];
var sort_locations = [
{"id": "target1"},
{"id": "target2"},
{"id": "target3"},
{"id": "target4"},
{"id": "target5"},
];
因此,例如,在sort_items第一和第二物體可以被放置在target3
,target4
,或,但不target1
或target2
。
我已經嘗試了許多不同的方法,但他們都有問題,在排序結束時,剩餘的sort_items中剩餘的唯一位置經常無效。例如:
sort_items[0] placed on target3,
sort_items[1] placed on target5,
sort_items[2] placed on target2,
sort_items[3] placed on target1,
Error: sort_items[4] cannot be placed on target4
即使在這個例子中,隨機挑選另一個和交換與它似乎是一個好主意,因爲其他人的一半也將導致在交換的無效比賽。
是否有一個很好的方法來做到這一點?
一個有趣的技術問題,但至於實際的遊戲,如果去一些物體在正確的位置開始將它真的重要嗎?只是簡單地做一個簡單的洗牌,然後把它留在那裏...關於你正在尋找的算法,它應該假設輸入數據是有效的嗎? (即,'sort_items'沒有指定一個不可能的組合?) – nnnnnn
確實很有意思。在真實情況下,你的列表有多大? – Arnauld
避免的目標總是後果..? – Redu