From 049e3aeda706df95b8511578e1a0a1b6ba8c3f8d Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 2 Feb 2021 18:02:10 +0100 Subject: [PATCH] parser --- src/lsystem/Main.java | 4 +-- src/lsystem/engine/parser/Parser.java | 41 +++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index fe0083a..dcba497 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -1,7 +1,6 @@ package lsystem; import lsystem.engine.parser.Parser; - import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -11,6 +10,7 @@ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Parser parser = null; + int nbIterations = 0; while(parser == null || parser.isCorrect()) { if(parser != null) System.out.println("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); @@ -22,7 +22,7 @@ public class Main { rules.add(scanner.next()); } rules.remove(rules.size() - 1); - parser = new Parser(axiom, rules); + parser = new Parser(axiom, rules,nbIterations); } scanner.close(); diff --git a/src/lsystem/engine/parser/Parser.java b/src/lsystem/engine/parser/Parser.java index b7b4401..febec65 100644 --- a/src/lsystem/engine/parser/Parser.java +++ b/src/lsystem/engine/parser/Parser.java @@ -6,15 +6,50 @@ 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"}; - public Parser(String axiom, List rules) { + public Parser(String axiom, List rules,int nbIterations) { this.axiom = axiom; this.rules = rules; + this.nbIterations = nbIterations; } public boolean isCorrect(){ - - return false; + 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; + } + return true; + } }