Conway's Game of Life: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(30 intermediate revisions by 15 users not shown)
Line 1,238:
Clr all marks
i=0, j=0, steps=0
Tok sep '""'
Locate (1,1)
Line 1,252:
Loop if( Less equal(j,SIZEC) )
[i,j]
If ( Get 'grid' ) //Greater equal (Get 'grid', 1) )
Get 'neighbour_count'
When ( Out of range including '1,4' ) {
Line 1,271:
Print table 'disp grid'
Break if ( Key pressed )
 
++steps
Back
Pause
Line 1,338:
<pre>
La llamada se realiza desde terminal, con el programa "rxvt" de Linux, para adaptar la terminal a los pixeles y dimensiones adecuados.
 
Llamada:
rxvt -g 270x100 -fn "xft:FantasqueSansMono-Regular:pixelsize=3" -e hopper jm/gamelife.jambo
</pre>
 
<pre>
[[File:Captura_de_pantalla_de_2022-10-07_04-37-57.png]]
</pre>
 
<p>Version 2</p>
<p>Me di cuenta de que la subrutina "Count NBR" era un poco lenta, y además, una función que cuente los vecinos en un radio dado, me es útil para hacer juegos, por lo que incluí esa función dentro de las funciones de HOPPER.</p>
<p>Además, reescribí el programa "a la Hopper", como debió ser desde un principio.</p>
<syntaxhighlight lang="text">
#include <jambo.h>
#define SIZER 90
#define SIZEC 120
 
Main
Cls
Hide cursor
 
Dim (SIZER, SIZEC), as zeros 'grid, neighbour_count'
Dim (SIZER, SIZEC), as fill '" ",disp grid'
c=0 , Let( c := Utf8(Chr(254)))
 
m={}
Set ' 0,1,1 ' Apend row to 'm'
Set ' 1,1,0 ' Apend row to 'm'
Set ' 0,1,0 ' Apend row to 'm'
[44:46, 59:61] Set 'm', Put 'grid'
Clr all marks
radio=1, r=0
 
Tok sep '""'
 
Locate (1,1)
Loop
i=1
Iterator ( ++i, Less equal(i,SIZER),\
j=1, Iterator ( ++j, Less equal(j,SIZEC), \
[i,j], Neighbour count (grid,radio), Put 'neighbour_count' ) )
Cartesian ( Greater equal(grid, 1)---Back up to 'r'--- Mul by 'neighbour_count';\
Out of range including '1,4' )
Get range, Set '0," "', Put 'disp grid', Put 'grid', Forget
 
Cartesian ( Not( r ); Mul by 'neighbour_count'; Is equal to '3' )
Get range, Set '1,c', Put 'disp grid', Put 'grid', Forget
Clr range
 
Clr all marks
Print table 'disp grid'
Break if ( Key pressed )
Back
Pause
Show cursor
End
 
</syntaxhighlight>
{{out}}
La salida es la misma, pero ahora va mucho más rápido... parecen estrellitas explotando :D
 
=={{header|APL}}==
[[GNU APL]] (from Wikipedia: https://aplwiki.com/wiki/John_Scholes%27_Conway%27s_Game_of_Life#Translations)<syntaxhighlight lang="apl">
Life←{↑↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}
m ← 5 5⍴(0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0)
m
0 0 0 0 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
Life m
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
Life Life m
0 0 0 0 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
</syntaxhighlight>
 
 
[http://www.dyalog.com/dfnsdws/c_life.htm APL2 (Dyalog) Example in one line]
 
Line 3,747 ⟶ 3,827:
 
=={{header|C++}}==
 
Because Nobody else included a version with Graphics, Here is a simple implementation using SFML for graphic rendering
<syntaxhighlight lang="c">
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <thread>
#include <chrono>
using namespace std;
 
class Life {
private:
int ticks;
bool pass;
int height;
int width;
bool **board;
bool **buffer;
vector<pair<float,float>> liveCoords;
void init();
void lives(int x, int y);
void dies(int x, int y);
bool isAlive(bool **curr, int x, int y);
int checkNeighbors(bool **curr, int x, int y);
void evaluatePosition(bool** curr, int x, int y);
public:
Life(int w = 100, int h = 50, int seed = 1337);
Life(const Life& life);
~Life();
vector<pair<float,float>> doTick();
Life& operator=(const Life& life);
};
void Life::init() {
board = new bool*[height];
buffer = new bool*[height];
for (int y = 0; y < height; y++) {
board[y] = new bool[width];
buffer[y] = new bool[width];
for (int x = 0; x < width; x++) {
board[y][x] = false;
buffer[y][x] = false;
}
}
}
 
Life::Life(int w, int h, int seed) {
width = w;
height = h;
init();
for (int i = 0; i < seed; i++) {
board[rand() % height][rand() % width] = true;
}
pass = true;
}
 
Life::Life(const Life& life) {
width = life.width;
height = life.height;
init();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
board[y][x] = life.board[y][x];
buffer[y][x] = life.buffer[y][x];
}
}
}
 
Life& Life::operator=(const Life& life) {
width = life.width;
height = life.height;
init();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
board[y][x] = life.board[y][x];
buffer[y][x] = life.buffer[y][x];
}
}
return *this;
}
 
