This commit is contained in:
Andrin Fassbind 2022-05-04 22:41:06 +02:00
parent 0505a0868e
commit 738e13afa1
2 changed files with 51 additions and 10 deletions

View File

@ -16,13 +16,14 @@ public class TM {
public TM(String codierung, String bandInitialisierung) { public TM(String codierung, String bandInitialisierung) {
numberOfSteps = 0; numberOfSteps = 0;
leseKopf = 0; leseKopf = 15;
band = new ArrayList<>(); band = new ArrayList<>();
this.codierung = codierung; this.codierung = codierung;
endZustand = 2; endZustand = 2;
alphabet = new HashMap<>(); alphabet = new HashMap<>();
parse();
initBand(bandInitialisierung); initBand(bandInitialisierung);
initAlphabet();
parse();
} }
@ -30,12 +31,17 @@ public class TM {
while (!checkWin()) { while (!checkWin()) {
try { try {
calcOneStep(); calcOneStep();
appendToBand();
System.out.println("Step: "+numberOfSteps); System.out.println("Step: "+numberOfSteps);
System.out.println(this); System.out.println(this);
} catch (TmException e) { } catch (TmException e) {
e.printStackTrace(); e.printStackTrace();
break;
} }
} }
System.out.println("Finished:");
System.out.println("Step: "+numberOfSteps);
System.out.println(this);
} }
private void calcOneStep() throws TmException { private void calcOneStep() throws TmException {
@ -62,23 +68,16 @@ public class TM {
if (str[4].length() == 2) { if (str[4].length() == 2) {
leseKopf += 1; leseKopf += 1;
} else { } else {
leseKopf--; leseKopf -= 1;
} }
aktuellerZustand = str[2].length(); aktuellerZustand = str[2].length();
} }
public String toString() { public String toString() {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 15 - leseKopf; i++) {
stringBuilder.append('_');
}
for (Character c : band) { for (Character c : band) {
stringBuilder.append(c); stringBuilder.append(c);
} }
for (int i = 0; i < 15; i++) {
stringBuilder.append('_');
}
return stringBuilder.toString(); return stringBuilder.toString();
} }
@ -86,8 +85,22 @@ public class TM {
return aktuellerZustand == endZustand; 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() { private void parse() {
String[] liste = codierung.substring(1).split("11"); String[] liste = codierung.substring(1).split("11");
funktionen = new String[liste.length][4];
for (int i = 0; i < liste.length; i++) { for (int i = 0; i < liste.length; i++) {
funktionen[i] = liste[i].split("1"); funktionen[i] = liste[i].split("1");
} }
@ -95,9 +108,15 @@ public class TM {
} }
private void initBand(String bandInit) { private void initBand(String bandInit) {
for (int i = 0; i < 15; i++) {
band.add('_');
}
for (int i = 0; i < bandInit.length(); i++) { for (int i = 0; i < bandInit.length(); i++) {
band.add(bandInit.charAt(i)); band.add(bandInit.charAt(i));
} }
for (int i = 0; i < 15; i++) {
band.add('_');
}
} }
private void initAlphabet() { private void initAlphabet() {

22
src/TestClass.java Normal file
View File

@ -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();
}
}