diff --git a/.gitignore b/.gitignore index 562b66d..29668e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ .idea/ -out/ \ No newline at end of file +out/ +/bin/ +.project +.classpath +.bin \ No newline at end of file diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index dcba497..d769669 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -22,6 +22,8 @@ public class Main { rules.add(scanner.next()); } rules.remove(rules.size() - 1); + System.out.println("Nombre d'itérations: "); + nbIterations = scanner.nextInt(); parser = new Parser(axiom, rules,nbIterations); } diff --git a/src/lsystem/engine/parser/Parser.java b/src/lsystem/engine/parser/Parser.java index febec65..75745ff 100644 --- a/src/lsystem/engine/parser/Parser.java +++ b/src/lsystem/engine/parser/Parser.java @@ -7,7 +7,7 @@ public class Parser { private final String axiom; private final List rules; private final int nbIterations; - private String[] validStrings = {"]","[",".","+","-","X","Y","Z","x","y","z","0","1","2","3","4","5","6","7","8","9"}; + private char[] validChars = {'=',']','[','.','+','-','X','Y','Z','x','y','z','0','1','2','3','4','5','6','7','8','9',' '}; public Parser(String axiom, List rules,int nbIterations) { this.axiom = axiom; @@ -16,40 +16,43 @@ public class Parser { } public boolean isCorrect(){ - return (rulesCorrect() || axiomCorrect()); - } - private boolean axiomCorrect() { - - return false; - } - - public boolean rulesCorrect() { - for(String r : this.rules) { - String[] stringsplitted = r.split(""); - String old = null; - int bracket = 0; - for (String rules : stringsplitted){ - for(String string : validStrings){ - if (rules == "[") - bracket += 1; - if(rules =="]") - bracket -=1; - if(old == "."){ - for(int i = 0;i<10;i++){ - if(rules == validStrings[i]) - return false; - } - } - old = rules; - if(rules == string) - break; - if(string == "9" && rules != string) - return false; - } - } - if (bracket != 0) - return false; + if (nbIterations == 0) { + System.out.println("Erreur, nombre d'itérations inssufissant (plus petit que 1)"); + return false; } - return true; + boolean bl = isCorrect(axiom); + for(String rule : this.rules){ + bl = bl && isCorrect(rule); + } + return bl; } + private boolean isCorrect(String stringToCheck) { + + char old = ' '; + int bracket = 0; + for (int i =0; i>stringToCheck.length();i++){ + char temp = stringToCheck.charAt(i); + for(char validChar : validChars){ + if (temp == '[') + bracket += 1; + if(temp ==']') + bracket -=1; + if(old == '.'){ + for(int y = 0;y<12;y++){ + if(temp == validChars[y]) + return false; + } + } + old = temp; + if(temp == validChar) + break; + if(validChar == ' ' && temp != validChar) + return false; + } + } + if (bracket != 0) + return false; + return true; + } + }