Life::~Life() {
for (int i = 0; i < height; i++) {
delete [] board[i];
delete [] buffer[i];
}
delete [] board;
delete [] buffer;
}
 
vector<pair<float,float>> Life::doTick() {
liveCoords.clear();
bool **currentGeneration = pass ? board:buffer;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
evaluatePosition(currentGeneration, x, y);
}
}
pass = !pass;
ticks++;
return liveCoords;
}
 
 
bool Life::isAlive(bool **curr, int x, int y) {
return curr[y][x];
}
int Life::checkNeighbors(bool **curr, int x, int y) {
int lc = 0;
int dx[8] = {-1, 0, 1,1,1,-1, 0,-1};
int dy[8] = {-1,-1,-1,0,1, 1, 1, 0};
for (int i = 0; i < 8; i++) {
int nx = ((dx[i]+x)+width) % width;
int ny = ((dy[i]+y)+height) % height;
lc += isAlive(curr, nx, ny);
}
return lc;
}
void Life::lives(int x, int y) {
if (!pass) {
board[y][x] = true;
} else {
buffer[y][x] = true;
}
liveCoords.push_back(make_pair((float)x,(float)y));
}
void Life::dies(int x, int y) {
if (!pass) {
board[y][x] = false;
} else {
buffer[y][x] = false;
}
}
void Life::evaluatePosition(bool** generation, int x, int y) {
int lc = checkNeighbors(generation, x, y);
if (isAlive(generation, x, y)) {
if (lc == 2 || lc == 3) {
lives(x, y);
} else {
dies(x, y);
}
} else {
if (lc == 3) {
lives(x, y);
} else {
dies(x, y);
}
}
}
 
class App {
private:
void sleep();
void drawLiveCells();
void render();
void handleEvent(sf::Event& event);
void saveDisplay();
int width;
int height;
Life life;
bool isRecording;
int tick;
sf::RenderWindow* window;
sf::RenderTexture* texture;
public:
App(int w = 100, int h = 50);
void start();
};
 
App::App(int w, int h) {
height = h;
width = w;
life = Life(width, height);
isRecording = false;
tick = 0;
}
 
void App::start() {
sf::Event event;
window = new sf::RenderWindow(sf::VideoMode(width*10, height*10), "The Game of Life");
texture = new sf::RenderTexture();
texture->create(width*10, height*10);
window->setFramerateLimit(60);
while (window->isOpen()) {
while (window->pollEvent(event)) {
handleEvent(event);
}
render();
tick++;
}
delete window;
delete texture;
}
 
void App::handleEvent(sf::Event& event) {
if (event.type == sf::Event::Closed) {
window->close();
}
if (event.type == sf::Event::KeyPressed) {
switch (event.key.code) {
case sf::Keyboard::R:
life = Life(width, height);
break;
case sf::Keyboard::S:
isRecording = !isRecording;
break;
case sf::Keyboard::Q:
case sf::Keyboard::Escape:
window->close();
break;
default:
break;
}
}
}
 
void App::sleep() {
std::this_thread::sleep_for(350ms);
}
 
