Skip to content

Instantly share code, notes, and snippets.

@rorymalcolm
Last active December 13, 2019 17:20
Show Gist options
  • Select an option

  • Save rorymalcolm/cd8ce4b0b73dc9645668843eeecf82ad to your computer and use it in GitHub Desktop.

Select an option

Save rorymalcolm/cd8ce4b0b73dc9645668843eeecf82ad to your computer and use it in GitHub Desktop.
Ascii Tree Generator
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