Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class State {
- StateAction[] actions;
- State(StateAction[] S_actions) {
- actions = S_actions;
- }
- void compute() {
- if (alphabet.indexOf(tape.get(head)) == -1) {
- state = HALT;
- println("Symbol \'" + tape.get(head) + "\' does not appear in alphabet \'" + alphabet + "\'");
- }
- else
- {
- actions[alphabet.indexOf(tape.get(head))].apply();
- }
- }
- }
- class StateAction {
- char write;
- int move;
- int change;
- StateAction(char S_write, int S_move, int S_change) {
- write = S_write;
- move = S_move;
- change = S_change;
- }
- void apply() {
- if ((write != tape.get(head)) && animateWrite) {
- writing = true;
- toWrite = write;
- p = createGraphics(50, 50, P2D);
- p.beginDraw();
- p.background(255);
- p.textAlign(CENTER);
- p.textSize(40);
- p.fill(0);
- p.text(toWrite, 25, 40);
- if (alphabet.indexOf(toWrite) == -1) {
- p.fill(255, 0, 0, 127);
- p.noStroke();
- p.rect(0, 0, 50, 50);
- }
- p.endDraw();
- }
- else
- {
- tape.set(head, write);
- }
- switch(move) {
- case STAY:
- break;
- case RIGHT:
- if (animateScroll) {
- scrolling = true;
- scrollDirection = true;
- }
- else
- {
- head ++;
- }
- break;
- case LEFT:
- if (animateScroll) {
- scrolling = true;
- scrollDirection = false;
- }
- else
- {
- head --;
- }
- break;
- }
- if (change != KEEP) state = change;
- }
- }
- State decode(String in) {
- in = in.replace(";", ":");
- String[] split = in.split(":");
- if (split.length < alphabet.length()) return null;
- StateAction[] states = new StateAction[alphabet.length()];
- for (int i = 0; i < states.length; i++) {
- states[i] = __subdec(split[i]);
- if (states[i] == null) return null;
- }
- return new State(states);
- }
- StateAction __subdec(String in) {
- if (in.length() < 3) return null;
- char write = in.charAt(0);
- int move = STAY;
- if (in.charAt(1) == 'L') move = LEFT;
- if (in.charAt(1) == 'R') move = RIGHT;
- String state = in.substring(2);
- int change = HALT;
- if (!state.equals("H")) {
- try {
- change = Integer.parseInt(state);
- } catch(NumberFormatException e) {
- return null;
- }
- }
- return new StateAction(write, move, change);
- }
- static final int STAY = 2346579;
- static final int KEEP = -1;
- static final int HALT = -2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement