From 738e13afa11162546d55b367405478518ea2d916 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 4 May 2022 22:41:06 +0200 Subject: [PATCH] finish --- src/TM.java | 39 +++++++++++++++++++++++++++++---------- src/TestClass.java | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/TestClass.java diff --git a/src/TM.java b/src/TM.java index f0ec42c..d28afaf 100644 --- a/src/TM.java +++ b/src/TM.java @@ -16,13 +16,14 @@ public class TM { public TM(String codierung, String bandInitialisierung) { numberOfSteps = 0; - leseKopf = 0; + leseKopf = 15; band = new ArrayList<>(); this.codierung = codierung; endZustand = 2; alphabet = new HashMap<>(); - parse(); initBand(bandInitialisierung); + initAlphabet(); + parse(); } @@ -30,12 +31,17 @@ public class TM { while (!checkWin()) { try { calcOneStep(); + appendToBand(); System.out.println("Step: "+numberOfSteps); System.out.println(this); } catch (TmException e) { e.printStackTrace(); + break; } } + System.out.println("Finished:"); + System.out.println("Step: "+numberOfSteps); + System.out.println(this); } private void calcOneStep() throws TmException { @@ -62,23 +68,16 @@ public class TM { if (str[4].length() == 2) { leseKopf += 1; } else { - leseKopf--; + leseKopf -= 1; } aktuellerZustand = str[2].length(); } 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(); } @@ -86,8 +85,22 @@ public class TM { return aktuellerZustand == endZustand; } + private void appendToBand() { + if(leseKopf - 15 < 0) { + for (int i = 0; i < 15 - leseKopf; i++) { + band.add(i,'_'); + } + } + if ((band.size() - leseKopf) < 15 ) { + for (int i = 0; i < 15 - (band.size() - leseKopf); i++) { + band.add(band.size(),'_'); + } + } + } + private void parse() { String[] liste = codierung.substring(1).split("11"); + funktionen = new String[liste.length][4]; for (int i = 0; i < liste.length; i++) { funktionen[i] = liste[i].split("1"); } @@ -95,9 +108,15 @@ public class TM { } private void initBand(String bandInit) { + for (int i = 0; i < 15; i++) { + band.add('_'); + } for (int i = 0; i < bandInit.length(); i++) { band.add(bandInit.charAt(i)); } + for (int i = 0; i < 15; i++) { + band.add('_'); + } } private void initAlphabet() { diff --git a/src/TestClass.java b/src/TestClass.java new file mode 100644 index 0000000..43f1389 --- /dev/null +++ b/src/TestClass.java @@ -0,0 +1,22 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestClass { + + TM turing; + + + @Test + void check() { + turing = new TM("101010001000100110100100000000000010001001100010010000100100110001010001010011000010001000001001011000010010000010010110000101000010100110000010000100000100001001100000100100000010010011000001010000010000100110000001000100000001010110000001010000001010011000000010010000000010010110000000101000000010101100000000100001000000001000010110000000010010000000001001001100000000101000001000010011000000000100001000000000101001100000000010010000000000100101100000000001001000000000001001011000000000010100000000001010110000000000010001010001001100000000000101000000000001010110000000000001001001000100110000000000001010000000000001000100", + "001000"); + turing.start(); + } + + @Test + void check2() { + turing = new TM("101010001000100110100100000000000010001001100010010000100100110001010001010011000010001000001001011000010010000010010110000101000010100110000010000100000100001001100000100100000010010011000001010000010000100110000001000100000001010110000001010000001010011000000010010000000010010110000000101000000010101100000000100001000000001000010110000000010010000000001001001100000000101000001000010011000000000100001000000000101001100000000010010000000000100101100000000001001000000000001001011000000000010100000000001010110000000000010001010001001100000000000101000000000001010110000000000001001001000100110000000000001010000000000001000100", + "0001000"); + turing.start(); + } +}