1

我正在制作一个简单的游戏,我想为玩家的负载做一个展示,并想展示他们拥有的所有可能的项目组合。播放器有三个插槽,每个插槽都有各自的枚举(PlayerSlotPosition)。在 Item 枚举中有多个值,但每个值都需要位于某个 P​​layerSlotPosition 中。

例子:

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

我需要考虑玩家在任何插槽(或多个插槽)中都没有装备任何物品的可能性,这就是我被卡住的地方。如果有人有类似的经验并愿意帮助我,我将不胜感激。谢谢阅读

4

3 回答 3

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]]

如果我在您的问题中遗漏了什么,请发表评论,这是一个有趣的问题。

于 2013-06-07T12:09:47.297 回答
0

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

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

(一个工作的幂集) 在Java中获取一个集合的幂集

于 2013-06-07T13:02:18.277 回答
-1

我不确定我是否完全理解这个问题。我的猜测是你并没有真正使用面向对象的方法,因此被 Java 卡住了。

所以我的建议是对你的对象进行不同的建模。例如:

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

public class Stick implements SlotOneItem {
    ...
}

对于插槽,您可以使用适当的接口来处理可以存储在该插槽中的项目。例如:

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

   public SlotOneItem getItem() {
      ...
   }    
}

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

于 2013-06-07T12:05:37.093 回答