Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. int head = 0;
  2. int state = 0;
  3.  
  4. //animations
  5. //scrolling
  6. float scrollFrame = 0;
  7. boolean animateScroll = true;
  8. boolean scrolling = false;
  9. boolean scrollDirection;
  10. float scrollSpeed = 2;
  11. //writing
  12. float writeFrame = 0;
  13. boolean animateWrite = true;
  14. boolean writing = false;
  15. float writeSpeed = 2;
  16. char toWrite;
  17. PGraphics p;
  18.  
  19. //program
  20. String alphabet = "01" + BLANK;
  21. Tape tape;
  22. State[] states;
  23.  
  24. void setup() {
  25.   size(1000, 500, P2D);
  26.   tape = new Tape();
  27.   surface.setTitle("Universal Turing machine emulator");
  28.   frameRate(60);
  29.   surface.setResizable(true);
  30.   states = new State[] {
  31.     decode("1S0; S0;0S0")
  32.   };
  33.   state = 0;
  34.   head = 0;
  35.   scrollFrame = 0;
  36.   scrolling = false;
  37.   writeFrame = 0;
  38.   writing = false;
  39. }
  40.  
  41. void draw() {
  42.   background(255);
  43.   stroke(0);
  44.   line(width / 2 - 25, height - 52, width / 2 - 25, height - 60);
  45.   line(width / 2 + 25, height - 52, width / 2 + 25, height - 60);
  46.   line(width / 2 - 25, height - 60, width / 2 + 25, height - 60);
  47.   fill(0);
  48.   textAlign(CENTER);
  49.   textSize(40);
  50.   int disp = ceil(width / 50) + 2;
  51.   stroke(#ff0000);
  52.   for (int i = -disp; i <= disp; i ++) {
  53.     if (i == 0 && writing) {
  54.       text(tape.get(head + i), width / 2 + i * 50 - scrollFrame, height - 10);
  55.       image(p.get(0, 0, 50, round(writeFrame)), width / 2 + i * 50 - 25 - scrollFrame, height - 50);
  56.       line(width / 2 + i * 50 - 25 - scrollFrame, height - 50 + writeFrame, width / 2 + i * 50 + 25 - scrollFrame, height - 50 + writeFrame);
  57.     }
  58.     else
  59.     {
  60.       fill(0);
  61.       text(tape.get(head + i), width / 2 + i * 50 - scrollFrame, height - 10);
  62.       if (alphabet.indexOf(tape.get(head + i)) == -1) {
  63.         fill(255, 0, 0, 127);
  64.         noStroke();
  65.         rect(width / 2 - 25 + i * 50 - scrollFrame, height - 50, 50, 50);
  66.       }
  67.     }
  68.   }
  69.   stroke(0);
  70.   line(0, height - 50, width, height - 50);
  71.   for (int i = -disp; i <= disp; i ++) {
  72.     line(width / 2 + i * 50 + 25 - scrollFrame, height - 50, width / 2 + i * 50 + 25 - scrollFrame, height);
  73.   }
  74.   if (writing) {
  75.     writeFrame += writeSpeed;
  76.     if (writeFrame >= 50) {
  77.       writing = false;
  78.       writeFrame = 0;
  79.       tape.set(head, toWrite);
  80.     }
  81.   }
  82.   if (scrolling && !writing) {
  83.     if (scrollDirection) {
  84.       scrollFrame += scrollSpeed;
  85.       if (scrollFrame >= 50) {
  86.         scrolling = false;
  87.         scrollFrame = 0;
  88.         head ++;
  89.       }
  90.     }
  91.     else
  92.     {
  93.       scrollFrame -= scrollSpeed;
  94.       if (scrollFrame <= -50) {
  95.         scrolling = false;
  96.         scrollFrame = 0;
  97.         head --;
  98.       }
  99.     }
  100.   }
  101.   if (!scrolling && !writing && state != HALT) {
  102.     states[state].compute();
  103.   }
  104. }
  105.  
  106. void keyPressed() {
  107.   if (mouseY > height - 50) {
  108.     int x = mouseX - width / 2;
  109.     x /= 50;
  110.     println(x);
  111.     tape.set(head + x, key);
  112.   }
  113. }
  114.  
  115. void mousePressed() {
  116.   state = 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement