gpt4 book ai didi

java - 生成所有可能的项目组合

转载 作者:行者123 更新时间:2023-12-01 06:18:41 25 4
gpt4 key购买 nike

我正在制作一个简单的游戏,我想为玩家的负载进行显示,并希望显示他们拥有的所有可能的元素组合。玩家有三个插槽,每个插槽都有各自的枚举(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);

我需要考虑到玩家在任何插槽(或多个插槽)中都没有装备任何元素的可能性,这就是我陷入困境的地方。如果有人有做类似事情的经验并且愿意帮助我,我将非常感激。感谢您的阅读

最佳答案

这里的关键是我想为玩家的负载进行显示,并希望显示他们拥有的所有可能的元素组合。这大大降低了复杂性,因为您所要做的就是打印出库存中元素的可能组合。

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

如果我遗漏了您问题中的某些内容,请发表评论,这是一个有趣的问题。

关于java - 生成所有可能的项目组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16982635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com