Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
101
Task/Langtons-ant/JavaScript/langtons-ant-1.js
Normal file
101
Task/Langtons-ant/JavaScript/langtons-ant-1.js
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// create global canvas
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.id = 'globalCanvas';
|
||||
document.body.appendChild(canvas);
|
||||
|
||||
function langtonant(antx, optx) {
|
||||
'use strict';
|
||||
var x, y, i;
|
||||
|
||||
// extend default opts
|
||||
var opts = {
|
||||
gridsize: 100,
|
||||
pixlsize: 4,
|
||||
interval: 4
|
||||
};
|
||||
for (i in optx) {
|
||||
opts[i] = optx[i];
|
||||
}
|
||||
|
||||
// extend default ants
|
||||
var ants = [{
|
||||
x: 50,
|
||||
y: 50,
|
||||
d: 0
|
||||
}];
|
||||
for (i in antx) {
|
||||
ants[i] = antx[i];
|
||||
}
|
||||
|
||||
// initialise grid
|
||||
var grid = [];
|
||||
for (x = 0; x < opts.gridsize; x ++) {
|
||||
grid[x] = [];
|
||||
for (y = 0; y < opts.gridsize; y ++) {
|
||||
grid[x][y] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// initialise directions
|
||||
var dirs = [
|
||||
{x: 1, y: 0},
|
||||
{x: 0, y: -1},
|
||||
{x: -1, y: 0},
|
||||
{x: 0, y: 1}
|
||||
];
|
||||
|
||||
// initialise canvas
|
||||
var canv = document.getElementById('globalCanvas');
|
||||
var cont = canv.getContext('2d');
|
||||
canv.width = opts.gridsize * opts.pixlsize;
|
||||
canv.height = opts.gridsize * opts.pixlsize;
|
||||
|
||||
// initialise pixels
|
||||
var pixlblac = cont.createImageData(opts.pixlsize, opts.pixlsize);
|
||||
for (i = 0; i < (opts.pixlsize * opts.pixlsize * 4); i += 4) {
|
||||
pixlblac.data[i + 3] = 255;
|
||||
}
|
||||
var pixlwhit = cont.createImageData(opts.pixlsize, opts.pixlsize);
|
||||
for (i = 0; i < (opts.pixlsize * opts.pixlsize * 4); i += 4) {
|
||||
pixlwhit.data[i + 3] = 0;
|
||||
}
|
||||
|
||||
// run simulation
|
||||
function simulate() {
|
||||
var sane = true;
|
||||
|
||||
// iterate over ants
|
||||
for (i = 0; i < ants.length; i ++) {
|
||||
var n = ants[i];
|
||||
|
||||
// invert, draw, turn
|
||||
if (grid[n.x][n.y]) {
|
||||
grid[n.x][n.y] = false;
|
||||
cont.putImageData(pixlblac, n.x * opts.pixlsize, n.y * opts.pixlsize);
|
||||
n.d --;
|
||||
} else {
|
||||
grid[n.x][n.y] = true;
|
||||
cont.putImageData(pixlwhit, n.x * opts.pixlsize, n.y * opts.pixlsize);
|
||||
n.d ++;
|
||||
}
|
||||
|
||||
// modulus wraparound
|
||||
n.d += dirs.length;
|
||||
n.d %= dirs.length;
|
||||
|
||||
// position + direction
|
||||
n.x += dirs[n.d].x;
|
||||
n.y += dirs[n.d].y;
|
||||
|
||||
// sanity check
|
||||
sane = (n.x < 0 || n.x > opts.gridsize || n.y < 0 || n.y > opts.gridsize) ? false : sane;
|
||||
}
|
||||
|
||||
// loop with interval
|
||||
if (sane) {
|
||||
setTimeout(simulate, opts.interval);
|
||||
}
|
||||
}
|
||||
|
||||
simulate();
|
||||
}
|
||||
5
Task/Langtons-ant/JavaScript/langtons-ant-2.js
Normal file
5
Task/Langtons-ant/JavaScript/langtons-ant-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
langtonant({}, {
|
||||
gridsize: 100,
|
||||
pixlsize: 4,
|
||||
interval: 4
|
||||
});
|
||||
19
Task/Langtons-ant/JavaScript/langtons-ant-3.js
Normal file
19
Task/Langtons-ant/JavaScript/langtons-ant-3.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
langtonant([
|
||||
{
|
||||
x: (100 / 2) + 7,
|
||||
y: (100 / 2) + 7,
|
||||
d: 1
|
||||
}, {
|
||||
x: (100 / 2) + 7,
|
||||
y: (100 / 2) - 7,
|
||||
d: 2
|
||||
}, {
|
||||
x: (100 / 2) - 7,
|
||||
y: (100 / 2) - 7,
|
||||
d: 3
|
||||
}, {
|
||||
x: (100 / 2) - 7,
|
||||
y: (100 / 2) + 7,
|
||||
d: 0
|
||||
}
|
||||
]);
|
||||
68
Task/Langtons-ant/JavaScript/langtons-ant-4.js
Normal file
68
Task/Langtons-ant/JavaScript/langtons-ant-4.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
///////////////////
|
||||
// LODASH IMPORT //
|
||||
///////////////////
|
||||
|
||||
// import all lodash functions to the main namespace, but isNaN not to cause conflicts
|
||||
_.each(_.keys(_), k => window[k === 'isNaN' ? '_isNaN' : k] = _[k]);
|
||||
|
||||
const
|
||||
WORLD_WIDTH = 100,
|
||||
WORLD_HEIGHT = 100,
|
||||
PIXEL_SIZE = 4,
|
||||
DIRTY_COLOR = '#000',
|
||||
VIRGIN_COLOR = '#fff',
|
||||
RUNS = 10000,
|
||||
SPEED = 50,
|
||||
|
||||
// up right down left
|
||||
DIRECTIONS = [0, 1, 2, 3],
|
||||
|
||||
displayWorld = (world) => each(world, (row, rowidx) => {
|
||||
each(row, (cell, cellidx) => {
|
||||
canvas.fillStyle = cell === 1 ? DIRTY_COLOR : VIRGIN_COLOR;
|
||||
canvas.fillRect(rowidx * PIXEL_SIZE, cellidx * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE);
|
||||
});
|
||||
}),
|
||||
|
||||
moveAnt = (world, ant) => {
|
||||
world[ant.x][ant.y] = world[ant.x][ant.y] === 1 ? 0 : 1;
|
||||
ant.dir = DIRECTIONS[(4 + ant.dir + (world[ant.x][ant.y] === 0 ? 1 : -1)) % 4];
|
||||
switch (ant.dir) {
|
||||
case DIRECTIONS[0]:
|
||||
ant.y -= 1;
|
||||
break;
|
||||
case DIRECTIONS[1]:
|
||||
ant.x -= 1;
|
||||
break;
|
||||
case DIRECTIONS[2]:
|
||||
ant.y += 1;
|
||||
break;
|
||||
case DIRECTIONS[3]:
|
||||
ant.x += 1;
|
||||
break;
|
||||
}
|
||||
|
||||
return [world, ant];
|
||||
},
|
||||
|
||||
updateWorld = (world, ant, runs) => {
|
||||
[world, ant] = moveAnt(world, ant);
|
||||
displayWorld(world);
|
||||
|
||||
if (runs > 0) setTimeout(partial(updateWorld, world, ant, --runs), SPEED);
|
||||
},
|
||||
|
||||
canvas = document.getElementById('c').getContext('2d');
|
||||
|
||||
let
|
||||
world = map(range(WORLD_HEIGHT), i => map(range(WORLD_WIDTH), partial(identity, 0))),
|
||||
ant = {
|
||||
x: WORLD_WIDTH / 2,
|
||||
y: WORLD_HEIGHT / 2,
|
||||
dir: DIRECTIONS[0]
|
||||
};
|
||||
|
||||
canvas.canvas.width = WORLD_WIDTH * PIXEL_SIZE;
|
||||
canvas.canvas.height = WORLD_HEIGHT * PIXEL_SIZE;
|
||||
|
||||
updateWorld(world, ant, RUNS);
|
||||
Loading…
Add table
Add a link
Reference in a new issue