From ec05143ea6a9bfa262e573aa3c7c52276b7f7748 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 4 May 2022 17:10:14 +0200 Subject: [PATCH] in prog --- src/TM.java | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/TM.java diff --git a/src/TM.java b/src/TM.java new file mode 100644 index 0000000..4db3fb3 --- /dev/null +++ b/src/TM.java @@ -0,0 +1,84 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class TM { + private String codierung; + private String[][] funktionen; + private int aktuellerZustand; + private int endZustand; + private List band; + private int leseKopf; + private Map alphabet; + + + public TM(String codierung, String bandInitialisierung) { + leseKopf = 0; + band = new ArrayList<>(); + this.codierung = codierung; + endZustand = 2; + alphabet = new HashMap<>(); + parse(); + initBand(bandInitialisierung); + + } + + private void calcOneStep() throws TmException { + boolean found = false; + for (String[] str : funktionen) { + if (str[0].length() == aktuellerZustand && str[1].length() == alphabet.get(band.get(leseKopf))) { + found = true; + writeOnBand(str); + break; + } + } + + if (!found) { + throw new TmException("Couldn't find matching function for Character on band"); + } + } + + private void writeOnBand(String[] str) { + aktuellerZustand = str[2].length(); + + //TODO Find corect char from Integer value + for (Character c : alphabet.keySet()) { + if(alphabet.get(c) == str[3].length()) { + band.set(leseKopf, c); + } + } + + //TODO Position von lesekopf + } + + private void parse() { + String[] liste = codierung.substring(1).split("11"); + for (int i = 0; i < liste.length; i++) { + funktionen[i] = liste[i].split("1"); + } + aktuellerZustand = funktionen[0][0].length(); + //TODO Startzustand wissen? + } + + private void initBand(String bandInit) { + for (int i = 0; i < bandInit.length(); i++) { + band.add(bandInit.charAt(i)); + } + } + + private void initAlphabet() { + alphabet.put('0', 1); + alphabet.put('1', 2); + alphabet.put('_', 3); + alphabet.put('X', 4); + } + + private class TmException extends Exception { + + private TmException(String msg) { + super(msg); + } + } + +}