Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,99 @@
|
|||
function GameOfLife () {
|
||||
|
||||
this.init = function (turns,width,height) {
|
||||
this.board = new Array(height);
|
||||
for (var x = 0; x < height; x++) {
|
||||
this.board[x] = new Array(width);
|
||||
for (var y = 0; y < width; y++) {
|
||||
this.board[x][y] = Math.round(Math.random());
|
||||
}
|
||||
}
|
||||
this.turns = turns;
|
||||
}
|
||||
|
||||
this.nextGen = function() {
|
||||
this.boardNext = new Array(this.board.length);
|
||||
for (var i = 0; i < this.board.length; i++) {
|
||||
this.boardNext[i] = new Array(this.board[i].length);
|
||||
}
|
||||
for (var x = 0; x < this.board.length; x++) {
|
||||
for (var y = 0; y < this.board[x].length; y++) {
|
||||
var n = 0;
|
||||
for (var dx = -1; dx <= 1; dx++) {
|
||||
for (var dy = -1; dy <= 1; dy++) {
|
||||
if ( dx == 0 && dy == 0){}
|
||||
else if (typeof this.board[x+dx] !== 'undefined'
|
||||
&& typeof this.board[x+dx][y+dy] !== 'undefined'
|
||||
&& this.board[x+dx][y+dy]) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
var c = this.board[x][y];
|
||||
switch (n) {
|
||||
case 0:
|
||||
case 1:
|
||||
c = 0;
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
case 3:
|
||||
c = 1;
|
||||
break;
|
||||
default:
|
||||
c = 0;
|
||||
}
|
||||
this.boardNext[x][y] = c;
|
||||
}
|
||||
}
|
||||
this.board = this.boardNext.slice();
|
||||
}
|
||||
|
||||
this.print = function() {
|
||||
for (var x = 0; x < this.board.length; x++) {
|
||||
var l = "";
|
||||
for (var y = 0; y < this.board[x].length; y++) {
|
||||
if (this.board[x][y])
|
||||
l += "X";
|
||||
else
|
||||
l += " ";
|
||||
}
|
||||
print(l);
|
||||
}
|
||||
}
|
||||
|
||||
this.start = function() {
|
||||
for (var t = 0; t < this.turns; t++) {
|
||||
print("---\nTurn "+(t+1));
|
||||
this.print();
|
||||
this.nextGen()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var game = new GameOfLife();
|
||||
|
||||
print("---\n3x3 Blinker over three turns.");
|
||||
game.init(3);
|
||||
game.board = [
|
||||
[0,0,0],
|
||||
[1,1,1],
|
||||
[0,0,0]];
|
||||
game.start();
|
||||
|
||||
print("---\n10x6 Glider over five turns.");
|
||||
game.init(5);
|
||||
game.board = [
|
||||
[0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,1,0,0,0,0,0,0,0],
|
||||
[0,0,0,1,0,0,0,0,0,0],
|
||||
[0,1,1,1,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0]];
|
||||
game.start();
|
||||
|
||||
print("---\nRandom 5x10");
|
||||
game.init(5,5,10);
|
||||
game.start();
|
||||
151
Task/Conways-Game-of-Life/JavaScript/conways-game-of-life-2.js
Normal file
151
Task/Conways-Game-of-Life/JavaScript/conways-game-of-life-2.js
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
<script type="text/javascript">
|
||||
|
||||
function GameOfLife () {
|
||||
|
||||
this.init = function (turns,width,height) {
|
||||
this.board = new Array(height);
|
||||
for (var x = 0; x < height; x++) {
|
||||
this.board[x] = new Array(width);
|
||||
for (var y = 0; y < width; y++) {
|
||||
this.board[x][y] = Math.round(Math.random());
|
||||
}
|
||||
}
|
||||
this.turns = turns;
|
||||
}
|
||||
|
||||
this.nextGen = function() {
|
||||
this.boardNext = new Array(this.board.length);
|
||||
for (var i = 0; i < this.board.length; i++) {
|
||||
this.boardNext[i] = new Array(this.board[i].length);
|
||||
}
|
||||
for (var x = 0; x < this.board.length; x++) {
|
||||
for (var y = 0; y < this.board[x].length; y++) {
|
||||
var n = 0;
|
||||
for (var dx = -1; dx <= 1; dx++) {
|
||||
for (var dy = -1; dy <= 1; dy++) {
|
||||
if ( dx == 0 && dy == 0){}
|
||||
else if (typeof this.board[x+dx] !== 'undefined'
|
||||
&& typeof this.board[x+dx][y+dy] !== 'undefined'
|
||||
&& this.board[x+dx][y+dy]) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
var c = this.board[x][y];
|
||||
switch (n) {
|
||||
case 0:
|
||||
case 1:
|
||||
c = 0;
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
case 3:
|
||||
c = 1;
|
||||
break;
|
||||
default:
|
||||
c = 0;
|
||||
}
|
||||
this.boardNext[x][y] = c;
|
||||
}
|
||||
}
|
||||
this.board = this.boardNext.slice();
|
||||
}
|
||||
|
||||
this.print = function(ctx,w,h) {
|
||||
if (!w)
|
||||
w = 8;
|
||||
if (!h)
|
||||
h = 8;
|
||||
for (var x = 0; x < this.board.length; x++) {
|
||||
var l = "";
|
||||
for (var y = 0; y < this.board[x].length; y++) {
|
||||
if (this.board[x][y])
|
||||
// x and y reversed to draw matrix like it looks in source
|
||||
// rather than the "actual" positions
|
||||
ctx.fillStyle = "orange";
|
||||
else
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(y*h,x*w,h,w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.start = function(ctx,w,h) {
|
||||
for (var t = 0; t < this.turns; t++) {
|
||||
this.print(ctx,w,h);
|
||||
this.nextGen()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function init() {
|
||||
// Change document title and text under canvas
|
||||
document.title = "Conway's Game of Life";
|
||||
|
||||
// Setup game boards for Conway's Game of Life
|
||||
var blinker = new GameOfLife();
|
||||
blinker.board = [
|
||||
[0,1,0],
|
||||
[0,1,0],
|
||||
[0,1,0]];
|
||||
|
||||
var glider = new GameOfLife();
|
||||
glider.board = [
|
||||
[0,0,0,0,0,0],
|
||||
[0,0,1,0,0,0],
|
||||
[0,0,0,1,0,0],
|
||||
[0,1,1,1,0,0],
|
||||
[0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0]];
|
||||
|
||||
var random = new GameOfLife();
|
||||
random.init(null,8,8);
|
||||
|
||||
// Get canvas contexts or return 1
|
||||
blinker.canvas = document.getElementById('blinker');
|
||||
glider.canvas = document.getElementById('glider');
|
||||
random.canvas = document.getElementById('random');
|
||||
if (blinker.canvas.getContext && glider.canvas.getContext && random.canvas.getContext) {
|
||||
blinker.ctx = blinker.canvas.getContext('2d');
|
||||
glider.ctx = glider.canvas.getContext('2d');
|
||||
random.ctx = random.canvas.getContext('2d');
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Run main() at set interval
|
||||
setInterval(function(){run(glider,glider.ctx,25,25)},250);
|
||||
setInterval(function(){run(blinker,blinker.ctx,25,25)},250);
|
||||
setInterval(function(){run(random,random.ctx,25,25)},250);
|
||||
return 0;
|
||||
}
|
||||
|
||||
function run(game,ctx,w,h) {
|
||||
game.print(ctx,w,h);
|
||||
game.nextGen()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body onLoad="init();">
|
||||
3x3 Blinker<br>
|
||||
<canvas id="blinker" width="75" height="75">
|
||||
No canvas support found!
|
||||
</canvas><br><br>
|
||||
6x6 Glider<br>
|
||||
<canvas id="glider" width="150" height="150">
|
||||
No canvas support found!
|
||||
</canvas><br><br>
|
||||
8x8 Random<br>
|
||||
<canvas id="random" width="200" height="200">
|
||||
No canvas support found!
|
||||
</canvas><br>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
const _ = require('lodash');
|
||||
|
||||
///////////////////
|
||||
// LODASH IMPORT //
|
||||
///////////////////
|
||||
|
||||
// import all lodash functions to the main namespace, but isNaN not to cause conflicts
|
||||
_.each(_.keys(_), k => global[k === 'isNaN' ? '_isNaN' : k] = _[k]);
|
||||
|
||||
///////////////
|
||||
// FUNCTIONS //
|
||||
///////////////
|
||||
const WORLD_WIDTH = 3,
|
||||
WORLD_HEIGHT = 3,
|
||||
displayWorld = (world) => console.log(map(world, x => x.join(' ')).join('\n') + '\n'),
|
||||
|
||||
aliveNeighbours = (world, x, y) => chain(range(-1, 2))
|
||||
.reduce((acc, i) => acc.concat(map(range(-1, 2), ii => [i, ii])), [])
|
||||
.reject(partial(isEqual, [0, 0]))
|
||||
.map(i => {
|
||||
try {
|
||||
return world[x + i[0]][y + i[1]];
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.compact()
|
||||
.value()
|
||||
.length,
|
||||
|
||||
isAlive = (cell, numAliveNeighbours) => (cell === 1 && inRange(numAliveNeighbours, 2, 4)) || (cell === 0 && numAliveNeighbours === 3) ? 1 : 0,
|
||||
updateWorld = (world) => map(world, (row, rowidx) => map(row, (cell, colidx) => isAlive(cell, aliveNeighbours(world, rowidx, colidx))));
|
||||
|
||||
|
||||
// let world = map(range(WORLD_WIDTH), partial(ary(map, 2), range(WORLD_HEIGHT), partial(random, 0, 1, false)));
|
||||
let world = [[0, 0, 0], [1, 1, 1], [0, 0, 0]];
|
||||
|
||||
setInterval(() => {
|
||||
world = updateWorld(world)
|
||||
displayWorld(world);
|
||||
}, 1000);
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
const alive = 1;
|
||||
const dead = 0;
|
||||
|
||||
const conwaysGameOfLife = (game) => {
|
||||
const newGame = []
|
||||
for (let y = 0; y < game.length; y += 1) {
|
||||
const newRow = []
|
||||
for (let x = 0; x < game[y].length; x += 1) {
|
||||
const cell = game[y][x];
|
||||
const prevX = x > 0 ? x - 1 : x;
|
||||
const nextX = x < game[y].length - 1 ? x + 2 : x + 1;
|
||||
const counter =
|
||||
(game[y - 1] ? game[y - 1].slice(prevX, nextX).reduce((acc, v) => acc + v) : 0) +
|
||||
(game[y][x - 1] || 0) + (game[y][x + 1] || 0) +
|
||||
(game[y + 1] ? game[y + 1].slice(prevX, nextX).reduce((acc, v) => acc + v) : 0)
|
||||
cell === alive
|
||||
? counter > 1 && counter <= 3
|
||||
? newRow.push(alive)
|
||||
: newRow.push(dead)
|
||||
: counter === 3
|
||||
? newRow.push(alive)
|
||||
: newRow.push(dead)
|
||||
}
|
||||
newGame.push(newRow)
|
||||
}
|
||||
return newGame
|
||||
}
|
||||
|
||||
const generateGame = (height, width) => {
|
||||
return Array.from({ length: height }, (v, k) => (
|
||||
Array.from({ length: width}, (v, k) => {
|
||||
return (Math.random() * 100 | 0) < 50 ? dead : alive
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
const output = (game) =>{
|
||||
process.stdout.write('\033c');
|
||||
let screen = '';
|
||||
for (let i = 0; i < game.length; i += 1) {
|
||||
screen += game[i].join('')
|
||||
screen += '\n'
|
||||
}
|
||||
console.log(screen)
|
||||
}
|
||||
|
||||
const setup = ((game) => {
|
||||
return () => {
|
||||
setInterval(() => {
|
||||
output(game)
|
||||
const newGame = conwaysGameOfLife(game)
|
||||
game = newGame
|
||||
}, 1000)
|
||||
}
|
||||
})
|
||||
|
||||
// for random game
|
||||
// const game = generateGame(10, 10)
|
||||
|
||||
// glider test
|
||||
const game = [
|
||||
[0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,1,0,0,0,0,0,0,0],
|
||||
[0,0,0,1,0,0,0,0,0,0],
|
||||
[0,1,1,1,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0]
|
||||
];
|
||||
const run = setup(game);
|
||||
run()
|
||||
Loading…
Add table
Add a link
Reference in a new issue