From 57aacfc0a64f01dcf8ba9c41989f58aeb58cf501 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 29 Sep 2015 13:44:16 -0400 Subject: Added examples from previous version. That's all of the things from the previous version added. --- .../java/bjc/utils/examples/BinarySearcher.java | 133 +++++++++++++++++++++ .../java/bjc/utils/examples/DiabloItemGen.java | 84 +++++++++++++ .../bjc/utils/examples/RandomStringExamples.java | 57 +++++++++ 3 files changed, 274 insertions(+) create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java (limited to 'BJC-Utils2/src/examples') diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java new file mode 100644 index 0000000..8e2ca51 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java @@ -0,0 +1,133 @@ +package bjc.utils.examples; + +import java.util.Scanner; + +import bjc.utils.data.bst.BinarySearchTree; +import bjc.utils.data.bst.ITreePart.TreeLinearizationMethod; + +/** + * Example showing how to use the binary search tree. + * + * @author ben + * + */ +public class BinarySearcher { + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + + System.out.println("Binary Tree Constructor/Searcher"); + + char c = 'n'; + + BinarySearchTree bst = new BinarySearchTree<>( + (o1, o2) -> o1 - o2); + + while (c != 'e') { + System.out.print("Enter a command (m for help): "); + c = s.nextLine().charAt(0); + + switch (c) { + case 'm': + System.out.println("Valid commands: "); + System.out.println("\tm: Display this help message."); + System.out.println("\te: Exit this program."); + System.out.println( + "\ta: Add a node to the binary tree."); + System.out.println("\td: Display the binary tree."); + System.out.println( + "\tr: Remove a node from the binary tree."); + System.out.println( + "\tf: Check if a given node is in the binary tree."); + System.out.println( + "\tt: Trim all deleted nodes from the tree."); + System.out.println( + "\tb: Balance the tree (also trims dead nodes)"); + break; + + case 'a': + System.out.print( + "Enter the letter to add to the binary tree: "); + c = s.nextLine().charAt(0); + + bst.addNode(c); + break; + case 'r': + System.out.print( + "Enter the letter to add to the binary tree: "); + c = s.nextLine().charAt(0); + + bst.deleteNode(c); + break; + case 'd': + displayTree(bst, s); + break; + case 'f': + System.out.print( + "Enter the letter to add to the binary tree: "); + c = s.nextLine().charAt(0); + + System.out.println("Node " + c + " was " + + (bst.isInTree(c) ? "" : "not ") + "found"); + break; + case 't': + bst.trim(); + break; + case 'b': + bst.balance(); + break; + default: + System.out.println("ERROR: Unrecognized command."); + } + + } + s.close(); + } + + private static void displayTree(BinarySearchTree bst, + Scanner s) { + System.out.print( + "What order would you like the tree to be printed in (m for options): "); + + char c; + + while (true) { + c = s.nextLine().charAt(0); + + TreeLinearizationMethod tlm = null; + + switch (c) { + case 'm': + System.out.println("Possible tree printing methods: "); + System.out.println( + "\tp: Preorder printing (print parent first, then left & right)."); + System.out.println( + "\ti: Inorder printing (print left first, then parent & right)."); + System.out.println( + "\to: Postorder printing (print left first, then right & parent)."); + break; + case 'p': + tlm = TreeLinearizationMethod.PREORDER; + break; + case 'i': + tlm = TreeLinearizationMethod.INORDER; + break; + case 'o': + tlm = TreeLinearizationMethod.POSTORDER; + break; + default: + System.out.println("ERROR: Unknown command."); + } + + if (tlm != null) { + bst.traverse(tlm, ch -> { + System.out.println("Node: " + ch); + return true; + }); + return; + } + + System.out.print( + "What order would you like the tree to be printed in (m for options): "); + } + } +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java new file mode 100644 index 0000000..fd0b221 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java @@ -0,0 +1,84 @@ +package bjc.utils.examples; + +import java.util.StringTokenizer; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.gen.WeightedGrammar; + +public class DiabloItemGen { + private static WeightedGrammar parts = new WeightedGrammar<>(); + + private static void addCase(String rn, int prob, String prts) { + parts.addCase(rn, prob, new FunctionalStringTokenizer( + new StringTokenizer(prts, " ")).toList(s -> s)); + } + + private static void addInfixRules() { + String rn = ""; + + addCase(rn, 60, "sword"); + addCase(rn, 50, "armor"); + addCase(rn, 40, "rune"); + addCase(rn, 30, "scroll"); + addCase(rn, 20, "potion"); + addCase(rn, 10, "helm"); + } + + private static void addItemRules() { + String rn = ""; + + addCase(rn, 10, ""); + addCase(rn, 20, " "); + addCase(rn, 30, " "); + addCase(rn, 40, " "); + addCase(rn, 50, " "); + addCase(rn, 60, " "); + } + + private static void addPrefixRules() { + String rn = ""; + + addCase(rn, 60, "sturdy"); + addCase(rn, 50, "fine"); + addCase(rn, 40, "strong"); + addCase(rn, 30, "azure"); + addCase(rn, 20, "crimson"); + addCase(rn, 10, "phasing"); + } + + private static void addSuffixRules() { + String rn = ""; + + addCase(rn, 60, "of Health"); + addCase(rn, 50, "of Wealth"); + addCase(rn, 40, "of Life"); + addCase(rn, 30, "of the Jackal"); + addCase(rn, 20, "of Vitality"); + addCase(rn, 10, "of Ability"); + } + + public static void main(String[] args) { + parts.addRule(""); + addItemRules(); + + parts.addRule(""); + addSuffixRules(); + + parts.addRule(""); + addPrefixRules(); + + parts.addRule(""); + addInfixRules(); + + for (int i = 0; i < 100; i++) { + FunctionalList ls = parts.genList("", " "); + + StringBuilder sb = new StringBuilder(); + + ls.forEach(sp -> sb.append(sp)); + + System.out.println(sb.toString().replaceAll("\\s+", " ")); + } + } +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java new file mode 100644 index 0000000..368c8ca --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java @@ -0,0 +1,57 @@ +package bjc.utils.examples; + +import java.util.StringTokenizer; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.gen.RandomGrammar; + +public class RandomStringExamples { + private static RandomGrammar rg; + + public static void main(String[] args) { + rg = new RandomGrammar<>(); + + addRule("", " ", + " thinks that I am ", + "I ", "You think that I am "); + + addRule("", "dancing", "eating", "sleeping"); + + addRule("", "", "life", "my computer", + "my friends"); + + addRule("", "hate", "am jealous of", "love"); + + addRule("", "hates", "loves"); + + addRule("", "my sister", "my father", "my girlfriend", + "the man next door"); + + addRule("", "creative", "intelligent"); + + addRule("", "", " with ", + ""); + + for (int i = 0; i < 10; i++) { + FunctionalList ls = rg.genList("", " "); + + StringBuilder sb = new StringBuilder(); + + ls.forEach(sp -> sb.append(sp)); + + System.out.println(sb.toString().replaceAll("\\s+", " ")); + } + } + + private static void addRule(String rule, String... cases) { + FunctionalList> cses = new FunctionalList<>(); + + for (String string : cases) { + cses.add(new FunctionalStringTokenizer( + new StringTokenizer(string, " ")).toList(s -> s)); + } + + rg.makeRule(rule, cses); + } +} -- cgit v1.2.3