⍝ John Conway's "Game of Life":

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ manifold operators:

    plane←{                             ⍝ ┌───┐
        1 0↓0 1↓⍺⍺ 0,0⍪⍵                ⍝ │   │
    }                                   ⍝ └───┘

    cylinder←{                          ⍝ ┌───┐
        1 0↓⍺⍺ 0⍪⍵                      ⍝ ⍋   ⍋
    }                                   ⍝ └───┘

    torus←{                             ⍝ ┌─→─┐
        ⍺⍺ ⍵                            ⍝ ⍋   ⍋
    }                                   ⍝ └─→─┘

    möbius←{                            ⍝ ┌───┐ möbius strip
        1 0↓0 2↓¯1⌽⍺⍺ 0⍪twist ⍵         ⍝ ⍋   ⍒
    }                                   ⍝ └───┘

    klein←{⎕ml←0                        ⍝ ┌─→─┐ klein bottle
        0 2↓¯1⌽⍺⍺ twist ⍵               ⍝ ⍋   ⍒
    }                                   ⍝ └─→─┘

⍝ where:
                                        ⍝ append reversed columns.
    twist←{                             ⍝  ┌───┐    ┌ ┌───┐ ┐
        ¯1⌽⍵,⌽⊖2↑[1+⎕io]¯1⌽⍵            ⍝  ⍋   ↑ →  ↓,⍋   ↑,⍒
    }                                   ⍝  └───┘    └ └───┘ ┘

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝

    glider ← (3 3⍴⍳9)∊3 4 6 8 9                 ⍝ initial population.

    try ← {0 disp{'·⍟'[⎕io+⍵]}¨⍺⍺ traj ⍵}

    life plane try 5 5↑glider                   ⍝ plane
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│·····│·····│
│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│·····│·····│·····│
│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│·····│·····│
│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│····⍟│····⍟│···⍟⍟│
│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│···⍟⍟│···⍟⍟│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

    life cylinder try 5 5↑glider                ⍝ cylinder
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│·····│·····│·····│·····│
│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│·····│·····│·····│·····│·····│
│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│····⍟│·····│·····│·····│
│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│⍟···⍟│⍟····│⍟··⍟·│⍟····│⍟···⍟│
│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│⍟··⍟⍟│⍟···⍟│⍟···⍟│⍟···⍟│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

    life torus try 5 5↑glider                   ⍝ torus
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│·····│····⍟│⍟···⍟│⍟···⍟│⍟⍟··⍟│⍟⍟···│·⍟··⍟│·⍟⍟··│··⍟··│⍟·⍟··│
│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│·····│·····│·····│·····│·····│·····│⍟····│⍟⍟···│⍟⍟···│⍟⍟⍟··│·⍟⍟··│
│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│····⍟│·····│·····│·····│·····│·····│·····│·····│·····│·⍟···│
│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│⍟···⍟│⍟····│⍟··⍟·│⍟····│····⍟│⍟····│·····│·····│·····│·····│·····│
│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│⍟··⍟⍟│⍟···⍟│⍟··⍟·│⍟⍟···│·⍟···│·⍟··⍟│·⍟···│⍟····│·⍟···│·····│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

    life möbius try 5 5↑glider                  ⍝ möbius
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│⍟····│⍟····│⍟····│⍟····│
│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│⍟····│⍟····│⍟····│⍟····│⍟····│
│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│····⍟│·····│·····│·····│
│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│····⍟│·····│···⍟·│·····│····⍟│
│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│···⍟⍟│····⍟│····⍟│····⍟│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

    life klein try 5 5↑glider                   ⍝ klein
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│⍟····│⍟···⍟│⍟···⍟│⍟⍟··⍟│·⍟··⍟│·⍟···│·⍟··⍟│⍟····│·⍟···│·····│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│···⍟⍟│····⍟│⍟··⍟·│⍟····│⍟⍟···│⍟⍟··⍟│·⍟···│·⍟⍟··│··⍟··│⍟·⍟··│
│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│⍟····│⍟····│⍟····│⍟····│·····│⍟····│·····│·····│·····│·····│·····│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│····⍟│·····│···⍟·│·····│····⍟│·····│⍟····│⍟⍟···│⍟⍟···│⍟⍟⍟··│·⍟⍟··│
│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│····⍟│·····│·····│·····│·····│·····│·····│·····│·····│·⍟···│·⍟⍟··│·⍟⍟··│·⍟⍟⍟·│··⍟⍟·│·⍟·⍟·│···⍟⍟│····⍟│··⍟·⍟│····⍟│···⍟·│····⍟│·····│·····│·····│·····│·····│·····│·····│·····│·⍟···│
│·····│·····│·····│··⍟··│··⍟⍟·│··⍟⍟·│··⍟⍟⍟│···⍟⍟│··⍟·⍟│····⍟│·····│···⍟·│·····│····⍟│·····│⍟····│⍟⍟···│⍟⍟···│⍟⍟⍟··│·⍟⍟··│⍟·⍟··│··⍟⍟·│···⍟·│·⍟·⍟·│···⍟·│··⍟··│···⍟·│·····│·····│⍟····│⍟····│⍟····│⍟····│·····│⍟····│·····│·····│·····│·····│·····│
│·····│·····│·····│·····│·····│·····│·····│···⍟·│···⍟⍟│···⍟⍟│···⍟⍟│····⍟│⍟··⍟·│⍟····│⍟⍟···│⍟⍟··⍟│·⍟···│·⍟⍟··│··⍟··│⍟·⍟··│··⍟··│·⍟···│··⍟··│·····│·····│·····│·····│·····│·····│·····│⍟····│⍟···⍟│⍟···⍟│⍟⍟··⍟│·⍟··⍟│·⍟···│·⍟··⍟│⍟····│·⍟···│·····│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

Back to: code

Back to: Dyalog APL

Trouble seeing APL font?