Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int head = 0;
- int state = 0;
- //animations
- //scrolling
- float scrollFrame = 0;
- boolean animateScroll = true;
- boolean scrolling = false;
- boolean scrollDirection;
- float scrollSpeed = 2;
- //writing
- float writeFrame = 0;
- boolean animateWrite = true;
- boolean writing = false;
- float writeSpeed = 2;
- char toWrite;
- PGraphics p;
- //program
- String alphabet = "01" + BLANK;
- Tape tape;
- State[] states;
- void setup() {
- size(1000, 500, P2D);
- tape = new Tape();
- surface.setTitle("Universal Turing machine emulator");
- frameRate(60);
- surface.setResizable(true);
- states = new State[] {
- decode("1S0; S0;0S0")
- };
- state = 0;
- head = 0;
- scrollFrame = 0;
- scrolling = false;
- writeFrame = 0;
- writing = false;
- }
- void draw() {
- background(255);
- stroke(0);
- line(width / 2 - 25, height - 52, width / 2 - 25, height - 60);
- line(width / 2 + 25, height - 52, width / 2 + 25, height - 60);
- line(width / 2 - 25, height - 60, width / 2 + 25, height - 60);
- fill(0);
- textAlign(CENTER);
- textSize(40);
- int disp = ceil(width / 50) + 2;
- stroke(#ff0000);
- for (int i = -disp; i <= disp; i ++) {
- if (i == 0 && writing) {
- text(tape.get(head + i), width / 2 + i * 50 - scrollFrame, height - 10);
- image(p.get(0, 0, 50, round(writeFrame)), width / 2 + i * 50 - 25 - scrollFrame, height - 50);
- line(width / 2 + i * 50 - 25 - scrollFrame, height - 50 + writeFrame, width / 2 + i * 50 + 25 - scrollFrame, height - 50 + writeFrame);
- }
- else
- {
- fill(0);
- text(tape.get(head + i), width / 2 + i * 50 - scrollFrame, height - 10);
- if (alphabet.indexOf(tape.get(head + i)) == -1) {
- fill(255, 0, 0, 127);
- noStroke();
- rect(width / 2 - 25 + i * 50 - scrollFrame, height - 50, 50, 50);
- }
- }
- }
- stroke(0);
- line(0, height - 50, width, height - 50);
- for (int i = -disp; i <= disp; i ++) {
- line(width / 2 + i * 50 + 25 - scrollFrame, height - 50, width / 2 + i * 50 + 25 - scrollFrame, height);
- }
- if (writing) {
- writeFrame += writeSpeed;
- if (writeFrame >= 50) {
- writing = false;
- writeFrame = 0;
- tape.set(head, toWrite);
- }
- }
- if (scrolling && !writing) {
- if (scrollDirection) {
- scrollFrame += scrollSpeed;
- if (scrollFrame >= 50) {
- scrolling = false;
- scrollFrame = 0;
- head ++;
- }
- }
- else
- {
- scrollFrame -= scrollSpeed;
- if (scrollFrame <= -50) {
- scrolling = false;
- scrollFrame = 0;
- head --;
- }
- }
- }
- if (!scrolling && !writing && state != HALT) {
- states[state].compute();
- }
- }
- void keyPressed() {
- if (mouseY > height - 50) {
- int x = mouseX - width / 2;
- x /= 50;
- println(x);
- tape.set(head + x, key);
- }
- }
- void mousePressed() {
- state = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement