Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclip5eLP committed Jan 24, 2020
0 parents commit bfb0545
Show file tree
Hide file tree
Showing 9 changed files with 2,820 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc
# Only exists if Bazel was run
/bazel-out

# dependencies
/node_modules
/LangtonsAnt

# profiling files
chrome-profiler-events*.json
speed-measure-plugin*.json

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings
/LangtonsAnt.zip

# System Files
.DS_Store
Thumbs.db
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Langtons Ant

Made By
```
___ ___ ___ ___ ___ ___
/\__\ /\__\ /\ \ /\__\ /\__\ /\ \
/:/ _/_ /:/ / ___ /::\ \ /:/ _/_ /:/ _/_ /::\ \
/:/ /\__\ /:/ / /\__\ /:/\:\__\ /:/ /\ \ /:/ /\__\ /:/\:\__\
/:/ /:/ _/_ /:/ / ___ ___ ___ /:/__/ /:/ /:/ / /:/ /::\ \ /:/ /:/ _/_ ___ ___ /:/ /:/ /
/:/_/:/ /\__\ /:/__/ /\__\ /\ \ /\__\ /::\ \ /:/_/:/ / /:/_/:/\:\__\ /:/_/:/ /\__\ /\ \ /\__\ /:/_/:/ /
\:\/:/ /:/ / \:\ \ /:/ / \:\ \ /:/ / \/\:\ \__ \:\/:/ / \:\/:/ /:/ / \:\/:/ /:/ / \:\ \ /:/ / \:\/:/ /
\::/_/:/ / \:\ /:/ / \:\ /:/ / \:\/\__\ \::/__/ \::/ /:/ / \::/_/:/ / \:\ /:/ / \::/__/
\:\/:/ / \:\/:/ / \:\/:/ / \::/ / \:\ \ \/_/:/ / \:\/:/ / \:\/:/ / \:\ \
\::/ / \::/ / \::/ / /:/ / \:\__\ /:/ / \::/ / \::/ / \:\__\
\/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/
```
## Info

Langtons Ant simulator made in Javascript.
Working release published.

## Usage and Dependencies

Please run `npm install -g [email protected]` globaly to install the needed dependencies.
If you want to package it with the build.bat then please also run `npm install -g electron-packager`.
112 changes: 112 additions & 0 deletions ant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//Ant Logic

var grid = null;
var state = 0;
var pos = [0,0];
var gridCols;
var gridRows;
var res;
var colors;
var id = 0;
var code = "";
var dead = false;

//Constants
const UP = 0;
const RIGHT = 1;
const DOWN = 2;
const LEFT = 3;

var dir = DOWN;

//Main Script Communication
onmessage = function(e) {
//Command: Setup all vars
if (e.data[0] == "setup") {
grid = e.data[1];
gridCols = e.data[2];
gridRows = e.data[3];
pos = e.data[6];
res = e.data[4];
colors = e.data[5];
id = e.data[7];
code = e.data[8];
}

//Command: Receive new Grid
if (e.data[0] == "GRID_SEND") {
grid = e.data[1];
}

//Command: Move and send back Data
if (e.data[0] == "MOVE") {
grid = e.data[1];
if (!dead) algorithm();
if (!dead) postMessage(["sendPos", pos, grid]);
}

//Command: Apply new Code to self
if (e.data[0] == "applyCode") {
code = e.data[1];
}
}

//Main Movement Algorithm
function algorithm() {
var x = pos[0];
var y = pos[1];

//Turn Direction
for (var i = 0; i < code.length; i++) {
if (x >= gridCols || x <= -1) continue;
if (y >= gridRows || y <= -1) continue;
if (grid[x][y] == i) {
var sub = code.substring(i,i+1);
if (sub == "R") turnRight();
if (sub == "L") turnLeft();
var nextCol = (i + 1 + code.length) % code.length;
colorChangeTo(x, y, nextCol);
break;
}
if (grid[x][y] == -1) {
var sub = code.substring(i,i+1);
if (sub == "R") turnRight();
if (sub == "L") turnLeft();
var nextCol = (i + 1 + code.length) % code.length;
colorChangeTo(x, y, nextCol);
break;
}
}

//Move to next
moveForward();

//Check for Bounds
if ((pos[0] > gridCols || pos[0] < 0) || (pos[1] > gridRows || pos[1] < 0)) {
dead = true;
postMessage(["KILL", "OutOfBounds"]);
}
}

//Change Color
function colorChangeTo(x, y, color) {
grid[x][y] = color;
}

//Move Ant 1 Cell forward
function moveForward() {
if (dir == UP) pos[1] -= 1;
if (dir == RIGHT) pos[0] += 1;
if (dir == DOWN) pos[1] += 1;
if (dir == LEFT) pos[0] -= 1;
}

//Turn Ant to the Right
function turnRight() {
dir = (dir + 1 + 4) % 4;
}

//Turn Ant to the Left
function turnLeft() {
dir = (dir - 1 + 4) % 4;
}
3 changes: 3 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
electron-packager . LangtonsAnt --electron-zip-dir %appdata%\..\Local\electron\Cache\
exit
47 changes: 47 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Langtons Ant</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>
<script src="langant.js"></script>
<style>
html {
background-color: #F2F2F2;
}

#main .wrapper {
width: 700px;
border-radius: 20px;
background-color: white;
padding: 15px;
box-shadow: 0px 0px 10px black;
}

h5 {
margin-top: -25px;
}
</style>
</head>
<body>
<div id="main" align="center">
<div class="wrapper">
<h1>Langtons Ant</h1>
<h5>By Eclip5e</h5>
<hr><br><br>
<div id="cell_canvas"></div><br>
Step: <span id="genDisp">0</span><br>
Current Code: <div id="codeDisp"></div><br><br>
Dimensions: <input type="text" placeholder="Enter new Height" id="inputDim"><br>
<button onclick="changeDim();">Change</button>
<hr>
<button onclick="gridStart();">Start / Pause</button> <button onclick="gridClear();">Clear</button><br><br>
Code: <input type="text" placeholder="Enter new Code" id="inputCode"> <button onclick="applyCode();">Apply</button><br>
Tick Speed: <input type="text" placeholder="Enter Tick Speed" id="inputSpeed" oninput="changeSpeed();"><br>
<br><h2><div id="msgService"></div></h2>
</div>
<input id="inputImport" type="file" name="name" style="display:none;">
<input id="GRID_STORE" type="text" name="GRID_STORE" style="display:none;">
</div>
</body>
</html>
Loading

0 comments on commit bfb0545

Please sign in to comment.