Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Added demo gif and improved controls
Browse files Browse the repository at this point in the history
  • Loading branch information
margual56 committed Nov 4, 2021
1 parent cafda93 commit f42250c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/home/marcos/Downloads/processing-3.5.4/core/library/core.jar"/>
<classpathentry kind="lib" path="/home/marcos/Projects/TuringMachine/libs/core.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# TuringMachine
A Turing machine interpreter made in Java using Processing libraries.

![](resources/turing.gif)

## Description
In [this Wikipedia article](https://en.wikipedia.org/wiki/Turing_machine) it is explained what a Turing machine is.
What I have made is a "simulator" of that machine.
Expand Down
Binary file added resources/turing.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 55 additions & 39 deletions src/app/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class MainWindow extends PApplet {
//The default Turing program to execute
String program = "program.tm";
String program = "";

TMd turing;
String text;
Expand All @@ -28,31 +28,34 @@ public void setup() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
System.err.println("Can't set look to \"WindowsLookAndFeel\", what a shame");
}

JFileChooser j = new JFileChooser();
j.setDialogTitle("Select a Turing Machine Code file");

// only allow files of .tm extension
j.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter restrict = new FileNameExtensionFilter("Turing Machine files (.tm)", "tm");
j.addChoosableFileFilter(restrict);

// invoke the showsOpenDialog function to show the save dialog
int r = j.showOpenDialog(null);

// if the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// set the label to the path of the selected file
program = j.getSelectedFile().getAbsolutePath();
}
// if the user cancelled the operation
else {
exit();
return;
if(program.isBlank()) {
String workingDirectory = Paths.get("").toAbsolutePath().toString();
JFileChooser j = new JFileChooser(workingDirectory);
j.setDialogTitle("Select a Turing Machine Code file");

// only allow files of .tm extension
j.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter restrict = new FileNameExtensionFilter("Turing Machine files (.tm)", "tm");
j.addChoosableFileFilter(restrict);

// invoke the showsOpenDialog function to show the save dialog
int r = j.showOpenDialog(null);

// if the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// set the label to the path of the selected file
program = j.getSelectedFile().getAbsolutePath();
}
// if the user cancelled the operation
else {
exit();
return;
}
}

finished = false;
text = "";
state = 1;
Expand Down Expand Up @@ -89,19 +92,9 @@ public void draw() {
text(String.format("Output: %s", ((char) 193) + ""), 50, 350);
else*/
text(String.format("Output: %s", turing.output()), 50, 350);

if (!pause && !finished && frameCount % 10 == 0) {
if (state <= 0)
finished = true;

try {
state = turing.update();
text = turing.printTape();
} catch (Exception error) {
print(error);
finished = true;
}
}

if (!pause && !finished && frameCount % 10 == 0)
doStep();

fill(255);
textSize(60);
Expand All @@ -110,10 +103,20 @@ public void draw() {
}

public void keyPressed() {
if (finished)
if(key == ' ') {
if (finished)
frameCount = -1;
else
pause = !pause;
}else if(keyCode == RIGHT) {
doStep();
}else if(keyCode == ENTER) {
while(!finished)
doStep();
}else if(key == 'r') {
program = "";
frameCount = -1;
else
pause = !pause;
}
}

int sign(float n) {
Expand All @@ -123,6 +126,19 @@ int sign(float n) {
return round(abs(n) / n);
}

private void doStep() {
if (state <= 0)
finished = true;

try {
state = turing.update();
text = turing.printTape();
} catch (Exception error) {
print(error);
finished = true;
}
}

public static void main(String[] args) {
String[] processingArgs = {"Turing Machine"};
MainWindow mySketch = new MainWindow();
Expand Down

0 comments on commit f42250c

Please sign in to comment.