这里的关键是I wanted to make a display for the player's load out, and wanted to show every possible combination of items that they have
。这大大降低了复杂性,因为您所要做的就是打印出您库存中物品的可能组合。
public class EnumCombinations {
//Commenting out more than 4 items, loooong printout otherwise
public static enum ITEM{
STICK(PlayerSlotPosition.SLOT_ONE),
STONE(PlayerSlotPosition.SLOT_ONE),
EGG(PlayerSlotPosition.SLOT_TWO),
SHOVEL(PlayerSlotPosition.SLOT_TWO);
// PAPER(PlayerSlotPosition.SLOT_THREE),
// DIRT(PlayerSlotPosition.SLOT_THREE);
// DIRT(PlayerSlotPosition.SLOT_THREE);
private ITEM(PlayerSlotPosition pos){
}
}
public static enum PlayerSlotPosition{
SLOT_THREE, SLOT_ONE, SLOT_TWO
}
public static List<List<ITEM>> getItemCombinations(List<ITEM> inventory){
List<ITEM> prefix = new ArrayList<>();
List<List<ITEM>> container = new ArrayList<>();
List<ITEM> remainder;
if(inventory==null){
System.out.println("Printing all item combinations, no inventory");
remainder = new ArrayList<>(Arrays.asList(ITEM.values()));
}
else {
remainder = new ArrayList<>(inventory);
}
getItemCombinations(prefix, remainder, container);
return container;
}
private static void getItemCombinations(List<ITEM> prefix, List<ITEM> remainder, List<List<ITEM>> container){
int n=remainder.size();
if(remainder.isEmpty()){
container.add(prefix);
}
else {
for(int i=0; i<n; i++) {
List<ITEM> r = new ArrayList<>();
List<ITEM> p = new ArrayList<>(prefix);
p.add(remainder.get(i));
for(int j=0; j<remainder.size(); j++){
if(j==i)
continue;
r.add(remainder.get(j));
}
getItemCombinations(p, r, container);
}
}
}
public static void main (String[] args) {
System.out.println(getItemCombinations(null)+"\n");
List<ITEM> playerINV = new ArrayList<>();
playerINV.add(ITEM.STICK);
playerINV.add(ITEM.STONE);
playerINV.add(ITEM.EGG);
System.out.println(getItemCombinations(playerINV));
}
}
输出:
Printing all item combinations, no inventory
[[STICK, STONE, EGG, SHOVEL], [STICK, STONE, SHOVEL, EGG], [STICK, EGG, STONE, SHOVEL], [STICK, EGG, SHOVEL, STONE], [STICK, SHOVEL, STONE, EGG], [STICK, SHOVEL, EGG, STONE], [STONE, STICK, EGG, SHOVEL], [STONE, STICK, SHOVEL, EGG], [STONE, EGG, STICK, SHOVEL], [STONE, EGG, SHOVEL, STICK], [STONE, SHOVEL, STICK, EGG], [STONE, SHOVEL, EGG, STICK], [EGG, STICK, STONE, SHOVEL], [EGG, STICK, SHOVEL, STONE], [EGG, STONE, STICK, SHOVEL], [EGG, STONE, SHOVEL, STICK], [EGG, SHOVEL, STICK, STONE], [EGG, SHOVEL, STONE, STICK], [SHOVEL, STICK, STONE, EGG], [SHOVEL, STICK, EGG, STONE], [SHOVEL, STONE, STICK, EGG], [SHOVEL, STONE, EGG, STICK], [SHOVEL, EGG, STICK, STONE], [SHOVEL, EGG, STONE, STICK]]
Printing inventory:
[[STICK, STONE, EGG], [STICK, EGG, STONE], [STONE, STICK, EGG], [STONE, EGG, STICK], [EGG, STICK, STONE], [EGG, STONE, STICK]]
如果我在您的问题中遗漏了什么,请发表评论,这是一个有趣的问题。