void App::drawLiveCells() {
float XSCALE = 10.0, YSCALE = 10.0;
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(XSCALE, YSCALE));
rect.setFillColor(sf::Color::Green);
auto coords = life.doTick();
texture->clear(sf::Color::Black);
for (auto m : coords) {
rect.setPosition(m.first*XSCALE, m.second*YSCALE);
texture->draw(rect);
}
texture->display();
}
 
void App::render() {
drawLiveCells();
window->clear();
sf::Sprite sprite(texture->getTexture());
window->draw(sprite);
window->display();
if (isRecording) saveDisplay();
sleep();
}
 
void App::saveDisplay() {
string name = "tick" + to_string(tick) + ".png";
sf::Image image = texture->getTexture().copyToImage();
image.saveToFile(name);
}
 
int main(int argc, char* argv[]) {
srand(time(0));
App app;
app.start();
return 0;
}
</syntaxhighlight>
 
And the output of the above program:
[[File:Game of Life.gif|thumb|Life]]
 
 
Considering that the simplest implementation in C++ would lack any use of the object-oriented paradigm, this code was specifically written to demonstrate the various object-oriented features of C++. Thus, while it is somewhat verbose, it fully simulates Conway's Game of Life and is relatively simple to expand to feature different starting shapes.
<syntaxhighlight lang="c">#include <iostream>
Line 4,315 ⟶ 4,657:
 
=={{header|Chapel}}==
 
Compile and run with <code>chpl gol.chpl; ./gol --gridWidth [x] --gridHeight [y]</code>.
<syntaxhighlight lang="chapel">
config const gridHeight : int = 3;
config const gridWidth : int = 3;
 
enum Statestate { dead = 0, alive = 1 };
 
