生成所有可能的项目组合

时间:2013-06-07 11:07:18

标签: java combinations permutation

我正在做一个简单的游戏,我想为玩家的装载显示,并希望展示他们拥有的每个项目的可能组合。播放器有三个插槽,每个插槽都有相应的枚举(PlayerSlotPosition)。在Item枚举中有多个值,但每个值都需要在某个PlayerSlotPosition中。

示例:

STICK(PlayerSlotPosition.SLOT_ONE),
STONE(PlayerSlotPosition.SLOT_ONE),
EGG(PlayerSlotPosition.SLOT_TWO),
SHOVEL(PlayerSlotPosition.SLOT_TWO),
PAPER(PlayerSlotPosition.SLOT_THREE),
DIRT(PlayerSlotPosition.SLOT_THREE);

我需要考虑到玩家不会在任何插槽(或多个插槽)中装备任何物品的可能性,这就是我被困住的地方。如果有人有这样的经历,并愿意帮助我,我将非常感激。感谢您的阅读

3 个答案:

答案 0 :(得分:0)

这里的关键是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]]

如果我错过了你的问题,请发表评论,这是一个有趣的问题。

答案 1 :(得分:0)

查看powersets 我想这就是你要找的东西

http://en.wikipedia.org/wiki/Power_set

(工作电源组) Obtaining a powerset of a set in Java

答案 2 :(得分:-1)

我不确定我完全理解这个问题。我的猜测是你并没有真正使用Object Oriented方法,因此陷入了Java。

所以我的建议是以不同的方式为你的对象建模。 E.g:

为某个插槽中的项目创建接口(例如SlotOneItem, SlotTwoItem),并让这些项目成为该接口的实现。

public class Stick implements SlotOneItem {
    ...
}

对于插槽,您可以使用适当的界面来处理可以存储在该插槽中的项目。 E.g:

public class SlotOne {
   public void setItem(SlotOneItem item) {
      ...
   }

   public SlotOneItem getItem() {
      ...
   }    
}

在你的游戏中,你将拥有两个项目的实例,我想每个插槽中的一个,然后可以填充适合该插槽的项目,或者为空(包含null