Last active
December 13, 2019 17:20
-
-
Save rorymalcolm/cd8ce4b0b73dc9645668843eeecf82ad to your computer and use it in GitHub Desktop.
Ascii Tree Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AsciiTreeGenerator { | |
| public static void main(String[] args) { | |
| generateLines(Integer.parseInt(args[0])); | |
| } | |
| private static void generateLines(int height) { | |
| System.out.print(generateLine(height, 0)); | |
| } | |
| private static String generateLine(int height, int line) { | |
| return (height > line) ? | |
| returnSpaces(calculateSpaces(height, line)) + | |
| returnStars(calculateStars(line)) + | |
| returnSpaces(calculateSpaces(height, line)) + | |
| "\n" + | |
| generateLine(height, line + 1) | |
| : | |
| returnSpaces((calculateStars(line) / 2) - 1) + | |
| "|" + | |
| returnSpaces((calculateStars(line) / 2) - 1); | |
| } | |
| private static String returnSpaces(int amount) { | |
| if (amount > 0) | |
| return returnSpaces(amount - 1) + " "; | |
| else | |
| return ""; | |
| } | |
| private static String returnStars(int amount) { | |
| if (amount > 0) | |
| return returnStars(amount - 1) + "*"; | |
| else | |
| return ""; | |
| } | |
| private static int calculateSpaces(int height, int line) { | |
| return height - line - 1; | |
| } | |
| private static int calculateStars(int line) { | |
| return ((line + 1) * 2) - 1; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment