From 496efb764360cd26fa40982532048aa18ae4c6be Mon Sep 17 00:00:00 2001 From: syeyoung <42869671+cyoung06@users.noreply.github.com> Date: Tue, 15 Dec 2020 00:40:58 +0900 Subject: dungeon actions and secrets --- .../dungeon/actions/tree/ActionTree.java | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/kr/syeyoung/dungeonsguide/dungeon/actions/tree/ActionTree.java (limited to 'src/main/java/kr/syeyoung/dungeonsguide/dungeon/actions/tree') diff --git a/src/main/java/kr/syeyoung/dungeonsguide/dungeon/actions/tree/ActionTree.java b/src/main/java/kr/syeyoung/dungeonsguide/dungeon/actions/tree/ActionTree.java new file mode 100644 index 00000000..1db6767c --- /dev/null +++ b/src/main/java/kr/syeyoung/dungeonsguide/dungeon/actions/tree/ActionTree.java @@ -0,0 +1,49 @@ +package kr.syeyoung.dungeonsguide.dungeon.actions.tree; + +import kr.syeyoung.dungeonsguide.dungeon.actions.Action; +import kr.syeyoung.dungeonsguide.dungeon.actions.ActionRoot; +import kr.syeyoung.dungeonsguide.dungeon.roomfinder.DungeonRoom; +import lombok.Data; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +@Data +public class ActionTree { + private ActionTree parent; + private Action current; + private Set children; + + + public static ActionTree buildActionTree(Set actions, DungeonRoom dungeonRoom) { + ActionRoot root = new ActionRoot(); + root.setPreRequisite(actions); + ActionTree tree = new ActionTree(); + tree.setParent(null); + tree.setCurrent(root); + HashSet set = new HashSet(); + for (Action action : actions) { + set.add(buildActionTree(tree, action, dungeonRoom, new HashMap())); + } + tree.setChildren(set); + return tree; + } + + private static ActionTree buildActionTree(ActionTree parent, Action action, DungeonRoom dungeonRoom, Map alreadyBuilt) { + if (action == null) return null; + if (alreadyBuilt.containsKey(action)) return alreadyBuilt.get(action); + + ActionTree tree = new ActionTree(); + alreadyBuilt.put(action, tree); + tree.setParent(parent); + tree.setCurrent(action); + HashSet set = new HashSet(); + for (Action action2 : action.getPreRequisites(dungeonRoom)) { + set.add(buildActionTree(tree, action2, dungeonRoom, alreadyBuilt)); + } + tree.setChildren(set); + return tree; + } +} -- cgit