class ConwaysGameofLife
{
var gridDomain : domain(2, int);
var computeDomain : subdomain(gridDomain);
var grid : [gridDomain] state;
 
proc init(height : int, width : int)
{
this.gridDomain = {0..#height+2, 0..#width+2};
this.computeDomain = this.gridDomain.expand(-1);
}
 
 
proc step() : void
{
var tempGrid: [this.computeDomain] state;
 
forall (i,j) in this.computeDomain
{
var isAlive = this.grid[i,j] == state.alive;
var numAlive = (+ reduce this.grid[i-1..i+1, j-1..j+1]:int) - if isAlive then 1 else 0;
tempGrid[i,j] = if ( (2 == numAlive && isAlive) || numAlive == 3 ) then state.alive else state.dead ;
}
 
this.grid[this.computeDomain] = tempGrid;
}
 
proc this(i : int, j : int) ref : state
{
return this.grid[i,j];
}
 
 
proc prettyPrint() : string
{
var str : string;
for i in this.gridDomain.dim(0)
{
if i == 0 || i == gridDomain.dim(0).last
{
for j in this.gridDomain.dim(1)
{
str += "-";
}
}
else
{
for j in this.gridDomain.dim(1)
{
if j == 0 || j == this.gridDomain.dim(1).last
{
str += "|";
}
else
{
str += if this.grid[i,j] == state.alive then "#" else " ";
}
}
}
str += "\n";
}
 
return str;
}
 
class ConwaysGameofLife {
var gridDomain: domain(2);
var computeDomain: subdomain( gridDomain );
var grid: [gridDomain] int;
proc ConwaysGameofLife( height: int, width: int ) {
this.gridDomain = {0..#height+2, 0..#width+2};
this.computeDomain = this.gridDomain.expand( -1 );
}
proc step(){
var tempGrid: [this.computeDomain] State;
forall (i,j) in this.computeDomain {
var isAlive = this.grid[i,j] == State.alive;
var numAlive = (+ reduce this.grid[ i-1..i+1, j-1..j+1 ]) - if isAlive then 1 else 0;
tempGrid[i,j] = if ( (2 == numAlive && isAlive) || numAlive == 3 ) then State.alive else State.dead ;
}
this.grid[this.computeDomain] = tempGrid;
}
proc this( i: int, j: int ) ref : State {
return this.grid[i,j];
}
proc prettyPrint(): string {
var str: string;
for i in this.gridDomain.dim(1) {
if i == 0 || i == gridDomain.dim(1).last {
for j in this.gridDomain.dim(2) {
str += "-";
}
} else {
for j in this.gridDomain.dim(2) {
if j == 0 || j == this.gridDomain.dim(2).last {
str += "|";
} else {
str += if this.grid[i,j] == State.alive then "#" else " ";
}
}
}
str += "\n";
}
return str;
}
 
}
 
 
proc main{
proc main()
var game = new ConwaysGameofLife( gridHeight, gridWidth );
{
game[gridHeight/2 + 1, gridWidth/2 ] = State.alive;
var game[gridHeight/2 += 1new ConwaysGameofLife(gridHeight, gridWidth/2 + 1 ] = State.alive);
 
game[gridHeight/2 + 1, gridWidth/2 + 2 ] = State.alive;
game[gridHeight/2 + 1, gridWidth/2 ] = state.alive;
for i in 1..3 {
game[gridHeight/2 + 1, gridWidth/2 + 1 ] = state.alive;
writeln( game.prettyPrint() );
game[gridHeight/2 + 1, gridWidth/2 + 2 ] = state.alive;
game.step();
 
}
for i in 1..3
{
writeln(game.prettyPrint());
game.step();
}
}
</syntaxhighlight>
Line 4,438 ⟶ 4,806:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobolcobolfree">identification division.
program-id. game-of-life-program.
 
data division.
working-storage section.
Line 4,458 ⟶ 4,827:
05 check-row pic s9.
05 check-cell pic s9.
 
procedure division.
control-paragraph.
Line 4,506 ⟶ 4,876:
if cell(neighbour-row,neighbour-cell) is equal to '#',
and check-cell is not equal to zero or check-row is not equal to zero,
then add 1 to living-neighbours.</syntaxhighlight>
 
end program game-of-life-program.</syntaxhighlight>
{{out}}
<pre>GENERATION 0:
Line 5,304 ⟶ 5,676:
=={{header|EasyLang}}==
 
[https://easylang.onlinedev/apps/game-of-life.html Run it]
 
<syntaxhighlight lang="text">n = 70
n += 170
time = 0.1
#
nx = n + 1
subr init
for r = 1 to n - 1
for c = 1 to n - 1
i = r * nnx + c
if randomf < 0.3
f[i] = 1
.
.
.
.
.
f = 100 / (n - 1)
subr show
clear
for r = 1 to n - 1
for c = 1 to n - 1
if f[r * nnx + c] = 1
move (c -* 1)f *- f (r -* 1)f *- f
rect f * 0.9 f * 0.9
.
.
.
.
.
subr update
swap f[] p[]
for r = 1 to n - 1
sm = 0
i = r * nnx + 1
sr = p[i - n + 1nx] + p[i + 1] + p[i + n + 1nx]
for c = 1 to n - 1
i + sl = 1sm
sl = sm = sr
sm in = sri + 1
sr = p[iin - n + 1nx] + p[i + 1in] + p[i + nin + 1nx]
s = sl + sm + sr
if s = 3 or s = 4 and p[i] = 1
f[i] = 1
else
f[i] = 0
.
i = in
.
.
.
.
on timer
call update
call show
timer 0.2time
.
on mouse_down
c = mouse_x div f + 1
r = mouse_y div f + 1
i = r * nnx + c + n + 1
f[i] = 1 - f[i]
call show
timer 3
.
len f[] nnx * nnx + n + 1nx
len p[] nnx * nnx + n + 1nx
call init
timer 0</syntaxhighlight>
</syntaxhighlight>
 
=={{header|eC}}==
Line 5,528 ⟶ 5,905:
 
=={{header|Elena}}==
ELENA 56.0x, using cellular library
<syntaxhighlight lang="elena">import extensions;
import system'threading;
import system'text;
import cellular;
 
Line 5,540 ⟶ 5,918:
sealed class Model
{
Space theSpace_space;
RuleSet theRuleSet_ruleSet;
bool started_started;
 
event Func<Space, object> OnUpdate : event;
constructor newRandomset(RuleSet transformSet)
{
theSpace_space := new IntMatrixSpace.allocate(maxY, maxX, randomSet);
 
theRuleSet_ruleSet := transformSet;
started_started := false
}
private onUpdate()
constructor newLoaded(RuleSet initSet, RuleSet transformSet)
{
OnUpdate.?(_space)
theSpace := IntMatrixSpace.allocate(maxY, maxX, initSet);
}
theRuleSet := transformSet;
started := false
}
private onUpdaterun()
{
if OnUpdate.?(theSpace_started)
} {
_space.update(_ruleSet)
run() }
{ else
if (started){
{ _started := true
};
theSpace.update(theRuleSet)
}
else
{
started := true
};
self.onUpdate()
}
}
 
singleton gameOfLifeRuleSet : RuleSet
{
int proceed(Space s, int x, int y, ref int retVal)
{
int cell := s.at(x, y);
int number := s.LiveCell(x, y, 1); // NOTE : number of living cells around the self includes the cell itself
if (cell == 0 && number == 3)
{
retVal :=^ 1
}
else if (cell == 1 && (number == 4 || number == 3))
{
retVal :=^ 1
}
else
{
retVal :=^ 0
}
}
}
 
public extension presenterOp : Space
{
print()
{
console.setCursorPosition(0, 0);
int columns := self.Columns;
int rows := self.Rows;
auto for(int iline := 0,new i < rows, i += 1TextBuilder();
for(int i {:= 0; i < rows; i += 1)
{
for(int j := 0, j < columns, j += 1)
{line.clear();
for(int j := 0; j < columns; intj cell :+= self.at(i, j1);
{
int console.write((cell =:= 0)self.iifat("i, ","o")j);
};
line.write((cell == 0).iif(" ","o"));
console.writeLine()};
}
console.writeLine(line.Value)
}
}
}
}
 
Line 5,640 ⟶ 6,011:
model.run();
threadControl.sleep:(DELAY)
};
 
Line 7,068 ⟶ 7,439:
{ „tcol” }
{ „x” } { „y” } { „n” }
 
</syntaxhighlight>
 
=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude args.uh
###sysinclude str.uh
###sysinclude system.uh
// Life simulator (game).
// It is a 'cellular automaton', and was invented by Cambridge mathematician John Conway.
 
// The Rules
 
// For a space that is 'populated':
// Each cell with one or no neighbors dies, as if by solitude.
// Each cell with four or more neighbors dies, as if by overpopulation.
// Each cell with two or three neighbors survives.
// For a space that is 'empty' or 'unpopulated'
// Each cell with three neighbors becomes populated.
// --------------------------------------------------------------------------------------------
#g
// Get the terminal-resolution:
terminallines -- sto tlin
terminalcolumns sto tcol
// .............................
// Verify the commandline parameters:
argc 3 < { #s ."Usage: " 0 argv print SPACE 1 argv print ." lifeshape-file.txt\n" end }
2 argv 'f inv istrue { #s ."The given file ( " 2 argv print ." ) doesn't exist!\n" end }
2 argv filetolist sto startingshape // read the file into the list
[[startingshape]]~~~ sto maxlinlen
@tlin @tcol [[mem]] sto neighbour // Generate the stringarray for the neighbour-calculations
@tlin @tcol [[mem]] sto livingspace // Generate the stringarray for the actual generations
@tlin @tcol [[mem]]! sto cellscreen // Generate the stringarray for the visible livingspace
// Calculate offset for the shape ( it must be put to the centre):
@tlin startingshape~ - 2 / sto originlin
@tcol @maxlinlen - 2 / sto origincol
 
startingshape~ shaperead: {{ {{}} [[startingshape]]~ {{ {{}}§shaperead {{}} [[startingshape]][]
32 > { 1 }{ 0 } sto emblem
@originlin {{}}§shaperead + @origincol {{}} + @emblem inv [[livingspace]][]
}}
}}
cursoroff
// ==================================================================
{.. // infinite loop starts
sbr §renderingsbr
topleft @cellscreen ![[print]]
."Generation: " {..} print fflush // print the number of the generations.
0 [[neighbour]][^] // fill up the neighbour list with zero value
// Calculate neighbourhoods
@tlin linloop: {{ // loop for every lines
@tcol {{ // loop for every columns
{{}}§linloop {{}} sbr §neighbors
{{}}§linloop {{}} @nn inv [[neighbour]][] // store the neighbournumber
}} }}
// Now, kill everybody if the neighbors are less than 2 or more than 3:
@tlin linloop2: {{ // loop for every lines
@tcol {{ // loop for every columns
{{}}§linloop2 {{}} [[neighbour]][] 2 < then §kill
{{}}§linloop2 {{}} [[neighbour]][] 3 > then §kill
{{<}} // Continue the inner loop
kill: {{}}§linloop2 {{}} 0 inv [[livingspace]][]
}} }}
@tlin linloop3: {{ // loop for every lines
@tcol {{ // loop for every columns
// Generate the newborn cells:
{{}}§linloop3 {{}} [[neighbour]][] 3 == { {{}}§linloop3 {{}} 1 inv [[livingspace]][] }
}} }}
50000 inv sleep
..} // infinite loop ends
// ==================================================================
end
// ==========================================================
neighbors: // This subroutine calculates the quantity of neighborhood
sto xx sto yy zero nn
@yy ? @tlin -- @xx ? @tcol -- [[livingspace]][] sum nn // upleft corner
@yy @xx ? @tcol -- [[livingspace]][] sum nn // upmid corner
@yy ++? tlin @xx ? @tcol -- [[livingspace]][] sum nn // upright corner
@yy ? @tlin -- @xx [[livingspace]][] sum nn // midleft corner
@yy ++? tlin @xx [[livingspace]][] sum nn // midright corner
@yy ? @tlin -- @xx ++? tcol [[livingspace]][] sum nn // downleft corner
@yy @xx ++? tcol [[livingspace]][] sum nn // downmid corner
@yy ++? tlin @xx ++? tcol [[livingspace]][] sum nn // downright corner
rts
// ==========================================================
renderingsbr:
@tlin livingspaceloop: {{ @tcol {{
{{}}§livingspaceloop {{}}
{{}}§livingspaceloop {{}} [[livingspace]][] { '* }{ 32 } inv [[cellscreen]][]
}}
}}
rts
 
{ „tlin” }
{ „tcol” }
{ „startingshape” }
{ „maxlinlen” }
{ „livingspace” }
{ „neighbour” }
{ „originlin” }
{ „origincol” }
{ „emblem” }
{ „cellscreen” }
{ „xx” } { „yy” } { „nn” }
 
</syntaxhighlight>
Line 7,119 ⟶ 7,595:
to_int_board board
</syntaxhighlight>
 
 
 
=={{header|FutureBasic}}==
This is just the basic central routine.
<syntaxhighlight lang="futurebasic">
 
Short a(4, 4), c(4, 4) // Initialize arrays of cells and a working copy
 
void local fn seed
Short x, y
// Blinker
a(1, 2) = 1 : a(2, 2) = 1 : a(3, 2) = 1
for y = 1 to 3 : for x = 1 to 3
print a(x, y); // Draw array
next : print : next : print
end fn
 
void local fn nextGen
Short x, y, dx, dy, n
// Calculate next generation on temporary board
for y = 1 to 3 : for x = 1 to 3
c(x, y) = 0 // Initialize
n = -a(x, y) // Don't count center cell
for dy = -1 to 1 : for dx = -1 to 1
n += a(x + dx, y + dy) // Count the neighbours
next : next
c(x, y) = ( n == 3 ) or ( n == 2 and a(x, y) ) // Conway’s rule
next : next
// Copy temp array to actual array and draw
for y = 1 to 3 : for x = 1 to 3
a(x, y) = c(x, y) // Copy
print a(x, y); // Draw
next : print : next : print
end fn
 
fn seed
fn nextGen
fn nextGen
 
handleevents // Go into Mac event loop
 
 
</syntaxhighlight>
Just three generations, as requested:
<pre>
000
111
000
 
010
010
010
 
000
111
000
 
 
</pre>
 
=={{header|Go}}==
Line 7,780 ⟶ 8,316:
0 0 0
1 1 1
0 0 0</syntaxhighlight>
 
</syntaxhighlight>
 
'''Example''' (showing start and six following generations of a glider)
Line 9,392 ⟶ 9,930:
..........##....................................................................
</pre>
 
=={{header|Lambdatalk}}==
Lambdatalk works in any web browsers coming with javascript and canvas.
<syntaxhighlight lang="scheme">
{center
{input {@ type="button" value="start random"
onclick="LIFE.startstop(this,'random')"}}
{input {@ type="button" value="start cross"
onclick="LIFE.startstop(this,'cross')"}}
{canvas {@ id="life" width="330" height="330"
style="box-shadow:0 0 8px #000"}}
}
 
calling the following javascript code
 
{script
var LIFE = (function() {
var board = [],
xmax = 8, ymax = 8,
interval = null,
delay = 1000;
var isdefined = function(x,y) {
return typeof board[x] !== 'undefined'
&& typeof board[x][y] !== 'undefined'
&& board[x][y];
};
 
var alive = function(c,n) {
if (n < 2 || n > 3) c = 0;
if (n === 3) c = 1;
return c
};
 
var neighbours = function(x,y) {
var n = 0;
if (isdefined(x-1,y-1)) n++;
if (isdefined(x ,y-1)) n++;
if (isdefined(x+1,y-1)) n++;
if (isdefined(x-1,y )) n++;
if (isdefined(x+1,y )) n++;
if (isdefined(x-1,y+1)) n++;
if (isdefined(x ,y+1)) n++;
if (isdefined(x+1,y+1)) n++;
return n
};
 
var nextGen = function() {
var next = [];
for (var x = 0; x < xmax; x++) {
next[x] = [];
for (var y = 0; y < ymax; y++)
next[x][y] = alive( board[x][y], neighbours(x,y) );
}
board = next
};
 
var print = function(ctx,dw,dh) {
for (var x = 0; x < xmax; x++) {
for (var y = 0; y < ymax; y++) {
ctx.fillStyle = (board[x][y])? "#fff" : "#888";
ctx.fillRect(y*dh+1,x*dw+1,dh-1,dw-1)
}
}
};
 
var init = function (type,w,h) {
xmax = w;
ymax = h;
delay = (type === "random")? 100 : 1000;
for (var x = 0; x < xmax; x++) {
board[x] = [];
for (var y = 0; y < ymax; y++) {
if (type === "random") {
board[x][y] = Math.round(Math.random());
} else {
board[x][y] = 0;
if (x === Math.floor(w/2)) board[x][y] = 1;
if (y === Math.floor(h/2)) board[x][y] = 1;
}
}
}
};
 
var run = function(ctx,dw,dh) {
print(ctx,dw,dh);
nextGen()
};
 
var startstop = function(id,type) {
var can = document.getElementById('life').getContext('2d');
if (interval === null) {
id.value = "stop";
init(type,33,33);
interval = window.setInterval( run, delay, can, 10, 10 );
} else {
id.value = (type === "random")? "start_random" : "start_cross";
window.clearInterval( interval );
interval = null;
}
};
 
return { startstop }
}) (); // end LIFE
}
</syntaxhighlight>
Results can be seen in http://lambdaway.free.fr/lambdaspeech/?view=life
and in http://lambdaway.free.fr/lambdawalks/?view=conway
 
=={{header|Lua}}==
Line 9,972 ⟶ 10,619:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])</syntaxhighlight>
Blinker over three generations
<syntaxhighlight lang="maxima">
three_all_alive: matrix([0,1,0],[0,1,0],[0,1,0])$
with_slider_draw(n,makelist(j,j,0,2),image(gen(three_all_alive,n),0,0,30,30));
</syntaxhighlight>
[[File:BlinkerMaxima.gif|thumb|center]]
 
=={{header|MiniScript}}==
Line 9,985 ⟶ 10,638:
display(dispIdx).mode = displayMode.tile
td = display(dispIdx)
td.cellSize = 109 // size of cells on screen
td.extent = [cols, rows]
td.overlap = -1 // adds a small gap between cells
Line 12,626 ⟶ 13,279:
 
conway_life()</syntaxhighlight>
 
=={{header|Quackery}}==
 
Uses a Tortoise and Hare algorithm to detect when Life becomes static or repetitive. The tortoise version is displayed, along with five victory laps of the cyclic part once a cycle is detected.
 
<syntaxhighlight lang="Quackery">
[ $ "turtleduck.qky" loadfile ] now!
 
[ $ \
import time
now = time.time()
sleepy_time = 5/7-(now%(5/7))
time.sleep(sleepy_time)
\ python ] is wait ( --> )
 
[ dup 1
[ 2dup > while + 1 >> 2dup / again ]
drop nip ] is sqrt ( n --> n )
 
[ stack ] is width ( --> s )
 
[ tuck witheach
[ over i^ peek + rot i^ poke swap ]
drop ] is add ( [ [ --> [ )
 
[ 1 split swap join ] is left ( [ --> [ )
 
[ -1 split swap join ] is right ( [ --> [ )
 
[ width share split swap join ] is up ( [ --> [ )
 
[ width share negate split swap join ] is down ( [ --> [ )
 
[ left dup up tuck add
swap right tuck add
swap right tuck add
swap down tuck add
swap down tuck add
swap left tuck add
swap left add ] is neighbours ( [ --> [ )
 
[ [] swap
width share times
[ width share split
dip [ 0 swap 0 join join join ] ]
drop
2 width tally
0 width share of tuck join join ] is makeborder ( [ --> [ )
 
[ dup size times
[ 0 swap i^ poke
0 swap i^ width share + 1 - poke
width share step ]
width share times
[ 0 swap i^ poke
0 swap i^ 1 + negate poke ] ] is clearborder ( [ --> [ )
 
[ dup neighbours
witheach
[ over i^ peek iff
[ 1 | 3 != if
[ 0 swap i^ poke ] ]
done
3 = if [ 1 swap i^ poke ] ]
clearborder ] is nextgen ( [ --> [ )
 
[ 6 random
[ table
[ 200 200 255 ] [ 200 255 200 ]
[ 255 200 200 ] [ 200 255 255 ]
[ 255 200 255 ] [ 255 255 200 ] ]
fill
[ 4 times
[ 20 1 walk -1 4 turn ] ] ] is block ( --> )
 
 
[ clear
width share 10 *
dup negate
4 - 1 fly
-1 4 turn
16 - 1 fly
1 4 turn
20 1 fly
' [ 200 200 200 ] colour
4 times
[ width share 2 - 20 * 1 walk
1 4 turn ]
-20 1 fly
' [ 63 63 63 ] colour
width share times
[ width share split
swap witheach
[ if block
20 1 fly ]
width share -20 * 1 fly
1 4 turn
20 1 fly
-1 4 turn ]
drop wait frame ] is draw ( [ --> )
 
[ turtle 0 frames 2 wide
dup size sqrt width put
makeborder
dup
[ dup draw nextgen
dip [ nextgen nextgen ]
2dup = until ]
5 times
[ dup draw nextgen 2dup = until ]
2drop width release ] is life ( [ --> )
 
' [ 0 0 0
1 1 1
0 0 0 ] life ( task requirement )
 
' [ 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 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 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 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 0
0 0 1 1 1 1 1 0 1 1 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 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 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 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 0 0 0 ] life ( more interesting )
 
randomise
[ []
1089 times
[ 2 random join ]
life
again ] ( ... runs forever )</syntaxhighlight>
 
{{out}}
 
<code>( task requirement )</code>
 
[[File:Quackery Life Task.png|thumb|center]]
 
<code>( more interesting )</code>
 
[[File:Quackery Life.gif|thumb|center]]
 
<code>( ... runs forever )</code>
 
https://youtu.be/U5owgEpxzwg
 
(The code runs forever, not the video. The accompanying music is also generative, and comes from the Kolakoski Sequence.)
 
=={{header|R}}==
Line 12,911 ⟶ 13,718:
 
=={{header|Red}}==
<syntaxhighlight lang="rebolred">Red [
Purpose: "Conway's Game of Life"
Author: "Joe Smith"
Line 16,894 ⟶ 17,701:
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import rand
import strings
import time
Line 17,014 ⟶ 17,821:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "timer" for Timer
 
6

edits