Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. class State {
  2.   StateAction[] actions;
  3.   State(StateAction[] S_actions) {
  4.     actions = S_actions;
  5.   }
  6.   void compute() {
  7.     if (alphabet.indexOf(tape.get(head)) == -1) {
  8.       state = HALT;
  9.       println("Symbol \'" + tape.get(head) + "\' does not appear in alphabet \'" + alphabet + "\'");
  10.     }
  11.     else
  12.     {
  13.       actions[alphabet.indexOf(tape.get(head))].apply();
  14.     }
  15.   }
  16. }
  17.  
  18. class StateAction {
  19.   char write;
  20.   int move;
  21.   int change;
  22.   StateAction(char S_write, int S_move, int S_change) {
  23.     write = S_write;
  24.     move = S_move;
  25.     change = S_change;
  26.   }
  27.   void apply() {
  28.     if ((write != tape.get(head)) && animateWrite) {
  29.       writing = true;
  30.       toWrite = write;
  31.       p = createGraphics(50, 50, P2D);
  32.       p.beginDraw();
  33.       p.background(255);
  34.       p.textAlign(CENTER);
  35.       p.textSize(40);
  36.       p.fill(0);
  37.       p.text(toWrite, 25, 40);
  38.       if (alphabet.indexOf(toWrite) == -1) {
  39.         p.fill(255, 0, 0, 127);
  40.         p.noStroke();
  41.         p.rect(0, 0, 50, 50);
  42.       }
  43.       p.endDraw();
  44.     }
  45.     else
  46.     {
  47.       tape.set(head, write);
  48.     }
  49.     switch(move) {
  50.       case STAY:
  51.         break;
  52.       case RIGHT:
  53.         if (animateScroll) {
  54.           scrolling = true;
  55.           scrollDirection = true;
  56.         }
  57.         else
  58.         {
  59.           head ++;
  60.         }
  61.         break;
  62.       case LEFT:
  63.         if (animateScroll) {
  64.           scrolling = true;
  65.           scrollDirection = false;
  66.         }
  67.         else
  68.         {
  69.           head --;
  70.         }
  71.         break;
  72.     }
  73.     if (change != KEEP) state = change;
  74.   }
  75. }
  76.  
  77. State decode(String in) {
  78.   in = in.replace(";", ":");
  79.   String[] split = in.split(":");
  80.   if (split.length < alphabet.length()) return null;
  81.   StateAction[] states = new StateAction[alphabet.length()];
  82.   for (int i = 0; i < states.length; i++) {
  83.     states[i] = __subdec(split[i]);
  84.     if (states[i] == null) return null;
  85.   }
  86.   return new State(states);
  87. }
  88.  
  89. StateAction __subdec(String in) {
  90.   if (in.length() < 3) return null;
  91.   char write = in.charAt(0);
  92.   int move = STAY;
  93.   if (in.charAt(1) == 'L') move = LEFT;
  94.   if (in.charAt(1) == 'R') move = RIGHT;
  95.   String state = in.substring(2);
  96.   int change = HALT;
  97.   if (!state.equals("H")) {
  98.     try {
  99.       change = Integer.parseInt(state);
  100.     } catch(NumberFormatException e) {
  101.       return null;
  102.     }
  103.   }
  104.   return new StateAction(write, move, change);
  105. }
  106.  
  107. static final int STAY = 2346579;
  108. static final int KEEP = -1;
  109. static final int HALT = -2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement