I'm trying to create a game in Java. I want to create a feature where you can sacrifice items, and if you sacrifice the right 'recipe' of different items, you get something in return. A pseudo example of recipes could be:
recipe1[1, 5, 32]
recipe2[4, 221, 21, 9]
The order of the recipes should not matter - meaning, if I have input = ..., 7, 5, 32, 1
this should return true for recipe1[1, 5, 32]
- but the sequence should matter, so input = ..., 5, 7, 32, 1
returns false , even though input does contain all the values of recipe1.
I have no real code yet, as I have no idea how to go about this.
The code would be much easier to implement if you use a Set of Sets.
Set<Set<Integer>> recipes = new HashSet<>();
now you can check if a recipe is part of the set:
boolean contains = recipes.contains(new HashSet<>(Arrays.asList(1,5,32));
The order is not important, but the exact items are.
Ah, I think I misunderstood, this is the exact opposite of what you are asking for. Dang!
Looking for the items in the right order will perform a lot worse and be more tedious. Basically, you can do a Collections.binarySearch()
for the first item in the sequence. And if you find it, keep looking from there. Your data structure would be a List in that case.
No, I think it's closer to my initial understanding after all. Use the set of sets, but test it for all possible sub-lists of your input.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.