This commit is contained in:
Andrin Fassbind 2022-05-04 17:10:14 +02:00
parent d24e086ae7
commit ec05143ea6
1 changed files with 84 additions and 0 deletions

84
src/TM.java Normal file
View File

@ -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<Character> band;
private int leseKopf;
private Map<Character, Integer> 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);
}
}
}