static class LSystem { static char [] alphabet = { 'F', 'X', 'Y', '+', '-', '[', ']' }; static String axiom; static String [] rule = { "F", "X", "Y", "+", "-", "[", "]" }; static String tree; LSystem(String axiom, String [] ruleset){ for(int i = 0; i < ruleset.length; i++){ rule[i] = ruleset[i]; } this.axiom = axiom; tree = ""; } void iterate(int maxLength){ tree = new String(axiom); int [] ruleLength = new int[alphabet.length]; for (int i = 0; i < alphabet.length; i++){ ruleLength[i] = rule[i].length(); } for (int i = 0; i < maxLength; i++){ int newLength = 0; for (int j = 0; j < tree.length(); j++){ char c = tree.charAt(j); for (int k = 0; k < alphabet.length; k++){ if (c == alphabet[k]){ newLength += ruleLength[k]; break; } } } StringBuffer newTree = new StringBuffer(newLength); for (int j = 0; j < tree.length(); j++){ char c = tree.charAt(j); for (int k = 0; k < alphabet.length; k++){ if (c == alphabet[k]){ newTree.append(rule[k]); break; } } } tree = newTree.toString(); } } }