Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6.  
  7. public class TuringMachine {
  8.     //region Fields
  9.     private List<String> alphabet;
  10.     private List<String> tape;
  11.     private ListIterator<String> carriage;
  12.     private List<State> states;
  13.     //endregion
  14.  
  15.     public TuringMachine() {
  16.         alphabet = new ArrayList<>() {{
  17.             add(" ");
  18.         }};
  19.         tape     = new ArrayList<>(Collections.nCopies(200, " "));
  20.         carriage = tape.listIterator(tape.size() / 2);
  21.         states   = new ArrayList<>();
  22.     }
  23.  
  24.     public void setAlphabet(String... alphabet) {
  25.         Arrays.stream(alphabet).forEach(this::alphabetAdd);
  26.     }
  27.  
  28.     private void alphabetAdd(String word) {
  29.         if (!alphabet.contains(word))
  30.             alphabet.add(word);
  31.     }
  32.  
  33.     public void addWord(String word) {
  34.         if (!alphabet.contains(word))
  35.             throw new IllegalArgumentException();
  36.  
  37.         tape.add(word);
  38.     }
  39.  
  40.     public void addAllWords(String... words) {
  41.         Arrays.stream(words).forEach(this::addWord);
  42.     }
  43.  
  44.     public static class State {
  45.         //region Fields
  46.         private String[] forWords;
  47.         private String[] words;
  48.         private String[] transitions;
  49.         private int[] doubleStates;
  50.         private int index;
  51.         private static int nextIndex;
  52.         //endregion
  53.  
  54.         public State(String[] forWords, String[] words, String[] transitions, int[] doubleStates) {
  55.             this.forWords     = forWords;
  56.             this.words        = words;
  57.             this.transitions  = transitions;
  58.             this.doubleStates = doubleStates;
  59.             index             = ++nextIndex;
  60.         }
  61.  
  62.         public static int getIndex() {
  63.             return nextIndex;
  64.         }
  65.     }
  66.  
  67.     public void addState(String[] forWords, String[] words, String[] transitions, int[] doubleStates) {
  68.         checkWords(forWords);
  69.         checkWords(words);
  70.         checkTransitions(transitions);
  71.         checkDoubleStates(doubleStates);
  72.  
  73.         states.add(new State(forWords, words, transitions, doubleStates));
  74.     }
  75.  
  76.     private void checkWords(String[] words) {
  77.         Arrays.stream(words).forEach(word -> {
  78.             if (!alphabet.contains(word))
  79.                 throw new IllegalArgumentException();
  80.         });
  81.     }
  82.  
  83.     private void checkTransitions(String[] transitions) {
  84.         Arrays.stream(transitions).forEach(transition -> {
  85.             switch (transition) {
  86.                 case "<":
  87.                 case ">":
  88.                 case ".": break;
  89.                 default:
  90.                     throw new IllegalArgumentException();
  91.             }
  92.         });
  93.     }
  94.  
  95.     private void checkDoubleStates(int[] doubleStates) {
  96.         Arrays.stream(doubleStates).forEach(doubleState -> {
  97.             if (doubleState > State.getIndex())
  98.                 throw new IllegalArgumentException();
  99.         });
  100.     }
  101.  
  102.     public List<String> run() {
  103.         return tape;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement