From 0505a0868e61a0344713a8fdb93c733d492c28b9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 4 May 2022 21:08:34 +0200 Subject: [PATCH] rdy to do first tests --- src/TM.java | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/TM.java b/src/TM.java index 4db3fb3..f0ec42c 100644 --- a/src/TM.java +++ b/src/TM.java @@ -11,9 +11,11 @@ public class TM { private List band; private int leseKopf; private Map alphabet; + private int numberOfSteps; public TM(String codierung, String bandInitialisierung) { + numberOfSteps = 0; leseKopf = 0; band = new ArrayList<>(); this.codierung = codierung; @@ -24,32 +26,64 @@ public class TM { } + public void start() { + while (!checkWin()) { + try { + calcOneStep(); + System.out.println("Step: "+numberOfSteps); + System.out.println(this); + } catch (TmException e) { + e.printStackTrace(); + } + } + } + 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); + numberOfSteps++; 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()) { + if (alphabet.get(c) == str[3].length()) { band.set(leseKopf, c); } } + if (str[4].length() == 2) { + leseKopf += 1; + } else { + leseKopf--; + } + aktuellerZustand = str[2].length(); + } - //TODO Position von lesekopf + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < 15 - leseKopf; i++) { + stringBuilder.append('_'); + } + for (Character c : band) { + stringBuilder.append(c); + } + for (int i = 0; i < 15; i++) { + stringBuilder.append('_'); + } + return stringBuilder.toString(); + } + + private boolean checkWin() { + return aktuellerZustand == endZustand; } private void parse() { @@ -58,7 +92,6 @@ public class TM { funktionen[i] = liste[i].split("1"); } aktuellerZustand = funktionen[0][0].length(); - //TODO Startzustand wissen? } private void initBand(String bandInit) {