https://aplwiki.com/index.php?title=Conway%27s_Game_of_Life&feed=atom&action=historyConway's Game of Life - Revision history2020-01-17T22:39:10ZRevision history for this page on the wikiMediaWiki 1.33.1https://aplwiki.com/index.php?title=Conway%27s_Game_of_Life&diff=1959&oldid=prevRichPark: 1 revision imported2019-11-21T09:39:48Z<p>1 revision imported</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr class="diff-title" lang="en-GB">
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">Revision as of 09:39, 21 November 2019</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en-GB"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>RichParkhttps://aplwiki.com/index.php?title=Conway%27s_Game_of_Life&diff=1958&oldid=prevMiraheze>Adám Brudzewsky: /* External links */2019-11-18T09:40:57Z<p><span dir="auto"><span class="autocomment">External links</span></span></p>
<p><b>New page</b></p><div>'''[[wikipedia:Conway's Game of Life|Conway's Game of Life]]''' is a well-known cellular automaton in which each generation of a population 'evolves' from the previous one according to a set of predefined rules.<br />
<br />
Here is [[John Scholes]]' famous one-line APL function takes a boolean [[matrix]] as argument and returns a boolean matrix with the new generation. <br />
<br />
<source lang=apl><br />
Life←{↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}<br />
</source><br />
<br />
== The Glider ==<br />
<br />
One interesting pattern that occurs in Life is a [[wikipedia:Glider (Conway's Life)|Glider]]:<br />
<source lang=apl><br />
glider←3 3⍴1 1 1 1 0 0 0 1 0<br />
glider<br />
1 1 1<br />
1 0 0<br />
0 1 0<br />
</source><br />
<br />
To see how the Glider evolves over the generations, we need to give it some space to move around. The Game of Life takes place on an infinite two-dimensional grid, but that might be a bit large for our computer! Let's start the Glider off in the bottom right-hand corner of a 10 x 10 grid:<br />
<source lang=apl><br />
grid←¯10 ¯10↑glider<br />
grid<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 1 1 1<br />
0 0 0 0 0 0 0 1 0 0<br />
0 0 0 0 0 0 0 0 1 0<br />
</source><br />
<br />
After one generation the pattern becomes:<br />
<source lang=apl><br />
Life grid<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 0 0<br />
0 0 0 0 0 0 0 0 1 0<br />
0 0 0 0 0 0 0 1 1 0<br />
0 0 0 0 0 0 0 1 0 1<br />
0 0 0 0 0 0 0 0 0 0<br />
</source><br />
<br />
The Glider has slightly changed shape and also moved up and to the left.<br />
<br />
=== Visualising it ===<br />
<br />
To make the pattern easier to see, we can print a <source lang=apl inline>⌺</source> symbol where there's a cell and a <source lang=apl inline>·</source> otherwise, for example:<br />
<source lang=apl><br />
Show←{'·⌺'[⎕IO+⍵]}<br />
Show grid<br />
··········<br />
··········<br />
··········<br />
··········<br />
··········<br />
··········<br />
··········<br />
·······⌺⌺⌺<br />
·······⌺··<br />
········⌺·<br />
</source><br />
<br />
Here's how the Glider evolves over the first ten generations:<br />
<source lang=apl><br />
Show¨{Life⍣⍵⊢grid}¨0,⍳9<br />
┌──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐<br />
│··········│··········│··········│··········│··········│··········│··········│··········│··········│··········│<br />
│··········│··········│··········│··········│··········│··········│··········│··········│··········│··········│<br />
│··········│··········│··········│··········│··········│··········│··········│··········│··········│··········│<br />
│··········│··········│··········│··········│··········│··········│··········│··········│··········│··········│<br />
│··········│··········│··········│··········│··········│··········│··········│··········│··········│······⌺···│<br />
│··········│··········│··········│··········│··········│·······⌺··│······⌺⌺··│······⌺⌺··│·····⌺⌺⌺··│·····⌺⌺···│<br />
│··········│········⌺·│·······⌺⌺·│·······⌺⌺·│······⌺⌺⌺·│······⌺⌺··│······⌺·⌺·│·····⌺⌺···│·····⌺····│·····⌺·⌺··│<br />
│·······⌺⌺⌺│·······⌺⌺·│·······⌺·⌺│······⌺⌺··│······⌺···│······⌺·⌺·│······⌺···│·······⌺··│······⌺···│··········│<br />
│·······⌺··│·······⌺·⌺│·······⌺··│········⌺·│·······⌺··│··········│··········│··········│··········│··········│<br />
│········⌺·│··········│··········│··········│··········│··········│··········│··········│··········│··········│<br />
└──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┘<br />
</source><br />
<br />
== How the APL code works ==<br />
If you're completely new to APL, this explanation is probably not the place to start, rather check out our [[Tutorials#Introductions|introductions]]. However, you should be able to get some idea of what's happening even if the details are not yet clear. Just bear in mind that APL evaluates expressions from right to left unless you use parentheses.<br />
<br />
To compute a new generation in the Game of Life we first need to find out how many neighbours each live cell has. To keep things simple let's consider the following 5-by-5 grid:<br />
<br />
<source lang=apl><br />
currentGeneration←5 5⍴0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 1 0 0<br />
currentGeneration<br />
0 0 0 0 0<br />
0 0 1 1 0<br />
0 1 1 0 0<br />
0 0 1 0 0<br />
0 0 0 0 0<br />
</source><br />
=== Rotation ===<br />
If we rotate each row of the grid one place left by using APL's [[Rotate]] function <source lang=apl inline>⌽</source>, each element is replaced by its right-hand neighbour. In other words, the new grid will have a 1 for all cells whose right-hand neighbour was 1:<br />
<br />
<source lang=apl><br />
1⌽currentGeneration<br />
0 0 0 0 0<br />
0 1 1 0 0<br />
1 1 0 0 0<br />
0 1 0 0 0<br />
0 0 0 0 0<br />
</source><br />
<br />
Similarly we can rotate right to find the left-hand neighbour by using <source lang=apl inline>¯1⌽currentGeneration</source>. By using APL's [[Outer Product]] operator <source lang=apl inline>∘.</source> together with the Rotate function we can get a set of three grids containing the left-hand neighbour, the original grid, and the right-hand neighbour:<br />
<br />
<source lang=apl><br />
¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┬─────────┬─────────┐<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
│0 0 0 1 1│0 0 1 1 0│0 1 1 0 0│<br />
│0 0 1 1 0│0 1 1 0 0│1 1 0 0 0│<br />
│0 0 0 1 0│0 0 1 0 0│0 1 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
└─────────┴─────────┴─────────┘<br />
</source><br />
=== Extending to two dimensions ===<br />
Now we can use a similar trick but rotate up and down by using APL's First Axis Rotate function <source lang=apl inline>⊖</source>. Used with an Outer Product this gives us a series of 9 grids:<br />
<br />
<source lang=apl><br />
¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┬─────────┬─────────┐<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
│0 0 0 1 1│0 0 1 1 0│0 1 1 0 0│<br />
│0 0 1 1 0│0 1 1 0 0│1 1 0 0 0│<br />
│0 0 0 1 0│0 0 1 0 0│0 1 0 0 0│<br />
├─────────┼─────────┼─────────┤<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
│0 0 0 1 1│0 0 1 1 0│0 1 1 0 0│<br />
│0 0 1 1 0│0 1 1 0 0│1 1 0 0 0│<br />
│0 0 0 1 0│0 0 1 0 0│0 1 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
├─────────┼─────────┼─────────┤<br />
│0 0 0 1 1│0 0 1 1 0│0 1 1 0 0│<br />
│0 0 1 1 0│0 1 1 0 0│1 1 0 0 0│<br />
│0 0 0 1 0│0 0 1 0 0│0 1 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│0 0 0 0 0│<br />
└─────────┴─────────┴─────────┘<br />
</source><br />
<br />
The grid in the centre is our original grid, and the others give the neighbours above, below, left, right and diagonally.<br />
=== Summing it up ===<br />
Now we can find out how many neighbours each cell in the original grid has by summing the corresponding elements in each of the 9 grids. For a live cell this will give us 1 more than the number of neighbours - e.g. a 4 in the result means a cell has three neighbours. For a dead cell the result is just the number of neighbours:<br />
<br />
<source lang=apl><br />
+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 1 2 2 1│<br />
│1 3 4 3 1│<br />
│1 4 5 4 1│<br />
│1 3 3 2 0│<br />
│0 1 1 1 0│<br />
└─────────┘<br />
</source><br />
<br />
For a cell to exist in the next generation, one of two rules must be met:<br />
<br />
* Any live cell with two neighbours lives, unchanged, to the next generation<br />
* Any cell, live or dead, with exactly three neighbours is live in the next generation<br />
<br />
This means that all cells which might be live in the next generation will have a sum of 3 (from the first rule) or 3 or 4 (from the second rule).<br />
<br />
So we're interested in cells with a sum of 3 or 4, which we can find as follows:<br />
<br />
<source lang=apl><br />
3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┬─────────┐<br />
│0 0 0 0 0│0 0 0 0 0│<br />
│0 1 0 1 0│0 0 1 0 0│<br />
│0 0 0 0 0│0 1 0 1 0│<br />
│0 1 1 0 0│0 0 0 0 0│<br />
│0 0 0 0 0│0 0 0 0 0│<br />
└─────────┴─────────┘<br />
</source><br />
<br />
The next step is best understood by first considering the two scores 4 and 3 separately<br />
<br />
(a) A score of 4 corresponds either to a live cell with three neighbours or a dead cell with four neighbours. Only in the former case do we get a cell in the next generation - i.e. we get a cell if the score is 4 ''and'' the cell is currently live. Here we're using APL's [[And]] function, <source lang=apl inline>∧</source><br />
<source lang=apl><br />
(⊂currentGeneration)∧4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 0 0 0 0│<br />
│0 0 1 0 0│<br />
│0 1 0 0 0│<br />
│0 0 0 0 0│<br />
│0 0 0 0 0│<br />
└─────────┘<br />
</source><br />
<br />
(b) A score of three corresponds either to a live cell with two neighbours or a dead cell with three neighbours. In either case we get a cell in the next generation:<br />
<source lang=apl><br />
3=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 0 0 0 0│<br />
│0 1 0 1 0│<br />
│0 0 0 0 0│<br />
│0 1 1 0 0│<br />
│0 0 0 0 0│<br />
└─────────┘<br />
</source><br />
<br />
We can write this slightly redundantly by ''and''-ing with 1 (meaning "''and'' the cell is alive or dead") for reasons which will become apparent shortly:<br />
<source lang=apl><br />
1∧3=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 0 0 0 0│<br />
│0 1 0 1 0│<br />
│0 0 0 0 0│<br />
│0 1 1 0 0│<br />
│0 0 0 0 0│<br />
└─────────┘<br />
</source><br />
<br />
So the next step is to say that a cell is live if either test (a) ''or'' test (b) is met. We need to use APL's [[Or]] function, <source lang=apl inline>∨</source>. We could write this rather long-windedly as:<br />
<source lang=apl><br />
(1∧3=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration)∨(⊂currentGeneration)∧4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 0 0 0 0│<br />
│0 1 1 1 0│<br />
│0 1 0 0 0│<br />
│0 1 1 0 0│<br />
│0 0 0 0 0│<br />
└─────────┘<br />
</source><br />
=== Refactoring ===<br />
But we can take advantage of APL's [[Inner Product]] operator to do the ''and'' operations of each test and then ''or'' the result - the <source lang=apl inline>∨.∧</source> in the following:<br />
<br />
<source lang=apl><br />
1 currentGeneration∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂currentGeneration<br />
┌─────────┐<br />
│0 0 0 0 0│<br />
│0 1 1 1 0│<br />
│0 1 0 0 0│<br />
│0 1 1 0 0│<br />
│0 0 0 0 0│<br />
└─────────┘<br />
</source><br />
<br />
The final expression ends with a bit of housekeeping using <source lang=apl inline>↑</source> to turn the result back into a simple array (it's currently nested).<br />
<br />
== External links ==<br />
* [[wikipedia:Conways_Game_of_Life|Wikipedia]]<br />
* [http://youtube.com/watch?v=a9xAKttWgP4 John Scholes' famous video] on YouTube<br />
* [http://dfns.dyalog.com/n_life.htm John Scholes' notes], as part of the [[dfns workspace]], includes a more in-depth treatment</div>Miraheze>Adám Brudzewsky