rdy to do first tests

This commit is contained in:
Andrin Fassbind 2022-05-04 21:08:34 +02:00
parent ec05143ea6
commit 0505a0868e
1 changed files with 40 additions and 7 deletions

View File

@ -11,9 +11,11 @@ public class TM {
private List<Character> band; private List<Character> band;
private int leseKopf; private int leseKopf;
private Map<Character, Integer> alphabet; private Map<Character, Integer> alphabet;
private int numberOfSteps;
public TM(String codierung, String bandInitialisierung) { public TM(String codierung, String bandInitialisierung) {
numberOfSteps = 0;
leseKopf = 0; leseKopf = 0;
band = new ArrayList<>(); band = new ArrayList<>();
this.codierung = codierung; 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 { private void calcOneStep() throws TmException {
boolean found = false; boolean found = false;
for (String[] str : funktionen) { for (String[] str : funktionen) {
if (str[0].length() == aktuellerZustand && str[1].length() == alphabet.get(band.get(leseKopf))) { if (str[0].length() == aktuellerZustand && str[1].length() == alphabet.get(band.get(leseKopf))) {
found = true; found = true;
writeOnBand(str); writeOnBand(str);
numberOfSteps++;
break; break;
} }
} }
if (!found) { if (!found) {
throw new TmException("Couldn't find matching function for Character on band"); throw new TmException("Couldn't find matching function for Character on band");
} }
} }
private void writeOnBand(String[] str) { private void writeOnBand(String[] str) {
aktuellerZustand = str[2].length();
//TODO Find corect char from Integer value
for (Character c : alphabet.keySet()) { for (Character c : alphabet.keySet()) {
if (alphabet.get(c) == str[3].length()) { if (alphabet.get(c) == str[3].length()) {
band.set(leseKopf, c); 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() { private void parse() {
@ -58,7 +92,6 @@ public class TM {
funktionen[i] = liste[i].split("1"); funktionen[i] = liste[i].split("1");
} }
aktuellerZustand = funktionen[0][0].length(); aktuellerZustand = funktionen[0][0].length();
//TODO Startzustand wissen?
} }
private void initBand(String bandInit) { private void initBand(String bandInit) {