# Conway's Game of Life

Conway's Game of Life
You are encouraged to solve this task according to the task description, using any language you may know.

The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton.

Conway's game of life is described   here:

A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells.

We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table:

```   C   N                 new C
1   0,1             ->  0  # Lonely
1   4,5,6,7,8       ->  0  # Overcrowded
1   2,3             ->  1  # Lives
0   3               ->  1  # It takes three to give birth!
0   0,1,2,4,5,6,7,8 ->  0  # Barren
```

Assume cells beyond the boundary are always dead.

The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.

References

## 6502 Assembly

Works with: [6502asm.com] version 1.2
`randfill:   stx \$01          ;\$200 for indirect            ldx #\$02         ;addressing            stx \$02randloop:   lda \$fe          ;generate random            and #\$01         ;pixels on the            sta (\$01),Y      ;screen            jsr inc0103            cmp #\$00            bne randloop            lda \$02            cmp #\$06            bne randloop  clearmem:   lda #\$df         ;set \$07df-\$0a20            sta \$01          ;to \$#00            lda #\$07            sta \$02clearbyte:  lda #\$00            sta (\$01),Y            jsr inc0103            cmp #\$20            bne clearbyte            lda \$02            cmp #\$0a            bne clearbyte  starttick:copyscreen: lda #\$00         ;set up source            sta \$01          ;pointer at            sta \$03          ;\$01/\$02 and            lda #\$02         ;dest pointer            sta \$02          ;at \$03/\$04            lda #\$08            sta \$04            ldy #\$00copybyte:   lda (\$01),Y      ;copy pixel to            sta (\$03),Y      ;back buffer            jsr inc0103      ;increment pointers            cmp #\$00         ;check to see            bne copybyte     ;if we're at \$600            lda \$02          ;if so, we've            cmp #\$06         ;copied the            bne copybyte     ;entire screen  conway:     lda #\$df         ;apply conway rules            sta \$01          ;reset the pointer            sta \$03          ;to \$#01df/\$#07df            lda #\$01         ;(\$200 - \$21)            sta \$02          ;(\$800 - \$21)            lda #\$07            sta \$04onecell:    lda #\$00         ;process one cell            ldy #\$01         ;upper cell            clc            adc (\$03),Y            ldy #\$41         ;lower cell            clc            adc (\$03),Ychkleft:    tax              ;check to see            lda \$01          ;if we're at the            and #\$1f         ;left edge            tay            txa            cpy #\$1f            beq rightcellsleftcells:  ldy #\$00         ;upper-left cell            clc            adc (\$03),Y            ldy #\$20         ;left cell            clc            adc (\$03),Y            ldy #\$40         ;lower-left cell            clc            adc (\$03),Ychkright:   tax              ;check to see            lda \$01          ;if we're at the            and #\$1f         ;right edge            tay            txa            cpy #\$1e            beq evaluaterightcells: ldy #\$02         ;upper-right cell            clc            adc (\$03),Y            ldy #\$22         ;right cell            clc            adc (\$03),Y            ldy #\$42         ;lower-right cell            clc            adc (\$03),Yevaluate:   ldx #\$01         ;evaluate total            ldy #\$21         ;for current cell            cmp #\$03         ;3 = alive            beq storex            ldx #\$00            cmp #\$02         ;2 = alive if            bne storex       ;c = alive            lda (\$03),Y            and #\$01            taxstorex:     txa              ;store to screen            sta (\$01),Y            jsr inc0103      ;move to next cellconwayloop: cmp #\$e0         ;if not last cell,            bne onecell      ;process next cell            lda \$02            cmp #\$05            bne onecell            jmp starttick    ;run next tick  inc0103:    lda \$01          ;increment \$01            cmp #\$ff         ;and \$03 as 16-bit            bne onlyinc01    ;pointers            inc \$02            inc \$04onlyinc01:  inc \$01            lda \$01            sta \$03            rts`

## ACL2

`(defun print-row (row)   (if (endp row)       nil       (prog2\$ (if (first row)                   (cw "[]")                   (cw "  "))               (print-row (rest row))))) (defun print-grid-r (grid)   (if (endp grid)       nil       (progn\$ (cw "|")               (print-row (first grid))               (cw "|~%")               (print-grid-r (rest grid))))) (defun print-line (l)   (if (zp l)       nil       (prog2\$ (cw "-")               (print-line (1- l))))) (defun print-grid (grid)   (progn\$ (cw "+")           (print-line (* 2 (len (first grid))))           (cw "+~%")           (print-grid-r grid)           (cw "+")           (print-line (* 2 (len (first grid))))           (cw "+~%"))) (defun neighbors-row-r (row)   (if (endp (rest (rest row)))       (list (if (first row) 1 0))       (cons (+ (if (first row) 1 0)                (if (third row) 1 0))             (neighbors-row-r (rest row))))) (defun neighbors-row (row)   (cons (if (second row) 1 0)         (neighbors-row-r row))) (defun zip+ (xs ys)   (if (or (endp xs) (endp ys))       (append xs ys)       (cons (+ (first xs) (first ys))             (zip+ (rest xs) (rest ys))))) (defun counts-row (row)   (if (endp row)       nil       (cons (if (first row) 1 0)             (counts-row (rest row))))) (defun neighbors-r (grid prev-counts curr-counts next-counts                         prev-neighbors curr-neighbors                         next-neighbors)   (if (endp (rest grid))       (list (zip+ (zip+ prev-counts                         prev-neighbors)                   (neighbors-row (first grid))))       (cons (zip+ (zip+ (zip+ prev-counts next-counts)                         (zip+ prev-neighbors next-neighbors))                   curr-neighbors)             (neighbors-r (rest grid)                          curr-counts                          next-counts                          (counts-row (third grid))                          curr-neighbors                          next-neighbors                          (neighbors-row (third grid)))))) (defun neighbors (grid)   (neighbors-r grid                nil                (counts-row (first grid))                (counts-row (second grid))                nil                (neighbors-row (first grid))                (neighbors-row (second grid)))) (defun life-rules-row (life neighbors)   (if (or (endp life) (endp neighbors))       nil       (cons (or (and (first life)                      (or (= (first neighbors) 2)                          (= (first neighbors) 3)))                 (and (not (first life))                      (= (first neighbors) 3)))             (life-rules-row (rest life) (rest neighbors))))) (defun life-rules-r (grid neighbors)   (if (or (endp grid) (endp neighbors))       nil       (cons (life-rules-row (first grid) (first neighbors))             (life-rules-r (rest grid) (rest neighbors))))) (defun conway-step (grid)   (life-rules-r grid (neighbors grid))) (defun conway (grid steps)   (if (zp steps)       nil       (progn\$ (print-grid grid)               (conway (conway-step grid) (1- steps)))))`
Output:
```+------+
|  []  |
|  []  |
|  []  |
+------+
+------+
|      |
|[][][]|
|      |
+------+
+------+
|  []  |
|  []  |
|  []  |
+------+```

`with Ada.Text_IO;  use Ada.Text_IO; procedure Life is   type Cell is (O, X); -- Two states of a cell      -- Computation of neighborhood   function "+" (L, R : Cell) return Integer is   begin      case L is         when O =>            case R is               when O => return 0;               when X => return 1;            end case;         when X =>            case R is               when O => return 1;               when X => return 2;            end case;      end case;   end "+";   function "+" (L : Integer; R : Cell) return Integer is   begin      case R is         when O => return L;         when X => return L + 1;      end case;   end "+";      -- A colony of cells. The borders are dire and unhabited   type Petri_Dish is array (Positive range <>, Positive range <>) of Cell;    procedure Step (Culture : in out Petri_Dish) is      Above : array (Culture'Range (2)) of Cell := (others => O);      Left  : Cell;      This  : Cell;   begin      for I in Culture'First (1) + 1 .. Culture'Last (1) - 1 loop         Left := O;         for J in Culture'First (2) + 1 .. Culture'Last (2) - 1 loop            case Above        (J-1) + Above        (J) + Above        (J+1) +                 Left                                  + Culture (I,   J+1) +                 Culture (I+1, J-1) + Culture (I+1, J) + Culture (I+1, J+1) is               when 2 =>     -- Survives if alive                  This := Culture (I, J);               when 3 =>     -- Survives or else multiplies                  This := X;               when others => -- Dies                  This := O;            end case;            Above (J-1) := Left;            Left        := Culture (I, J);            Culture (I, J) := This;         end loop;         Above (Above'Last - 1) := Left;      end loop;   end Step;    procedure Put (Culture : Petri_Dish) is   begin      for I in Culture'Range loop         for J in Culture'Range loop            case Culture (I, J) is               when O => Put (' ');               when X => Put ('#');            end case;         end loop;         New_Line;      end loop;   end Put;    Blinker : Petri_Dish := (2..4 =>(O,O,X,O,O), 1|5 =>(O,O,O,O,O));   Glider  : Petri_Dish :=             (  (O,O,O,O,O,O,O,O,O,O,O),                (O,O,X,O,O,O,O,O,O,O,O),                (O,O,O,X,O,O,O,O,O,O,O),                (O,X,X,X,O,O,O,O,O,O,O),                (O,O,O,O,O,O,O,O,O,O,O),                (O,O,O,O,O,O,O,O,O,O,O)             );begin   for Generation in 1..3 loop      Put_Line ("Blinker" & Integer'Image (Generation));      Put (Blinker);      Step (Blinker);   end loop;   for Generation in 1..5 loop      Put_Line ("Glider" & Integer'Image (Generation));      Put (Glider);      Step (Glider);   end loop;end Life;`

The solution uses one cell thick border around square Petri dish as uninhabited dire land. This simplifies computations of neighborhood. Sample output contains 3 generations of the blinker and 5 of the glider:

Output:
```Blinker 1

#
#
#

###

#
#
#

Glider 1

#
#
###

Glider 2

# #
##
#

Glider 3

#
# #
##

Glider 4

#
##
##

Glider 5

#
#
###

```

## APL

APL \ 1130 example (very old APL dialect via simulator)

From: APL \ 1130 Samples

The following APL \ 1130 code will need APL385 font installed to display correctly.

`       ∇LIFE[⎕]∇[0]   NG←LIFE CG;W[1]   W←CG+(¯1⊖CG)+(1⊖CG)+(¯1⌽CG)+(1⌽CG)[2]   W←W+(1⊖1⌽CG)+(¯1⊖1⌽CG)+(1⊖¯1⌽CG)+(¯1⊖¯1⌽CG)[3]   NG←(3=W)+(CG∧4=W)      ∇      RP←5 5⍴0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 1 0 0      RP0 0 0 0 00 0 1 1 00 1 1 0 00 0 1 0 00 0 0 0 0      LIFE RP0 0 0 0 00 1 1 1 00 1 0 0 00 1 1 0 00 0 0 0 0      LIFE LIFE RP0 0 1 0 00 1 1 0 01 0 0 1 00 1 1 0 00 0 0 0 0  `

## ARM Assembly

Works with: TI-Nspire
`	.string "PRG" 	lcd_ptr .req r4	active_fb .req r5	inactive_fb .req r6	offset_r .req r7	backup_fb .req r8 	@ start	push {r4-r10, r12, lr} 	ldr lcd_ptr, =0xC0000000 @ address of the LCD controller	adr offset_r, offsets	ldrh r0, [offset_r, #6] @ 0xffff is already in memory because -1 is in the offsets table	str r0, [lcd_ptr, #0x200] @ set up paletted colors: 1 is black, 0 is white 	ldr r2, [lcd_ptr, #0x18] @ load lcd configuration	bic r2, #14	orr r2, #6 @ Set color mode to 8 bpp, paletted	str r2, [lcd_ptr, #0x18] 	ldr backup_fb, [lcd_ptr, #0x10] @ Save address of OS framebuffer 	@ allocate a buffer for game state / framebuffer	ldr r0, =153600 @ 320 * 240 * 2	add r0, #8	svc #5 @ malloc	push {r0}	orr inactive_fb, r0, #7	add inactive_fb, #1	add active_fb, inactive_fb, #76800 	@ fill buffer with random ones and zeroes	ldr r10, =76800	mov r9, #01:	subs r10, r10, #1	strb r9, [active_fb, r10] @ zero other framebuffer	svc #206 @ rand syscall	and r0, r0, #1	strb r0, [inactive_fb, r10]	bne 1b 	@ set first and last rows to zero	mov r2, #320	mov r1, #0	mov r0, inactive_fb	push {r1,r2}	svc #7 @ memset	pop {r1,r2}	ldr r3, =76480	add r0, r0, r3	svc #7 	@ beginning of main loop, swap framebuffers3:	ldr r0, =76480 @ 320 * 239	str inactive_fb, [lcd_ptr, #0x10]	mov inactive_fb, active_fb	ldr active_fb, [lcd_ptr, #0x10] 	@ per-pixel loop2:	mov r1, #16 @ 8 * 2	mov r2, #0	sub r0, #1 	@ loop to count up neighboring living cells1:	subs r1, #2	ldrsh r3, [offset_r, r1] @ cant use lsl #1	add r3, r3, r0	ldrb r3, [active_fb, r3]	add r2, r2, r3	bne 1b @ at end of loop, r1 and r3 can be discarded 	@ decides whether the cell should live or die based on neighbors	ldrb r1, [active_fb, r0]	add r2, r2, r1	teq r2, #3	moveq r1, #1	teqne r2, #4	movne r1, #0	strb r1, [inactive_fb, r0]	teq r0, #320	bne 2b 	@ checks if the escape key is pressed	ldr r0, =0x900E001C	ldr r1, [r0]	tst r1, #0x80	beq 3b 	str backup_fb, [lcd_ptr, #0x10] @ restores OS framebuffer 	pop {r0}	svc #6 @ free buffer	pop {r4-r10, r12, pc}offsets:	.hword -321, -320, -319, -1, 1, 319, 320, 321 `

## AutoHotkey

ahk discussion

`rows := cols := 10                               ; set grid dimensionsi = -1,0,1, -1,1, -1,0,1                         ; neighbors' x-offsetsj = -1,-1,-1, 0,0, 1,1,1                         ; neighbors' y-offsetsStringSplit i, i, `,                             ; make arraysStringSplit j, j, `, Loop % rows {                                    ; setup grid of checkboxes   r := A_Index, y := r*17-8                     ; looks good in VISTA   Loop % cols {      c := A_Index, x := c*17-5      Gui Add, CheckBox, x%x% y%y% w17 h17 vv%c%_%r% gCheck   }}Gui Add, Button, % "x12 w" x+2, step             ; button to step to next generationGui ShowReturn Check:   GuiControlGet %A_GuiControl%                  ; manual set of cellsReturn ButtonStep:                                      ; move to next generation   Loop % rows {      r := A_Index      Loop % cols {         c := A_Index, n := 0         Loop 8                                  ; w[x,y] <- new states            x := c+i%A_Index%, y := r+j%A_Index%, n += 1=v%x%_%y%         GuiControl,,v%c%_%r%,% w%c%_%r% := v%c%_%r% ? n=2 || n=3 : n=3      }   }   Loop % rows {                                 ; update v[x,y] = states      r := A_Index      Loop % cols         v%A_Index%_%r% := w%A_Index%_%r%   }Return GuiClose:                                        ; exit when GUI is closedExitApp`

## AWK

50x20 grid (hardcoded) with empty border, filled with random cells, running for 220 generations, using ANSI escape-codes for output to terminal:

` BEGIN { c=220; d=619; i=10000;  printf("\033[2J");      # Clear screen while(i--) m[i]=0; while(d--) m[int(rand()*1000)]=1;  while(c--){  for(i=52; i<=949; i++){   d=m[i-1]+m[i+1]+m[i-51]+m[i-50]+m[i-49]+m[i+49]+m[i+50]+m[i+51];   n[i]=m[i];   if(m[i]==0 && d==3) n[i]=1;   else if(m[i]==1 && d<2) n[i]=0;        else if(m[i]==1 && d>3) n[i]=0;  }  printf("\033[1;1H");   # Home cursor  for(i=1;i<=1000;i++)   # gridsize 50x20  {   if(n[i]) printf("O"); else printf(".");   m[i]=n[i];   if(!(i%50)) printf("\n");  }  printf("%3d\n",c);     # Countdown  x=30000; while(x--) ;  # Delay }} `
Output:
Finally:
```..................................................
..........................................OO......
..........................................O.O.....
...........................................O......
......................................OOO.......OO
................................................OO
....................................O.....O.......
....................................O.....O.......
....................................O.....O.......
..................................................
......................................OOO.........
..................................................
..................................................
..................................................
..O...............................................
.O.O..............................................
O.O...........OO...........OO.....................
.O............OO.........O..O.....................
.........................OOO......................
..................................................
0
```

## Axe

 This example is in need of improvement: Improve performance and add a way to interactively seed the map.

This implementation uses the full screen buffer instead of a 3x3 grid. This naive, unoptimized version gets less than 1 FPS.

`Full While getKey(0)End ClrDraw.BLINKERPxl-On(45,45)Pxl-On(46,45)Pxl-On(47,45) .GLIDERPxl-On(1,1)Pxl-On(2,2)Pxl-On(2,3)Pxl-On(3,1)Pxl-On(3,2) Repeat getKey(0) DispGraph EVOLVE() RecallPic ClrDrawʳEndReturn Lbl EVOLVEFor(Y,0,63) For(X,0,95)  0→N  For(B,Y-1,Y+1)   For(A,X-1,X+1)    pxl-Test(A,B)?N++   End  End  pxl_Test(X,Y)?N--  If N=3??(N=2?pxl-Test(X,Y))   Pxl-On(X,Y)ʳ  Else   Pxl-Off(X,Y)ʳ  End EndEndReturn`

## BASIC

### BASIC256

"Thunderbird" methuselah evolution in the Game of Life (created with BASIC-256)

Saving to PNG files function is omited. You can find it in the Galton box animation example.

`# Conway's_Game_of_Life X = 59 : Y = 35 : H = 4 fastgraphicsgraphsize X*H,Y*H dim c(X,Y) : dim cn(X,Y) : dim cl(X,Y) `
` # Thunderbird methuselahc[X/2-1,Y/3+1] = 1 : c[X/2,Y/3+1] = 1 : c[X/2+1,Y/3+1] = 1	c[X/2,Y/3+3] = 1 : c[X/2,Y/3+4] = 1 : c[X/2,Y/3+5] = 1 s = 0do	color black	rect 0,0,graphwidth,graphheight	alive = 0 : stable = 1	s = s + 1	for y = 0 to Y-1 		for x = 0 to X-1			xm1 = (x-1+X)%X : xp1 = (x+1+X)%X			ym1 = (y-1+Y)%Y : yp1 = (y+1+Y)%Y			cn[x,y] = c[xm1,y] + c[xp1,y]			cn[x,y] = c[xm1,ym1] + c[x,ym1] + c[xp1,ym1] + cn[x,y]			cn[x,y] = c[xm1,yp1] + c[x,yp1] + c[xp1,yp1] + cn[x,y]			if c[x,y] = 1 then				if cn[x,y] < 2 or cn[x,y] > 3 then 					cn[x,y] = 0				else 					cn[x,y] = 1					alive = alive + 1				end if			else 				if cn[x,y] = 3 then 					cn[x,y] = 1					alive = alive + 1				else					cn[x,y] = 0				end if			end if			if c[x,y] then				if cn[x,y] then					if cl[x,y] then color purple		# adult					if not cl[x,y] then color green	        # newborn				else 					if cl[x,y] then color red		# old					if not cl[x,y] then color yellow	# shortlived				end if 				rect x*H,y*H,H,H			end if		next x	next y	refresh	pause 0.06	# Copy arrays	for i = 0 to X-1		for j = 0 to Y-1			if cl[i,j]<>cn[i,j] then stable = 0			cl[i,j] = c[i,j]			c[i,j] = cn[i,j]		next j	next i	until not alive or stable if not alive then	print "Died in "+s+" iterations"	color black	rect 0,0,graphwidth,graphheight	refreshelse 	print "Stabilized in "+(s-2)+" iterations"	end if`
Output:
``` Stabilized in 243 iterations
```

### BBC BASIC

`      dx% = 64      dy% = 64      DIM old&(dx%+1,dy%+1), new&(dx%+1,dy%+1)      VDU 23,22,dx%*4;dy%*4;16,16,16,0      OFF       REM Set blinker:      old&(50,50) = 1 : old&(50,51) = 1 : old&(50,52) = 1      REM Set glider:      old&(5,7) = 1 : old&(6,7) = 1 : old&(7,7) = 1 : old&(7,6) = 1 : old&(6,5) = 1       REM Draw initial grid:      FOR X% = 1 TO dx%        FOR Y% = 1 TO dy%          IF old&(X%,Y%) GCOL 11 ELSE GCOL 4          PLOT 69, X%*8-6, Y%*8-4        NEXT      NEXT X%       REM Run:      GCOL 4,0      REPEAT        FOR X% = 1 TO dx%          FOR Y% = 1 TO dy%            S% = old&(X%-1,Y%) + old&(X%,Y%-1) + old&(X%-1,Y%-1) + old&(X%+1,Y%-1) + \            \    old&(X%+1,Y%) + old&(X%,Y%+1) + old&(X%-1,Y%+1) + old&(X%+1,Y%+1)            O% = old&(X%,Y%)            N% = -(S%=3 OR (O%=1 AND S%=2))            new&(X%,Y%) = N%            IF N%<>O% PLOT X%*8-6, Y%*8-4          NEXT        NEXT X%        SWAP old&(), new&()        WAIT 30      UNTIL FALSE`
Output:

### FreeBASIC

`' FreeBASIC Conway's Game of Life' May 2015' 07-10-2016 cleanup/little changes' moved test inkey outside the ScreenLock - ScreenUnLock block' compile: fbc -s gui Const As UInteger grid  = 300  '480 by 480Const As UInteger gridy = gridConst As UInteger gridx = gridConst As UInteger pointsize = 5 'pixelsConst As UInteger steps = 10Dim As UInteger gen, n, neighbours, x, y, was Dim As String press Const As UByte red   = 4  'red is color 6Const As UByte white = 15 'colorConst As UByte black = 0  'color 'color 0 normaly is black'color 1 normaly is dark blue'color 2 normaly is greenConst As UInteger bot = 35 'this is 35 lines from the top of the pageDim As UByte old( grid + 10, grid +10), new_( grid +10, grid +10) 'Set blinker:' old( 160, 160) =1: old( 160, 170) =1 : old( 160, 180) =1 'Set blinker:' old( 160, 20) =1: old( 160, 30) =1 : old( 160, 40) =1 'Set blinker:' old( 20, 20) =1: old( 20, 30) =1 : old( 20, 40) =1 'Set glider:'  old(  50,  70) =1: old(  60,  70) =1: old(  70,  70) =1' old(  70,  60) =1: old(  60,  50) =1 ' http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life' Thunderbird methuselah'X = 59 : Y = 35 : H = 4'c[X/2-1,Y/3+1] = 1 : c[X/2,Y/3+1] = 1 : c[X/2+1,Y/3+1] = 1'c[X/2,Y/3+3] = 1 : c[X/2,Y/3+4] = 1 : c[X/2,Y/3+5] = 1 'xb = 59 : yb = 35' old( Xb/2-1,Yb/3+1) =1: old(Xb/2,Yb/3+1) =1: old(Xb/2+1,Yb/3+1) =1' old( Xb/2,Yb/3+3) =1: old(Xb/2,Yb/3+4) =1 :old(Xb/2,Yb/3+5) = 1'r-pentomino'  old( 150,140) =1: old( 160,140) =1'  old( 140,150) =1 :old( 150,150) =1'  old( 150,160) =1 'Die Hard  around 150 generations' old( 150,140) =1: old(160,140) =1 : old(160,150) =1' old( 200,150) =1: old(210,150) =1 : old(210,130) = 1 : old(220,150) = 1 'Acorn  around 450 generations' it looks like this:'   0X'   000X'   XX00XXXold( 180,200) =1old( 200,210) =1old( 170,220) =1 : old( 180,220) =1 : old( 210,220) =1 : old( 220,220) =1 : old( 230,220) =1 Screen 20 'Resolution 800x600 with at least 256 colors Color whiteLine (10, 10) - (gridx + 10, gridy + 10),,B  'box from top left to bottom right Locate bot, 1  'Use a standard place on the bottom of the pageColor whitePrint " Welcome to Conway's Game of Life"Print " Using a constrained playing field (300x300), the Acorn seed runs"Print " for about 450 generations before it becomes stable (or stale)."Print " Enter any key to start"BeepSleep Do      ' flush the key input buffer  press = InkeyLoop Until press = ""'Print "                       " 'Draw initial gridFor x = 10 To gridX Step steps  For y = 10 To gridY Step steps    Color white 'old(x,y)    If old(x,y) = 1 Then Circle (x + pointsize, y + pointsize), pointsize,,,,, F  Next yNext x'Locate bot, 1Color whitePrint " Welcome to Conway's Game of Life"Print " Using a constrained playing field, the Acorn seed runs for      "Print " about 450 generations before it becomes stable (or stale).    "Color redPrint " Enter spacebar to continue or pause, ESC to stop"Sleep'Do      ' flush the key input buffer  press = InkeyLoop Until press = "" Do  gen = gen + 1  Locate bot+5,1  Color white  Print " Gen = "; gen  ScreenLock  For x = 10 To gridX Step steps    For y = 10 To gridY Step steps      'find number of live neighbours      neighbours = old( x - steps, y - steps) +old( x , y - steps)      neighbours = neighbours + old( x + steps, y -steps)      neighbours = neighbours + old( x - steps, y) + old( x + steps, y)      neighbours = neighbours + old( x - steps, y + steps)      neighbours = neighbours + old( x, y + steps) +old( x + steps, y + steps)      was =old( x, y)      If was =0 Then        If neighbours =3 Then N =1 Else N =0      Else        If neighbours =3  Or neighbours =2 Then N =1 Else N =0      End If      new_( x, y) = N      If n = 2 Then Color white      If n = 1 Then Color red      If n = 0 Then Color black      Circle (x + pointsize, y + pointsize), pointsize,,,,, F    Next y  Next x  Color white  Line (10, 10) - (gridx + 10, gridy + 10),,B  'box from top left to bottom right  ' Locate bot,1  '  't = timer  'do  'loop until timer > t + .2  ScreenUnlock  ' might not be slow enough  Sleep 70, 1  ' ignore key press   press = Inkey  If press = " " Then    Do      ' flush the key input buffer      press = Inkey    Loop Until press = ""    Do      ' wait until a key is pressed      press = Inkey    Loop Until press <> ""  End If  If press = Chr(27) Then Exit Do  ' mouse click on close window "X"  If press = Chr(255)+"k" Then End ' stop and close window   For x =10 To gridX Step steps    For y =10 To gridY Step steps      old( x, y) =new_( x, y)    Next y  Next x Loop ' UNTIL press = CHR(27) 'return to do loop up top until "esc" key is pressed. Color whiteLocate bot+3,1Print Space(55) 'clear instructionsLocate bot+6,1Print " Press any key to exit                            "SleepEnd`

### GFA Basic

` '' Conway's Game of Life'' 30x30 world held in an array size 32x32' world is in indices 1->30, 0 and 31 are always false, for neighbourhoods'DIM world!(32,32)DIM ns%(31,31) ! used to hold the neighbour countsclock%=1'' run the world'@setup_world@open_windowDO  @display_world  t\$=INKEY\$  EXIT IF t\$="q" ! need to hold key down to exit  @update_world  DELAY 0.5 ! delay of 0.5s needed in compiled versionLOOP@close_window'' Setup the world, with a blinker in one corner and a glider in the other'PROCEDURE setup_world  ARRAYFILL world!(),FALSE  ' blinker in lower-right  world!(25,25)=TRUE  world!(26,25)=TRUE  world!(27,25)=TRUE  ' glider in top-left  world!(2,2)=TRUE  world!(3,3)=TRUE  world!(3,4)=TRUE  world!(2,4)=TRUE  world!(1,4)=TRUERETURN'' Count the number of neighbours of the point i,j' (Assume i/j +/- 1 will not fall out of world)'FUNCTION count_neighbours(i%,j%)  LOCAL count%,l%  count%=0  FOR l%=-1 TO 1    IF world!(i%+l%,j%-1)      count%=count%+1    ENDIF    IF world!(i%+l%,j%+1)      count%=count%+1    ENDIF  NEXT l%  IF world!(i%-1,j%)    count%=count%+1  ENDIF  IF world!(i%+1,j%)    count%=count%+1  ENDIF  RETURN count%ENDFUNC'' Update the world one step'PROCEDURE update_world  LOCAL i%,j%  ' compute neighbour counts and store  FOR i%=1 TO 30    FOR j%=1 TO 30      ns%(i%,j%)[email protected]_neighbours(i%,j%)    NEXT j%  NEXT i%  ' update the world cells  FOR i%=1 TO 30    FOR j%=1 TO 30      IF world!(i%,j%)        SELECT ns%(i%,j%)        CASE 0,1          world!(i%,j%)=FALSE ! LONELY        CASE 2,3          world!(i%,j%)=TRUE ! LIVES        CASE 4,5,6,7,8          world!(i%,j%)=FALSE ! OVERCROWDED        ENDSELECT      ELSE        IF ns%(i%,j%)=3          world!(i%,j%)=TRUE ! BIRTH        ELSE          world!(i%,j%)=FALSE ! BARREN        ENDIF      ENDIF    NEXT j%  NEXT i%  ' update the clock  clock%=clock%+1RETURN'' Display the world in window'PROCEDURE display_world  LOCAL offsetx%,offsety%,i%,j%,x%,y%,scale%  @clear_window  ' show clock  VSETCOLOR 2,0,0,0  DEFTEXT 2  PRINT AT(5,1);"Clock: ";clock%  ' offset from top-left of display  offsetx%=10  offsety%=10  ' colour to display active cell  VSETCOLOR 1,15,0,0  DEFFILL 1  ' scale of display  scale%=9  ' display each cell in world  FOR i%=1 TO 30    FOR j%=1 TO 30      IF world!(i%,j%)        ' display active cell        x%=offsetx%+scale%*i%        y%=offsety%+scale%*j%        PBOX x%,y%,x%+scale%,y%+scale%      ENDIF    NEXT j%  NEXT i%RETURN'' Manage window for display'PROCEDURE open_window  OPENW 1  CLEARW 1RETURN'PROCEDURE clear_window  VSETCOLOR 0,15,15,15  DEFFILL 0  PBOX 0,0,300,300RETURN'PROCEDURE close_window  CLOSEW 1RETURN `

### Liberty BASIC

It will run slow for grids above say 25!

`       nomainwin       gridX = 20      gridY = gridX       mult      =500 /gridX      pointSize =360 /gridX       dim old( gridX +1, gridY +1), new( gridX +1, gridY +1) 'Set blinker:      old( 16, 16) =1: old( 16, 17) =1 : old( 16, 18) =1 'Set glider:      old(  5,  7) =1: old(  6,  7) =1: old(  7,  7) =1      old(  7,  6) =1: old(  6,  5) =1       WindowWidth  =570      WindowHeight =600       open "Conway's 'Game of Life'." for graphics_nsb_nf as #w       #w "trapclose [quit]"      #w "down ; size "; pointSize      #w "fill black" 'Draw initial grid      for x = 1 to gridX        for y = 1 to gridY          '#w "color "; int( old( x, y) *256); " 0 255"          if old( x, y) <>0 then #w "color red" else #w "color darkgray"          #w "set "; x *mult +20; " "; y *mult +20        next y      next x'   ______________________________________________________________________________'Run      do        for x =1 to gridX          for y =1 to gridY            'find number of live Moore neighbours            neighbours =old( x -1, y -1) +old( x, y -1) +old( x +1, y -1)+_                        old( x -1, y)                   +old( x +1, y   )+_                        old( x -1, y +1) +old( x, y +1) +old( x +1, y +1)            was =old( x, y)            if was =0 then                if neighbours =3 then N =1 else N =0            else                if neighbours =3  or neighbours =2 then N =1 else N =0Tail Recursive            end if            new( x, y) = N            '#w "color "; int( N /8 *256); " 0 255"            if N <>0 then #w "color red" else #w "color darkgray"            #w "set "; x *mult +20; " "; y *mult +20          next y        next x        scan'swap        for x =1 to gridX          for y =1 to gridY            old( x, y) =new( x, y)          next y        next x'Re-run until interrupted...      loop until FALSE'User shutdown received    [quit]    close #w    end `

### PureBasic

`EnableExplicitDefine.i x, y ,Xmax ,Ymax ,NXmax = 13 : Ymax = 20Dim     world.i(Xmax+1,Ymax+1)Dim Nextworld.i(Xmax+1,Ymax+1)   ; Glider test;------------------------------------------ world(1,1)=1 : world(1,2)=0 : world(1,3)=0 world(2,1)=0 : world(2,2)=1 : world(2,3)=1 world(3,1)=1 : world(3,2)=1 : world(3,3)=0;------------------------------------------ OpenConsole()EnableGraphicalConsole(1)ClearConsole()Print("Press any key to interrupt")Repeat  ConsoleLocate(0,2)  PrintN(LSet("", Xmax+2, "-")) ;---------- endless world ---------  For y = 1 To Ymax    world(0,y)=world(Xmax,y)    world(Xmax+1,y)=world(1,y)    Next  For x = 1 To Xmax    world(x,0)=world(x,Ymax)    world(x,Ymax+1)=world(x,1)  Next  world(0     ,0     )=world(Xmax,Ymax)  world(Xmax+1,Ymax+1)=world(1   ,1   )  world(Xmax+1,0     )=world(1   ,Ymax)  world(     0,Ymax+1)=world(Xmax,1   ) ;---------- endless world ---------  For y = 1 To Ymax    Print("|")     For x = 1 To Xmax      Print(Chr(32+world(x,y)*3))      N = world(x-1,y-1)+world(x-1,y)+world(x-1,y+1)+world(x,y-1)      N + world(x,y+1)+world(x+1,y-1)+world(x+1,y)+world(x+1,y+1)      If (world(x,y) And (N = 2 Or N = 3))Or (world(x,y)=0 And N = 3)        Nextworld(x,y)=1            Else        Nextworld(x,y)=0      EndIf    Next    PrintN("|")  Next  PrintN(LSet("", Xmax+2, "-"))  Delay(100)   ;Swap world() , Nextworld()    ;PB  <4.50  CopyArray(Nextworld(), world());PB =>4.50  Dim Nextworld.i(Xmax+1,Ymax+1) Until Inkey() <> "" PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""`

Sample output:

### Sinclair ZX81 BASIC

Requires at least 2k of RAM. Expects to find a square array of "0" and "1" characters, L\$(), giving the initial configuration. You can build this up by issuing commands in immediate mode and then run the program by entering `GOTO 1000`, but it's probably easier—assuming you can spare some RAM—to write the setup into the program using line numbers below 1000.

The graphics character in lines 1030 to 1060 can be obtained by typing `SHIFT``9` then `SHIFT``H`, and the one in line 1130 by typing `SHIFT``9` then `SPACE`.

`1000 LET M=LEN L\$(1)1010 DIM N\$(M,M)1020 FOR I=0 TO M+11030 PRINT AT I,0;"▩"1040 PRINT AT I,M+1;"▩"1050 PRINT AT 0,I;"▩"1060 PRINT AT M+1,I;"▩"1070 NEXT I1080 LET G=01090 PRINT AT 1,M+3;G1100 FOR I=1 TO M1110 FOR J=1 TO M1120 IF L\$(I,J)="0" THEN GOTO 11501130 PRINT AT I,J;"■"1140 GOTO 11601150 PRINT AT I,J;" "1160 NEXT J1170 NEXT I1180 FOR I=1 TO M1190 FOR J=1 TO M1200 LET N=01210 FOR K=I-1 TO I+11220 FOR L=J-1 TO J+11230 IF K=0 OR K>M OR L=0 OR L>M OR (K=I AND L=J) THEN GOTO 12501240 LET N=N+VAL L\$(K,L)1250 NEXT L1260 NEXT K1270 LET N\$(I,J)=L\$(I,J)1280 IF N<=1 OR N>=4 THEN LET N\$(I,J)="0"1290 IF N=3 THEN LET N\$(I,J)="1"1300 NEXT J1310 NEXT I1320 FOR I=1 TO M1330 LET L\$(I)=N\$(I)1340 NEXT I1350 LET G=G+11360 GOTO 1090`

To run the blinker, add this code:

`10 DIM L\$(3,3)20 LET L\$(1)="000"30 LET L\$(2)="111"40 LET L\$(3)="000"`

A screenshot of it running can be found here.

To try a random starting configuration on a 16x16 grid, use this:

`10 DIM L\$(16,16)20 FOR I=1 TO 1630 FOR J=1 TO 1640 LET L\$(I,J)="0"50 IF RND>=.7 THEN LET L\$(I,J)="1"60 NEXT J70 NEXT I`

A screenshot is here.

### TI-83 BASIC

This implementation is loosely based on the Processing Version. It uses the home screen and draws cells as "X"s. It is extremely slow, and limited to a bounding box of 16 by 8. In order for it to work, you need to initialize arrays [A] and [B] to be 18x10.

` PROGRAM:CONWAY:While 1:For(X,2,9,1):For(Y,2,17,1):If [A](Y,X):Then:Output(X-1,Y-1,"X"):Else:Output(X-1,Y-1," "):End:[A](Y-1,X-1)+[A](Y,X-1)+[A](Y+1,X-1)+[A](Y-1,X)+[A](Y+1,X)+[A](Y-1,X+1)+[A](Y,X+1)+[A](Y+1,X+1)→N:If ([A](Y,X) and (N=2 or N=3)) or (not([A](Y,X)) and N=3):Then:1→[B](Y,X):Else:0→[B](Y,X):End:End:End:[B]→[A]:End `

Here is an additional, very simple program to input the top corner of the GRAPH screen into the starting array. Make sure to draw on pixels in the rectangle (1,1) to (8,16).

`PROGRAM:PIC2LIFE:For(I,0,17,1):For(J,0,9,1):pxl-Test(J,I)→[A](I+1,J+1):End:End `

### TI-89 BASIC

This program draws its cells as 2x2 blocks on the graph screen. In order to avoid needing external storage for the previous generation, it uses the upper-left corner of each block to mark the next generation's state in all cells, then updates each cell to match its corner pixel.

A further improvement would be to have an option to start with the existing picture rather than clearing, and stop at a point where the picture has clean 2x2 blocks.

`Define life(pattern) = Prgm  Local x,y,nt,count,save,xl,yl,xh,yh  Define nt(y,x) = when(pxlTest(y,x), 1, 0)   {}→save  setGraph("Axes", "Off")→save[1]  setGraph("Grid", "Off")→save[2]  setGraph("Labels", "Off")→save[3]  FnOff  PlotOff  ClrDraw   If pattern = "blinker" Then    36→yl    40→yh    78→xl    82→xh    PxlOn  36,80    PxlOn  38,80    PxlOn  40,80  ElseIf pattern = "glider" Then    30→yl    40→yh    76→xl    88→xh    PxlOn  38,76    PxlOn  36,78    PxlOn  36,80    PxlOn  38,80    PxlOn  40,80  ElseIf pattern = "r" Then    38-5*2→yl    38+5*2→yh    80-5*2→xl    80+5*2→xh    PxlOn  38,78    PxlOn  36,82    PxlOn  36,80    PxlOn  38,80    PxlOn  40,80  EndIf   While getKey() = 0    © Expand upper-left corner to whole cell    For y,yl,yh,2      For x,xl,xh,2        If pxlTest(y,x) Then          PxlOn y+1,x          PxlOn y+1,x+1          PxlOn y,  x+1        Else          PxlOff y+1,x          PxlOff y+1,x+1          PxlOff y,  x+1        EndIf      EndFor    EndFor     © Compute next generation    For y,yl,yh,2      For x,xl,xh,2        nt(y-1,x-1) + nt(y-1,x) + nt(y-1,x+2) + nt(y,x-1) + nt(y+1,x+2) + nt(y+2,x-1) + nt(y+2,x+1) + nt(y+2,x+2) → count        If count = 3 Then          PxlOn y,x        ElseIf count ≠ 2 Then          PxlOff y,x        EndIf      EndFor    EndFor  EndWhile   © Restore changed options  setGraph("Axes", save[1])  setGraph("Grid", save[2])  setGraph("Labels", save[3])EndPrgm`

## Batch File

This code takes three parameters: `m chance iterations` Where,
m - The length and width of the array of cells
chance - The percent chance of any cell within the set array initially being alive. Full numbers only.
iterations - The amount of iterations of evolution the array goes through to display.

If no parameters are parsed, it defaults to 5 iterations of the blinking example.

` @echo offsetlocal enabledelayedexpansion if "%1"=="" (  call:_blinkerArray) else (  call:_randomArray %*) for /l %%i in (1,1,%iterations%) do (  call:_setStatus  call:_display   for /l %%m in (1,1,%m%) do (    for /l %%n in (1,1,%m%) do (      call:_evolution %%m %%n    )  )) :_blinkerArrayfor /l %%m in (0,1,4) do (  for /l %%n in (0,1,4) do (    set cell[%%m][%%n]=0  ))set cell[2][1]=1set cell[2][2]=1set cell[2][3]=1set iterations=5set m=3set cellsaddone=4 exit /b :_randomArrayset cellsaddone=%1+1for /l %%m in (0,1,%cellsaddone%) do for /l %%n in (0,1,%cellsaddone%) do set cell[%%m][%%n]=0for /l %%m in (1,1,%1) do (  for /l %%n in (1,1,%1) do (    set /a cellrandom=!random! %% 101    set cell[%%m][%%n]=0    if !cellrandom! leq %2 set cell[%%m][%%n]=1  ))set iterations=%3set m=%1 exit /b :_setStatusfor /l %%m in (0,1,%cellsaddone%) do (  for /l %%n in (0,1,%cellsaddone%) do (    if !cell[%%m][%%n]!==1 set cellstatus[%%m][%%n]=alive    if !cell[%%m][%%n]!==0 set cellstatus[%%m][%%n]=dead  ))exit /b  :_evolutionset /a lowerm=%1-1set /a upperm=%1+1set /a lowern=%2-1set /a uppern=%2+1set numm=%1set numn=%2set sum=0for /l %%m in (%lowerm%,1,%upperm%) do (  for /l %%n in (%lowern%,1,%uppern%) do (    if %%m==%numm% (      if %%n==%numn% (        set /a sum=!sum!      ) else (        if !cellstatus[%%m][%%n]!==alive set /a sum+=1      )    ) else (      if !cellstatus[%%m][%%n]!==alive set /a sum+=1    )  ))goto:!cell[%numm%][%numn%]! exit /b :0set alive=3set death=0 1 2 4 5 6 7 8 for %%i in (%alive%) do if %sum%==%%i set cell[%numm%][%numn%]=1for %%i in (%death%) do if %sum%==%%i set cell[%numm%][%numn%]=0exit /b :1set alive=2 3set death=0 1 4 5 6 7 8for %%i in (%alive%) do if %sum%==%%i set cell[%1][%2]=1for %%i in (%death%) do if %sum%==%%i set cell[%1][%2]=0exit /b :_displayecho.for /l %%m in (1,1,%m%) do (  set m%%m=   for /l %%n in (1,1,%m%) do set m%%m=!m%%m! !cell[%%m][%%n]!  echo !m%%m!) exit /b `
Output:

```0 0 0
1 1 1
0 0 0

0 1 0
0 1 0
0 1 0

0 0 0
1 1 1
0 0 0

0 1 0
0 1 0
0 1 0

0 0 0
1 1 1
0 0 0
```
Input:
```10 35 5
```
Output:
```1 1 0 0 0 0 1 1 0 0
1 1 0 0 1 1 0 1 1 0
1 0 1 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1 0 1
1 0 0 0 1 0 0 1 0 1
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 1 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 1 1
1 1 0 0 1 0 0 1 1 0

1 1 0 0 0 1 1 1 1 0
0 0 0 1 1 1 0 0 1 0
1 0 0 1 0 1 0 1 0 0
1 1 0 1 0 0 0 1 0 0
0 0 1 1 1 0 1 0 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1 1 0
1 0 1 1 0 0 0 1 1 1
1 1 0 0 0 0 0 1 1 1

0 0 0 0 0 1 1 1 1 0
1 1 1 1 0 0 0 0 1 0
1 1 0 1 0 1 0 1 1 0
1 1 0 0 0 1 0 1 0 0
0 1 1 1 1 1 1 0 1 0
0 0 0 1 0 1 1 1 0 0
0 0 0 0 0 0 1 0 0 0
0 1 1 0 0 0 0 1 0 1
1 0 1 0 0 0 1 0 0 0
1 1 1 0 0 0 0 1 0 1

0 1 1 0 0 0 1 1 1 0
1 0 0 1 0 1 0 0 0 1
0 0 0 1 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0
1 1 0 1 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 1 0 0 1 0
0 1 1 0 0 0 1 1 0 0
1 0 0 1 0 0 1 1 0 0
1 0 1 0 0 0 0 0 0 0

0 1 1 0 0 0 1 1 1 0
0 1 0 1 1 0 0 0 0 1
0 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 0 1 1 0
0 0 1 0 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0 0
0 1 1 1 0 0 1 1 0 0
0 1 1 1 0 1 0 0 1 0
1 0 0 1 0 0 1 1 0 0
0 1 0 0 0 0 0 0 0 0
```

## Befunge

Takes as input the width and height of the universe, followed by the pattern (which is terminated by the end of file). If your interpreter can't easily redirect the input from a file, or doesn't handle end-of-file detection, you can also type in the pattern manually and mark the end of input with a ~ character.

The pattern format itself is fairly lenient in what it accepts. You can use either space or . for dead cells, and o, O, * or # for live cells. This should make it fairly easy to cut and paste a number of existing formats, including the Life 1.05 format and the Plaintext .cells format used on the LifeWiki website (comments aren't supported though, so make sure to copy just the pattern itself).

In Befunge-93, the maximum value for the width and height of the universe is 127, but there is an additional constraint of 4080 cells in total, so the largest universe would really be something like 120x34 or 68x60. Befunge-98 has no real limit on the size, although in practice a much larger universe will probably be unbearably slow.

`00p10p20p30p&>40p&>50p60p>\$#v~>:55+-vv+`1:%3:+*g04p03< >3/"P"%\56v>p\56*8*/8+:vv5\`\"~"::-*3p06!:!-+67:_^#!<*<!g06!<>1+70g*\:3/"P"%v^ ^::+*g04%<*0v`1:%3\gp08<>6*`*#v_55+-#v_p10g1+10p>^pg08g07+gp08:+8/*8*65\p07:<^ >/10g-50g^87>+1+:01p/8/v>%#74#<-!!70p 00g::1+00p:20g\-:0`*+20p10g::30g\-:0`*+^ ^2+2+g03*<*:v+g06p09:%2<.v,:*93"[2J"0<>"H["39*,,,50g0v!:-1,+55\$_:40g3*20g+2+2/\-40g%50g3^/%\ >:3-\3-90vO>"l52?[">:#,_^v/3+2:*g05g04\$_>:10p40g0^!:-1,g+4\0%2/+1+`1:%3\g+8<^: \$v10!*-g<<g+70g80gp:#v_\$^>1-:::"P"%\"P"/8+:10v  >/10g+1-50g+50g%40g*+::3/"P"^>!|>g*70g80g:p00%g04:-1<<\$_^#!:pg01%"P"\*8%8gp<<  ^3\%g04+g04-1+g00%3:%9+4:-1p06\<90p01/g04`
Input:

Here's an example of what the input could look like for the Blinker pattern in a 5x5 universe:

```5
5
OOO```

And for a more complicated example, this is the Queen bee pattern in a 50x30 universe:

```50
30
...*
..*.*
.*...*
..***
**...**```
Output:

In order to produce an animated view of the universe evolving, we use a few basic ANSI escape sequences to reset the cursor position between frames. Without ANSI support, you'll just see the individual frames scrolling past with a bit of junk inbetween. The output shown below is just an extract of the first three generations of the Blinker in a 5x5 universe.

```.....   .....   .....
.....   ..O..   .....
.OOO.   ..O..   .OOO.
.....   ..O..   .....
.....   .....   .....```

## Brainf***

A life-program written in Brainf***

With Example-Output.

## Brat

`width = 3height = 3rounds = 3 universe = [[0 1 0]            [0 1 0]            [0 1 0]] next = height.of({width.of(0)}) cell = { x, y |  true? x < width && { x >= 0 && { y >= 0 && { y < height }}}  {    universe[y][x]  }  { 0 }} neighbors = { x, y |  cell(x - 1, y - 1) +  cell(x, y - 1) +  cell(x + 1, y - 1) +  cell(x + 1, y) +  cell(x + 1, y + 1) +  cell(x, y + 1) +  cell(x - 1, y + 1) +  cell(x - 1, y)} set_next = { x, y, v |  next[y][x] = v} step = {  universe.each_with_index { row, y |    row.each_with_index { c, x |      n = neighbors(x, y)       when { n < 2 } { set_next x,y, 0 }           { n > 3 } { set_next x, y, 0 }           { n == 3 } { set_next x, y, 1 }           { true } { set_next x, y, c }    }  }   u2 = universe  universe = next  next = u2} display = {  p universe.map({ r |    r.map({ n | true? n == 0, '-', "O" }).join  }).join("\n")} rounds.times {  display  p  step}`
Output:
```-O-
-O-
-O-

---
OOO
---

-O-
-O-
-O-
```

## C

Play game of life on your console: `gcc -std=c99 -Wall game.c; ./a.out [width] [height]`

`#include <stdio.h>#include <stdlib.h>#include <unistd.h> #define for_x for (int x = 0; x < w; x++)#define for_y for (int y = 0; y < h; y++)#define for_xy for_x for_yvoid show(void *u, int w, int h){	int (*univ)[w] = u;	printf("\033[H");	for_y {		for_x printf(univ[y][x] ? "\033[07m  \033[m" : "  ");		printf("\033[E");	}	fflush(stdout);} void evolve(void *u, int w, int h){	unsigned (*univ)[w] = u;	unsigned new[h][w]; 	for_y for_x {		int n = 0;		for (int y1 = y - 1; y1 <= y + 1; y1++)			for (int x1 = x - 1; x1 <= x + 1; x1++)				if (univ[(y1 + h) % h][(x1 + w) % w])					n++; 		if (univ[y][x]) n--;		new[y][x] = (n == 3 || (n == 2 && univ[y][x]));	}	for_y for_x univ[y][x] = new[y][x];} void game(int w, int h){	unsigned univ[h][w];	for_xy univ[y][x] = rand() < RAND_MAX / 10 ? 1 : 0;	while (1) {		show(univ, w, h);		evolve(univ, w, h);		usleep(200000);	}} int main(int c, char **v){	int w = 0, h = 0;	if (c > 1) w = atoi(v[1]);	if (c > 2) h = atoi(v[2]);	if (w <= 0) w = 30;	if (h <= 0) h = 30;	game(w, h);}`

Also see Conway's Game of Life/C

## C#

` using System;using System.Text;using System.Threading; namespace ConwaysGameOfLife{    // Plays Conway's Game of Life on the console with a random initial state.    class Program    {        // The delay in milliseconds between board updates.        private const int DELAY = 50;         // The cell colors.        private const ConsoleColor DEAD_COLOR = ConsoleColor.White;        private const ConsoleColor LIVE_COLOR = ConsoleColor.Black;         // The color of the cells that are off of the board.        private const ConsoleColor EXTRA_COLOR = ConsoleColor.Gray;         private const char EMPTY_BLOCK_CHAR = ' ';        private const char FULL_BLOCK_CHAR = '\u2588';         // Holds the current state of the board.        private static bool[,] board;         // The dimensions of the board in cells.        private static int width = 32;        private static int height = 32;         // True if cell rules can loop around edges.        private static bool loopEdges = true;          static void Main(string[] args)        {            // Use initializeRandomBoard for a larger, random board.            initializeDemoBoard();             initializeConsole();             // Run the game until the Escape key is pressed.            while (!Console.KeyAvailable || Console.ReadKey(true).Key != ConsoleKey.Escape) {                Program.drawBoard();                Program.updateBoard();                 // Wait for a bit between updates.                Thread.Sleep(DELAY);            }        }         // Sets up the Console.        private static void initializeConsole()        {            Console.BackgroundColor = EXTRA_COLOR;            Console.Clear();             Console.CursorVisible = false;             // Each cell is two characters wide.            // Using an extra row on the bottom to prevent scrolling when drawing the board.            int width = Math.Max(Program.width, 8) * 2 + 1;            int height = Math.Max(Program.height, 8) + 1;            Console.SetWindowSize(width, height);            Console.SetBufferSize(width, height);             Console.BackgroundColor = DEAD_COLOR;            Console.ForegroundColor = LIVE_COLOR;        }         // Creates the initial board with a random state.        private static void initializeRandomBoard()        {            var random = new Random();             Program.board = new bool[Program.width, Program.height];            for (var y = 0; y < Program.height; y++) {                for (var x = 0; x < Program.width; x++) {                    // Equal probability of being true or false.                    Program.board[x, y] = random.Next(2) == 0;                }            }        }         // Creates a 3x3 board with a blinker.        private static void initializeDemoBoard()        {            Program.width = 3;            Program.height = 3;             Program.loopEdges = false;             Program.board = new bool[3, 3];            Program.board[1, 0] = true;            Program.board[1, 1] = true;            Program.board[1, 2] = true;        }         // Draws the board to the console.        private static void drawBoard()        {            // One Console.Write call is much faster than writing each cell individually.            var builder = new StringBuilder();             for (var y = 0; y < Program.height; y++) {                for (var x = 0; x < Program.width; x++) {                    char c = Program.board[x, y] ? FULL_BLOCK_CHAR : EMPTY_BLOCK_CHAR;                     // Each cell is two characters wide.                    builder.Append(c);                    builder.Append(c);                }                builder.Append('\n');            }             // Write the string to the console.            Console.SetCursorPosition(0, 0);            Console.Write (builder.ToString());        }         // Moves the board to the next state based on Conway's rules.        private static void updateBoard()        {            // A temp variable to hold the next state while it's being calculated.            bool[,] newBoard = new bool[Program.width, Program.height];             for (var y = 0; y < Program.height; y++) {                for (var x = 0; x < Program.width; x++) {                    var n = countLiveNeighbors(x, y);                    var c = Program.board[x, y];                     // A live cell dies unless it has exactly 2 or 3 live neighbors.                    // A dead cell remains dead unless it has exactly 3 live neighbors.                    newBoard[x, y] = c && (n == 2 || n == 3) || !c && n == 3;                }            }             // Set the board to its new state.            Program.board = newBoard;        }         // Returns the number of live neighbors around the cell at position (x,y).        private static int countLiveNeighbors(int x, int y)        {            // The number of live neighbors.            int value = 0;             // This nested loop enumerates the 9 cells in the specified cells neighborhood.            for (var j = -1; j <= 1; j++) {                // If loopEdges is set to false and y+j is off the board, continue.                if (!Program.loopEdges && y + j < 0 || y + j >= Program.height) {                    continue;                }                 // Loop around the edges if y+j is off the board.                int k = (y + j + Program.height) % Program.height;                 for (var i = -1; i <= 1; i++) {                    // If loopEdges is set to false and x+i is off the board, continue.                    if (!Program.loopEdges && x + i < 0 || x + i >= Program.width) {                        continue;                    }                     // Loop around the edges if x+i is off the board.                    int h = (x + i + Program.width) % Program.width;                     // Count the neighbor cell at (h,k) if it is alive.                    value += Program.board[h, k] ? 1 : 0;                }            }             // Subtract 1 if (x,y) is alive since we counted it as a neighbor.            return value - (Program.board[x, y] ? 1 : 0);        }    }}  `

Output:

` Frame 1:    Frame 2:    Frame 3:              ██              ██████        ██        ██████              ██                     `

## C++

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.

`#include <iostream>#define HEIGHT 4#define WIDTH 4 struct Shape {public:    char xCoord;    char yCoord;    char height;    char width;    char **figure;}; struct Glider : public Shape {    static const char GLIDER_SIZE = 3;    Glider( char x , char y );    ~Glider();}; struct Blinker : public Shape {    static const char BLINKER_HEIGHT = 3;    static const char BLINKER_WIDTH = 1;    Blinker( char x , char y );    ~Blinker();}; class GameOfLife {public:    GameOfLife( Shape sh );    void print();    void update();    char getState( char state , char xCoord , char yCoord , bool toggle);    void iterate(unsigned int iterations);private:    char world[HEIGHT][WIDTH];    char otherWorld[HEIGHT][WIDTH];    bool toggle;    Shape shape;}; GameOfLife::GameOfLife( Shape sh ) :    shape(sh) ,    toggle(true) {    for ( char i = 0; i < HEIGHT; i++ ) {        for ( char j = 0; j < WIDTH; j++ ) {            world[i][j] = '.';        }    }    for ( char i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {        for ( char j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {            if ( i < HEIGHT && j < WIDTH ) {                world[i][j] =                     shape.figure[ i - shape.yCoord ][j - shape.xCoord ];            }        }    }} void GameOfLife::print() {    if ( toggle ) {        for ( char i = 0; i < HEIGHT; i++ ) {            for ( char j = 0; j < WIDTH; j++ ) {                std::cout << world[i][j];            }            std::cout << std::endl;        }    } else {        for ( char i = 0; i < HEIGHT; i++ ) {            for ( char j = 0; j < WIDTH; j++ ) {                std::cout << otherWorld[i][j];            }            std::cout << std::endl;        }    }    for ( char i = 0; i < WIDTH; i++ ) {        std::cout << '=';    }    std::cout << std::endl;} void GameOfLife::update() {    if (toggle) {        for ( char i = 0; i < HEIGHT; i++ ) {            for ( char j = 0; j < WIDTH; j++ ) {                otherWorld[i][j] =                     GameOfLife::getState(world[i][j] , i , j , toggle);            }        }        toggle = !toggle;    } else {        for ( char i = 0; i < HEIGHT; i++ ) {            for ( char j = 0; j < WIDTH; j++ ) {                world[i][j] =                     GameOfLife::getState(otherWorld[i][j] , i , j , toggle);            }        }        toggle = !toggle;    }} char GameOfLife::getState( char state, char yCoord, char xCoord, bool toggle ) {    char neighbors = 0;    if ( toggle ) {        for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {            for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {                if ( i == yCoord && j == xCoord ) {                    continue;                }                if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {                    if ( world[i][j] == 'X' ) {                        neighbors++;                    }                }            }        }    } else {        for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {            for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {                if ( i == yCoord && j == xCoord ) {                    continue;                }                if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {                    if ( otherWorld[i][j] == 'X' ) {                        neighbors++;                    }                }            }        }    }    if (state == 'X') {        return ( neighbors > 1 && neighbors < 4 ) ? 'X' : '.';    }    else {        return ( neighbors == 3 ) ? 'X' : '.';    }} void GameOfLife::iterate( unsigned int iterations ) {    for ( int i = 0; i < iterations; i++ ) {        print();        update();    }} Glider::Glider( char x , char y ) {    xCoord = x;    yCoord = y;    height = GLIDER_SIZE;    width = GLIDER_SIZE;    figure = new char*[GLIDER_SIZE];    for ( char i = 0; i < GLIDER_SIZE; i++ ) {        figure[i] = new char[GLIDER_SIZE];    }    for ( char i = 0; i < GLIDER_SIZE; i++ ) {        for ( char j = 0; j < GLIDER_SIZE; j++ ) {            figure[i][j] = '.';        }    }    figure[0][1] = 'X';    figure[1][2] = 'X';    figure[2][0] = 'X';    figure[2][1] = 'X';    figure[2][2] = 'X';} Glider::~Glider() {    for ( char i = 0; i < GLIDER_SIZE; i++ ) {        delete[] figure[i];    }    delete[] figure;} Blinker::Blinker( char x , char y ) {    xCoord = x;    yCoord = y;    height = BLINKER_HEIGHT;    width = BLINKER_WIDTH;    figure = new char*[BLINKER_HEIGHT];    for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {        figure[i] = new char[BLINKER_WIDTH];    }    for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {        for ( char j = 0; j < BLINKER_WIDTH; j++ ) {            figure[i][j] = 'X';        }    }} Blinker::~Blinker() {    for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {        delete[] figure[i];    }    delete[] figure;} int main() {    Glider glider(0,0);    GameOfLife gol(glider);    gol.iterate(5);    Blinker blinker(1,0);    GameOfLife gol2(blinker);    gol2.iterate(4);} `
Output:
first a glider, then a blinker, over a few iterations

(reformatted for convenience).

```.X.. .... .... .... ....
..X. X.X. ..X. .X.. ..X.
XXX. .XX. X.X. ..XX ...X
.... .X.. .XX. .XX. .XXX
==== ==== ==== ==== ====

.X.. .... .X..
.X.. XXX. .X..
.X.. .... .X..
.... .... ....
==== ==== ====
```

### Alternate version

Another aproach - a pretty simple one.
This version allows you to start the automata with different set of rules. Just for the fun of it.

` #include <algorithm>#include <vector>#include <iostream>#include <string> typedef unsigned char byte;  class world {public:    world( int x, int y ) : _wid( x ), _hei( y ) {        int s = _wid * _hei * sizeof( byte );        _cells = new byte[s];        memset( _cells, 0, s );    }    ~world() {        delete [] _cells;    }    int wid() const {        return _wid;    }    int hei() const {        return _hei;    }    byte at( int x, int y ) const {        return _cells[x + y * _wid];    }    void set( int x, int y, byte c ) {        _cells[x + y * _wid] = c;    }    void swap( world* w ) {        memcpy( _cells, w->_cells, _wid * _hei * sizeof( byte ) );    }private:    int _wid, _hei;    byte* _cells;};class rule {public:    rule( world* w ) : wrd( w ) {        wid = wrd->wid();        hei = wrd->hei();        wrdT = new world( wid, hei );    }    ~rule() {        if( wrdT ) delete wrdT;    }    bool hasLivingCells() {        for( int y = 0; y < hei; y++ )            for( int x = 0; x < wid; x++ )                if( wrd->at( x, y ) ) return true;        std::cout << "*** All cells are dead!!! ***\n\n";        return false;    }    void swapWrds() {        wrd->swap( wrdT );    }    void setRuleB( std::vector<int>& birth ) {        _birth = birth;    }    void setRuleS( std::vector<int>& stay ) {        _stay = stay;    }    void applyRules() {        int n;        for( int y = 0; y < hei; y++ ) {            for( int x = 0; x < wid; x++ ) {                n = neighbours( x, y );                if( wrd->at( x, y ) ) {                    wrdT->set( x, y, inStay( n ) ? 1 : 0 );                } else {                    wrdT->set( x, y, inBirth( n ) ? 1 : 0 );                }            }        }    }private:    int neighbours( int xx, int yy ) {        int n = 0, nx, ny;        for( int y = -1; y < 2; y++ ) {            for( int x = -1; x < 2; x++ ) {                if( !x && !y ) continue;                nx = ( wid + xx + x ) % wid;                ny = ( hei + yy + y ) % hei;                n += wrd->at( nx, ny ) > 0 ? 1 : 0;            }        }        return n;    }    bool inStay( int n ) {        return( _stay.end() != find( _stay.begin(), _stay.end(), n ) );    }    bool inBirth( int n ) {        return( _birth.end() != find( _birth.begin(), _birth.end(), n ) );    }    int wid, hei;    world *wrd, *wrdT;    std::vector<int> _stay, _birth;};class cellular {public:    cellular( int w, int h ) : rl( 0 ) {        wrd = new world( w, h );    }    ~cellular() {        if( rl ) delete rl;        delete wrd;    }    void start( int r ) {        rl = new rule( wrd );        gen = 1;        std::vector<int> t;        switch( r ) {            case 1: // conway                t.push_back( 2 ); t.push_back( 3 ); rl->setRuleS( t );                t.clear(); t.push_back( 3 ); rl->setRuleB( t );                break;            case 2: // amoeba                t.push_back( 1 ); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 8 ); rl->setRuleS( t );                t.clear(); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 7 ); rl->setRuleB( t );                break;            case 3: // life34                t.push_back( 3 ); t.push_back( 4 ); rl->setRuleS( t );                rl->setRuleB( t );                break;            case 4: // maze                t.push_back( 1 ); t.push_back( 2 ); t.push_back( 3 ); t.push_back( 4 ); t.push_back( 5 ); rl->setRuleS( t );                t.clear(); t.push_back( 3 ); rl->setRuleB( t );                break;        }         /* just for test - shoud read from a file */        /* GLIDER */        wrd->set( 6, 1, 1 ); wrd->set( 7, 2, 1 );        wrd->set( 5, 3, 1 ); wrd->set( 6, 3, 1 );        wrd->set( 7, 3, 1 );        /* BLINKER */        wrd->set( 1, 3, 1 ); wrd->set( 2, 3, 1 );        wrd->set( 3, 3, 1 );        /******************************************/        generation();    }private:    void display() {        system( "cls" );        int wid = wrd->wid(),            hei = wrd->hei();        std::cout << "+" << std::string( wid, '-' ) << "+\n";        for( int y = 0; y < hei; y++ ) {            std::cout << "|";            for( int x = 0; x < wid; x++ ) {                if( wrd->at( x, y ) ) std::cout << "#";                else std::cout << ".";            }            std::cout << "|\n";        }        std::cout << "+" << std::string( wid, '-' ) << "+\n";        std::cout << "Generation: " << gen << "\n\nPress [RETURN] for the next generation...";        std::cin.get();    }    void generation() {        do {            display();            rl->applyRules();            rl->swapWrds();            gen++;        }        while ( rl->hasLivingCells() );    }    rule* rl;    world* wrd;    int gen; }; int main( int argc, char* argv[] ) {    cellular c( 20, 12 );    std::cout << "\n\t*** CELLULAR AUTOMATA ***" << "\n\n Which one you want to run?\n\n\n";    std::cout << " [1]\tConway's Life\n [2]\tAmoeba\n [3]\tLife 34\n [4]\tMaze\n\n > ";    int o;     do {        std::cin >> o;    }     while( o < 1 || o > 4 );    std::cin.ignore();    c.start( o );    return system( "pause" );} `
Output:
```
+--------------------+  +--------------------+  +--------------------+  +--------------------+
|....................|  |....................|  |....................|  |....................|
|......#.............|  |....................|  |....................|  |....................|
|.......#............|  |..#..#.#............|  |.......#............|  |..#...#.............|
|.###.###............|  |..#...##............|  |.###.#.#............|  |..#....##...........|
|....................|  |..#...#.............|  |......##............|  |..#...##............|
|....................|  |....................|  |....................|  |....................|
|....................|  |....................|  |....................|  |....................|
|....................|  |....................|  |....................|  |....................|
+--------------------+  +--------------------+  +--------------------+  +--------------------+
Generation: 1           Generation: 2           Generation: 3           Generation: 4

```

## Chapel

` config const gridHeight: int = 3;config const gridWidth: int = 3; enum State { dead = 0, alive = 1 }; 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{  var game = new ConwaysGameofLife( gridHeight, gridWidth );  game[gridHeight/2 + 1, gridWidth/2     ] = State.alive;  game[gridHeight/2 + 1, gridWidth/2 + 1 ] = State.alive;  game[gridHeight/2 + 1, gridWidth/2 + 2 ] = State.alive;  for i in 1..3 {    writeln( game.prettyPrint() );    game.step();  }} `

Output:

```-----
|   |
|###|
|   |
-----

-----
| # |
| # |
| # |
-----

-----
|   |
|###|
|   |
-----

```

## Clojure

Based on the implementation by Christophe Grand here: http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/ This implementation models the live cells as a set of coordinates.

`(defn moore-neighborhood [[x y]]  (for [dx [-1 0 1]        dy [-1 0 1]        :when (not (= [dx dy] [0 0]))]    [(+ x dx) (+ y dy)])) (defn step [set-of-cells]  (set (for [[cell count] (frequencies (mapcat moore-neighborhood set-of-cells))             :when (or (= 3 count)                       (and (= 2 count) (contains? set-of-cells cell)))]         cell))) (defn print-world   ([set-of-cells] (print-world set-of-cells 10))  ([set-of-cells world-size]     (let [r (range 0 (+ 1 world-size))]       (pprint (for [y r] (apply str (for [x r] (if (set-of-cells [x y]) \# \.)))))))) (defn run-life [world-size num-steps set-of-cells]  (loop [s num-steps          cells set-of-cells]    (print-world cells world-size)    (when (< 0 s)       (recur (- s 1) (step cells))))) (def *blinker* #{[1 2] [2 2] [3 2]})(def *glider* #{[1 0] [2 1] [0 2] [1 2] [2 2]}) `

## COBOL

`identification division.program-id. game-of-life-program.data division.working-storage section.01  grid.    05 cell-table.        10 row occurs 5 times.            15 cell pic x value space occurs 5 times.    05 next-gen-cell-table.        10 next-gen-row occurs 5 times.            15 next-gen-cell pic x occurs 5 times.01  counters.    05 generation pic 9.    05 current-row pic 9.    05 current-cell pic 9.    05 living-neighbours pic 9.    05 neighbour-row pic 9.    05 neighbour-cell pic 9.    05 check-row pic s9.    05 check-cell pic s9.procedure division.control-paragraph.    perform blinker-paragraph varying current-cell from 2 by 1    until current-cell is greater than 4.    perform show-grid-paragraph through life-paragraph    varying generation from 0 by 1    until generation is greater than 2.    stop run.blinker-paragraph.    move '#' to cell(3,current-cell).show-grid-paragraph.    display 'GENERATION ' generation ':'.    display '   +---+'.    perform show-row-paragraph varying current-row from 2 by 1    until current-row is greater than 4.    display '   +---+'.    display ''.life-paragraph.    perform update-row-paragraph varying current-row from 2 by 1    until current-row is greater than 4.    move next-gen-cell-table to cell-table.show-row-paragraph.    display '   |' with no advancing.    perform show-cell-paragraph varying current-cell from 2 by 1    until current-cell is greater than 4.    display '|'.show-cell-paragraph.    display cell(current-row,current-cell) with no advancing.update-row-paragraph.    perform update-cell-paragraph varying current-cell from 2 by 1    until current-cell is greater than 4.update-cell-paragraph.    move 0 to living-neighbours.    perform check-row-paragraph varying check-row from -1 by 1    until check-row is greater than 1.    evaluate living-neighbours,        when 2 move cell(current-row,current-cell) to next-gen-cell(current-row,current-cell),        when 3 move '#' to next-gen-cell(current-row,current-cell),        when other move space to next-gen-cell(current-row,current-cell),    end-evaluate.check-row-paragraph.    add check-row to current-row giving neighbour-row.    perform check-cell-paragraph varying check-cell from -1 by 1    until check-cell is greater than 1.check-cell-paragraph.    add check-cell to current-cell giving neighbour-cell.    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.`
Output:
```GENERATION 0:
+---+
|   |
|###|
|   |
+---+

GENERATION 1:
+---+
| # |
| # |
| # |
+---+

GENERATION 2:
+---+
|   |
|###|
|   |
+---+```

## Common Lisp

`(defun next-life (array &optional results)  (let* ((dimensions (array-dimensions array))         (results (or results (make-array dimensions :element-type 'bit))))    (destructuring-bind (rows columns) dimensions      (labels ((entry (row col)                 "Return array(row,col) for valid (row,col) else 0."                 (if (or (not (< -1 row rows))                         (not (< -1 col columns)))                   0                   (aref array row col)))               (neighbor-count (row col &aux (count 0))                 "Return the sum of the neighbors of (row,col)."                 (dolist (r (list (1- row) row (1+ row)) count)                   (dolist (c (list (1- col) col (1+ col)))                     (unless (and (eql r row) (eql c col))                       (incf count (entry r c))))))               (live-or-die? (current-state neighbor-count)                 (if (or (and (eql current-state 1)                              (<=  2 neighbor-count 3))                         (and (eql current-state 0)                              (eql neighbor-count 3)))                   1                   0)))        (dotimes (row rows results)          (dotimes (column columns)            (setf (aref results row column)                  (live-or-die? (aref array row column)                                (neighbor-count row column))))))))) (defun print-grid (grid &optional (out *standard-output*))  (destructuring-bind (rows columns) (array-dimensions grid)    (dotimes (r rows grid)      (dotimes (c columns (terpri out))        (write-char (if (zerop (aref grid r c)) #\+ #\#) out))))) (defun run-life (&optional world (iterations 10) (out *standard-output*))  (let* ((world (or world (make-array '(10 10) :element-type 'bit)))         (result (make-array (array-dimensions world) :element-type 'bit)))    (do ((i 0 (1+ i))) ((eql i iterations) world)      (terpri out) (print-grid world out)      (psetq world (next-life world result)             result world))))`
`(run-life (make-array '(3 3)                       :element-type 'bit                      :initial-contents '((0 0 0)                                           (1 1 1)                                          (0 0 0)))          3)`

produces

```+++
###
+++

+#+
+#+
+#+

+++
###
+++```

A version using a sparse list of living cells rather than an explicit board.

`(defun moore-neighborhood (cell)  (let ((r '(-1 0 1)))    (mapcan	 (lambda (delta-x)	   (loop for delta-y in r	      unless (and (= delta-x 0) (= delta-y 0))	      collect (cons (+ (car cell) delta-x) (+ (cdr cell) delta-y))))	 r))) (defun frequencies (cells)  (let ((h (make-hash-table :test #'equal)))    (loop for c in cells       if (gethash c h)         do (incf (gethash c h))       else       do (setf (gethash c h) 1))    h)) (defun life-step (cells)  (let ((f (frequencies (mapcan #'moore-neighborhood cells))))    (loop for k being the hash-keys in f       when (or 	     (= (gethash k f) 3) 	     (and (= (gethash k f) 2) (member k cells :test #'equal)))	 collect k))) (defun print-world (live-cells &optional (world-size 10))  (dotimes (y world-size)    (dotimes (x world-size)      (if (member (cons x y) live-cells :test #'equal)	  (format t "X")	  (format t ".")))    (format t "~%"))) (defun run-life (world-size steps cells)  (print-world cells world-size)  (format t "~%")  (when (< 0 steps)    (run-life world-size (- steps 1) (life-step cells)))) (defparameter *blinker* '((1 . 2) (2 . 2) (3 . 2)))(defparameter *glider* '((1 . 0) (2 . 1) (0 . 2) (1 . 2) (2 . 2)))`

## D

`import std.stdio, std.string, std.algorithm, std.array, std.conv; struct GameOfLife {  enum Cell : char { dead = ' ', alive = '#' }  Cell[][] grid, newGrid;   this(in int x, in int y) pure nothrow @safe {    grid = new typeof(grid)(y + 2, x + 2);    newGrid = new typeof(grid)(y + 2, x + 2);  }   void opIndexAssign(in string[] v, in size_t y, in size_t x)  pure /*nothrow*/ @safe /*@nogc*/ {    foreach (immutable nr, row; v)      foreach (immutable nc, state; row)        grid[y + nr][x + nc] = state.to!Cell;  }   void iteration() pure nothrow @safe @nogc {    newGrid[0][] = Cell.dead;    newGrid[\$ - 1][] = Cell.dead;    foreach (row; newGrid)      row[0] = row[\$ - 1] = Cell.dead;     foreach (immutable r; 1 .. grid.length - 1)      foreach (immutable c; 1 .. grid[0].length - 1) {        uint count = 0;        foreach (immutable i; -1 .. 2)          foreach (immutable j; -1 .. 2)            if (i != 0 || j != 0)              count += grid[r + i][c + j] == Cell.alive;        immutable a = count == 3 ||                      (count == 2 && grid[r][c] == Cell.alive);        newGrid[r][c] = a ? Cell.alive : Cell.dead;      }     grid.swap(newGrid);  }   string toString() const pure /*nothrow @safe*/ {    auto ret = "-".replicate(grid[0].length - 1) ~ "\n";    foreach (const row; grid[1 .. \$ - 1])      ret ~= "|%(%c%)|\n".format(row[1 .. \$ - 1]);    return ret ~ "-".replicate(grid[0].length - 1);  }} void main() /*@safe*/ {  immutable glider1 = ["  #", "# #", " ##"];  immutable glider2 = ["#  ", "# #", "## "];   auto uni = GameOfLife(60, 20);  uni[3,  2] = glider1;  uni[3, 15] = glider2;  uni[3, 19] = glider1;  uni[3, 32] = glider2;  uni[5, 50] = [" #  #", "#  ", "#   #", "#### "];  uni.writeln;   foreach (immutable _; 0 .. 20) {    uni.iteration;    uni.writeln;  }}`
Output, first iteration:
```-------------------------------------------------------------
|                                                            |
|                                                            |
|   #          #     #          #                            |
| # #          # # # #          # #                          |
|  ##          ##   ##          ##                 #  #      |
|                                                 #          |
|                                                 #   #      |
|                                                 ####       |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
|                                                            |
-------------------------------------------------------------```

### Faster Version

Same output.

`import std.stdio, std.string, std.algorithm, std.typetuple,       std.array, std.conv; struct GameOfLife {  enum Cell : char { dead = ' ', alive = '#' }  Cell[] grid, newGrid;  immutable size_t nCols;   this(in int nx, in int ny) pure nothrow @safe {    nCols = nx + 2;    grid = new typeof(grid)(nCols * (ny + 2));    newGrid = new typeof(grid)(grid.length);  }   void opIndexAssign(in string[] v, in size_t y, in size_t x)  pure /*nothrow*/ @safe /*@nogc*/ {    foreach (immutable nr, const row; v)      foreach (immutable nc, immutable state; row)        grid[(y + nr) * nCols + x + nc] = state.to!Cell;  }   void iteration() pure nothrow @safe @nogc {    newGrid[0 .. nCols] = Cell.dead;    newGrid[\$ - nCols .. \$] = Cell.dead;    foreach (immutable nr; 1 .. (newGrid.length / nCols) - 1) {      newGrid[nr * nCols + 0] = Cell.dead;      newGrid[nr * nCols + nCols - 1] = Cell.dead;    }     foreach (immutable nr; 1 .. (grid.length / nCols) - 1) {      size_t nr_nCols = nr * nCols;      foreach (immutable nc; 1 .. nCols - 1) {        uint count = 0;        /*static*/ foreach (immutable i; TypeTuple!(-1, 0, 1))          /*static*/ foreach (immutable j; TypeTuple!(-1, 0, 1))            static if (i != 0 || j != 0)              count += (grid[nr_nCols + i * nCols + nc + j] == Cell.alive);        immutable a = count == 3 ||                      (count == 2 && grid[nr_nCols + nc] == Cell.alive);        newGrid[nr_nCols + nc] = a ? Cell.alive : Cell.dead;      }    }     swap(grid, newGrid);  }   string toString() const pure /*nothrow @safe*/ {    string ret = "-".replicate(nCols - 1) ~ "\n";    foreach (immutable nr; 1 .. (grid.length / nCols) - 1)      ret ~= "|%(%c%)|\n".format(grid[nr * nCols + 1 .. nr * nCols + nCols - 1]);    return ret ~ "-".replicate(nCols - 1);  }} void main() {  immutable glider1 = ["  #", "# #", " ##"];  immutable glider2 = ["#  ", "# #", "## "];   auto uni = GameOfLife(60, 20);  uni[3,  2] = glider1;  uni[3, 15] = glider2;  uni[3, 19] = glider1;  uni[3, 32] = glider2;  uni[5, 50] = [" #  #", "#  ", "#   #", "#### "];  uni.writeln;   foreach (immutable _; 0 .. 20) {    uni.iteration;    uni.writeln;  }}`

## Dart

`/*** States of a cell. A cell is either [ALIVE] or [DEAD].* The state contains its [symbol] for printing.*/class State {  const State(this.symbol);   static final ALIVE = const State('#');  static final DEAD = const State(' ');   final String symbol;} /*** The "business rule" of the game. Depending on the count of neighbours,* the [cellState] changes.*/class Rule {  Rule(this.cellState);   reactToNeighbours(int neighbours) {    if (neighbours == 3) {      cellState = State.ALIVE;    } else if (neighbours != 2) {      cellState = State.DEAD;    }  }   var cellState;} /*** A coordinate on the [Grid].*/class Point {  const Point(this.x, this.y);   operator +(other) => new Point(x + other.x, y + other.y);   final int x;  final int y;} /*** List of the relative indices of the 8 cells around a cell.*/class Neighbourhood {  List<Point> points() {    return [      new Point(LEFT, UP), new Point(MIDDLE, UP), new Point(RIGHT, UP),      new Point(LEFT, SAME), new Point(RIGHT, SAME),      new Point(LEFT, DOWN), new Point(MIDDLE, DOWN), new Point(RIGHT, DOWN)    ];  }   static final LEFT = -1;  static final MIDDLE = 0;  static final RIGHT = 1;  static final UP = -1;  static final SAME = 0;  static final DOWN = 1;} /*** The grid is an endless, two-dimensional [field] of cell [State]s.*/class Grid {  Grid(this.xCount, this.yCount) {    _field = new Map();    _neighbours = new Neighbourhood().points();  }   set(point, state) {    _field[_pos(point)] = state;  }   State get(point) {    var state = _field[_pos(point)];    return state != null ? state : State.DEAD;  }   int countLiveNeighbours(point) =>    _neighbours.filter((offset) => get(point + offset) == State.ALIVE).length;   _pos(point) => '\${(point.x + xCount) % xCount}:\${(point.y + yCount) % yCount}';   print() {    var sb = new StringBuffer();    iterate((point) { sb.add(get(point).symbol); }, (x) { sb.add("\n"); });    return sb.toString();  }   iterate(eachCell, [finishedRow]) {    for (var x = 0; x < xCount; x++) {      for (var y = 0; y < yCount; y++) {         eachCell(new Point(x, y));      }      if(finishedRow != null) {        finishedRow(x);      }    }  }   final xCount, yCount;  List<Point> _neighbours;  Map<String, State> _field;} /*** The game updates the [grid] in each step using the [Rule].*/class Game {  Game(this.grid);   tick() {    var newGrid = createNewGrid();     grid.iterate((point) {      var rule = new Rule(grid.get(point));      rule.reactToNeighbours(grid.countLiveNeighbours(point));      newGrid.set(point, rule.cellState);    });     grid = newGrid;  }   createNewGrid() => new Grid(grid.xCount, grid.yCount);   printGrid() => print(grid.print());   Grid grid;} main() {  // Run the GoL with a blinker.  runBlinker();} runBlinker() {  var game = new Game(createBlinkerGrid());   for(int i = 0; i < 3; i++) {    game.printGrid();    game.tick();  }  game.printGrid();} createBlinkerGrid() {  var grid = new Grid(4, 4);  loadBlinker(grid);  return grid;} loadBlinker(grid) => blinkerPoints().forEach((point) => grid.set(point, State.ALIVE)); blinkerPoints() => [new Point(0, 1), new Point(1, 1), new Point(2, 1)];`

Test cases driving the design of this code:

`#import('<path to sdk>/lib/unittest/unittest.dart'); main() {  group('rules', () {    test('should let living but lonely cell die', () {      var rule = new Rule(State.ALIVE);      rule.reactToNeighbours(1);      expect(rule.cellState, State.DEAD);    });    test('should let proper cell live on', () {      var rule = new Rule(State.ALIVE);      rule.reactToNeighbours(2);      expect(rule.cellState, State.ALIVE);    });    test('should let dead cell with three neighbours be reborn', () {      var rule = new Rule(State.DEAD);      rule.reactToNeighbours(3);      expect(rule.cellState, State.ALIVE);    });    test('should let living cell with too many neighbours die', () {      var rule = new Rule(State.ALIVE);      rule.reactToNeighbours(4);      expect(rule.cellState, State.DEAD);    });  });   group('grid', () {    var origin = new Point(0, 0);    test('should have state', () {      var grid = new Grid(1, 1);      expect(grid.get(origin), State.DEAD);      grid.set(origin, State.ALIVE);      expect(grid.get(origin), State.ALIVE);    });    test('should have dimension', () {      var grid = new Grid(2, 3);      expect(grid.get(origin), State.DEAD);      grid.set(origin, State.ALIVE);      expect(grid.get(origin), State.ALIVE);      expect(grid.get(new Point(1, 2)), State.DEAD);      grid.set(new Point(1, 2), State.ALIVE);      expect(grid.get(new Point(1, 2)), State.ALIVE);    });    test('should be endless', () {      var grid = new Grid(2, 4);      grid.set(new Point(2, 4), State.ALIVE);      expect(grid.get(origin), State.ALIVE);      grid.set(new Point(-1, -1), State.ALIVE);      expect(grid.get(new Point(1, 3)), State.ALIVE);    });    test('should print itself', () {      var grid = new Grid(1, 2);      grid.set(new Point(0, 1), State.ALIVE);      expect(grid.print(), " #\n");    });  });   group('game', () {    test('should exists', () {     var game = new Game(null);     expect(game, isNotNull);    });    test('should create a new grid when ticked', () {      var grid = new Grid(1, 1);      var game = new Game(grid);      game.tick();      expect(game.grid !== grid);    });    test('should have a grid with the same dimension after tick', (){      var game = new Game(new Grid(2, 3));      game.tick();      expect(game.grid.xCount, 2);      expect(game.grid.yCount, 3);    });    test('should apply rules to middle cell', (){      var grid = new Grid(3, 3);      grid.set(new Point(1, 1), State.ALIVE);      var game = new Game(grid);      game.tick();      expect(game.grid.get(new Point(1, 1)), State.DEAD);       grid.set(new Point(0, 0), State.ALIVE);      grid.set(new Point(1, 0), State.ALIVE);      game = new Game(grid);      game.tick();      expect(game.grid.get(new Point(1, 1)), State.ALIVE);    });    test('should apply rules to all cells', (){      var grid = new Grid(3, 3);      grid.set(new Point(0, 1), State.ALIVE);      grid.set(new Point(1, 0), State.ALIVE);      grid.set(new Point(1, 1), State.ALIVE);      var game = new Game(grid);      game.tick();      expect(game.grid.get(new Point(0, 0)), State.ALIVE);    });  });}`
Output:
``` #
#
#

###

#
#
#

###

```

## E

Just does three generations of a blinker in a dead-boundary grid, as specified. (User:Kevin Reid has graphical and wrapping versions.)

`def gridWidth := 3def gridHeight := 3def X := 0..!gridWidthdef Y := 0..!gridHeight def makeFlexList := <elib:tables.makeFlexList>def makeGrid() {  def storage := makeFlexList.fromType(<type:java.lang.Boolean>, gridWidth * gridHeight)  storage.setSize(gridWidth * gridHeight)   def grid {    to __printOn(out) {      for y in Y {        out.print("[")        for x in X {          out.print(grid[x, y].pick("#", " "))        }        out.println("]")      }    }    to get(xb :int, yb :int) {      return if (xb =~ x :X && yb =~ y :Y) {        storage[y * gridWidth + x]      } else {        false      }    }    to put(x :X, y :Y, c :boolean) {       storage[y * gridWidth + x] := c    }  }  return grid} def mooreNeighborhood := [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]def computeNextLife(prevGrid, nextGrid) {  for y in Y {    for x in X {      var neighbors := 0      for [nx, ny] ? (prevGrid[x+nx, y+ny]) in mooreNeighborhood {        neighbors += 1      }      def self := prevGrid[x, y]      nextGrid[x, y] := (self && neighbors == 2 || neighbors == 3)    }  }} var currentFrame := makeGrid()var nextFrame := makeGrid()currentFrame[1, 0] := truecurrentFrame[1, 1] := truecurrentFrame[1, 2] := true for _ in 1..3 {  def frame := nextFrame  computeNextLife(currentFrame, frame)  nextFrame := currentFrame  currentFrame := frame  println(currentFrame)}`

## eC

Library: Ecere
` import "ecere" define seed = 12345;define popInit = 1000;define width = 100;define height = 100;define cellWidth = 4;define cellHeight = 4; Array<byte> grid { size = width * height };Array<byte> newState { size = width * height }; class GameOfLife : Window{   caption = \$"Conway's Game of Life";   background = lightBlue;   borderStyle = sizable;   hasMaximize = true;   hasMinimize = true;   hasClose = true;   clientSize = { width * cellWidth, height * cellHeight };    Timer tickTimer   {      delay = 0.05, started = true, userData = this;       bool DelayExpired()      {         int y, x, ix = 0;         for(y = 0; y < height; y++)         {            for(x = 0; x < width; x++, ix++)            {               int nCount = 0;               byte alive;               if(x > 0       && y > 0        && grid[ix - width - 1]) nCount++;               if(               y > 0        && grid[ix - width    ]) nCount++;               if(x < width-1 && y > 0        && grid[ix - width + 1]) nCount++;               if(x > 0                       && grid[ix         - 1]) nCount++;               if(x < width - 1               && grid[ix         + 1]) nCount++;               if(x > 0       && y < height-1 && grid[ix + width - 1]) nCount++;               if(               y < height-1 && grid[ix + width    ]) nCount++;               if(x < width-1 && y < height-1 && grid[ix + width + 1]) nCount++;                if(grid[ix])                  alive = nCount >= 2 && nCount <= 3; // Death               else                  alive = nCount == 3; // Birth               newState[ix] = alive;            }         }         memcpy(grid.array, newState.array, width * height);         Update(null);         return true;      }   };    void OnRedraw(Surface surface)   {      int x, y;      int ix = 0;       surface.background = navy;      for(y = 0; y < height; y++)      {         for(x = 0; x < width; x++, ix++)         {            if(grid[ix])            {               int sy = y * cellHeight;               int sx = x * cellWidth;               surface.Area(sx, sy, sx + cellWidth-1, sy + cellHeight-1);            }         }      }   }    bool OnCreate()   {      int i;       RandomSeed(seed);       for(i = 0; i < popInit; i++)      {         int x = GetRandom(0, width-1);         int y = GetRandom(0, height-1);          grid[y * width + x] = 1;      }      return true;   }} GameOfLife life {}; `

## Egel

`import "prelude.eg"import "io.ego" using Systemusing Listusing IO def boardsize = 5 def empty = [ X Y -> 0 ] def insert =    [ X Y BOARD ->         [ X0 Y0 -> if and (X0 == X) (Y0 == Y) then 1                    else BOARD X0 Y0 ] ] def coords =    let R = fromto 0 (boardsize - 1) in        [ XX YY -> map (\X -> map (\Y -> X Y) YY) XX ] R R def printcell =    [ 0 -> print ". "    | _ -> print "* " ] def printboard =    [ BOARD ->        let M  = map [XX -> let _ = map [(X Y) -> printcell (BOARD X Y)] XX in print "\n" ] coords in            nop ] def count =    [ BOARD, X, Y ->        (BOARD (X - 1) (Y - 1)) + (BOARD (X) (Y - 1)) + (BOARD (X+1) (Y - 1)) +        (BOARD (X - 1) Y) + (BOARD (X+1) Y) +        (BOARD (X - 1) (Y+1)) + (BOARD (X) (Y+1)) + (BOARD (X+1) (Y+1)) ] def next =    [ 0 N -> if N == 3 then 1 else 0    | _ N -> if or (N == 2) (N == 3) then 1 else 0 ] def updateboard =    [ BOARD ->        let XX = map (\(X Y) -> X Y (BOARD X Y) (count BOARD X Y)) (flatten coords) in        let YY = map (\(X Y C N) -> X Y (next C N)) XX in            foldr [(X Y 0) BOARD -> BOARD | (X Y _) BOARD -> insert X Y BOARD ] empty YY ] def blinker =    (insert 1 2) @ (insert 2 2) @ (insert 3 2) def main =     let GEN0 = blinker empty in    let GEN1 = updateboard GEN0 in    let GEN2 = updateboard GEN1 in    let _ = map [ G -> let _ = print "generation:\n" in printboard G ] {GEN0, GEN1, GEN2} in        nop `

## Elena

ELENA 3.4, using cellular library

`import extensions.import cellular.import system'threading. const int maxX = 48.const int maxY = 28.const int DELAY = 50. sealed class Model{    T<Space>   theSpace.    T<RuleSet> theRuleSet.    bool    started.     Func<Space, object> event onUpdate :: theListener.     constructor newRandomset(RuleSet transformSet)    [        theSpace := IntMatrixSpace new(maxY, maxX, randomSet).        theRuleSet := transformSet.                started := false.    ]     private onUpdate    [        if (nil != theListener)            [ theListener(theSpace) ].    ]     run    [        if (started)            [ theSpace update(theRuleSet) ];            [ started := true. ].                 self onUpdate.    ]} singleton gameOfLifeRuleSet :: RuleSet{    proceed(Space s, int x, int y, ref<int> retVal)    [        int cell := s getAt(x, y).        int number := s getLiveCell(x, y, 1). // NOTE : number of living cells around the self includes the cell itself                if ((cell == 0) && (number == 3))        [             retVal value := 1         ];        if ((cell == 1) && ((number == 4) || (number == 3)))        [            retVal value := 1         ];        [            retVal value := 0        ]    ]} public extension<Space> presenterOp{    print    [        console setCursorPosition(0, 0).                int columns := self columns.        int rows := self rows.                int i := 0.        int j := 0.        while (i < rows)        [            j := 0.                        while (j < columns)            [                int cell := self getAt(i, j).                 console write((cell == 0)iif(" ","o")).                 j := j + 1            ].                        i := i + 1.            console writeLine        ].    ]} program[    auto model := Model newRandomset(gameOfLifeRuleSet).    model onUpdate := (:sp)<Space>[ sp print ].     console clear.        until (console isKeyAvailable)    [        model run.                threadControl sleep:DELAY    ].    console readChar]`

## Elixir

Works with: Elixir version 1.2
Translation of: Ruby
`defmodule Conway do  def game_of_life(name, size, generations, initial_life\\nil) do    board = seed(size, initial_life)    print_board(board, name, size, 0)    reason = generate(name, size, generations, board, 1)    case reason do      :all_dead -> "no more life."      :static   -> "no movement"      _         -> "specified lifetime ended"    end    |> IO.puts    IO.puts ""  end   defp new_board(n) do    for x <- 1..n, y <- 1..n, into: %{}, do: {{x,y}, 0}  end   defp seed(n, points) do    if points do      points    else # randomly seed board      (for x <- 1..n, y <- 1..n, do: {x,y}) |> Enum.take_random(10)    end    |> Enum.reduce(new_board(n), fn pos,acc -> %{acc | pos => 1} end)  end   defp generate(_, _, generations, _, gen) when generations < gen, do: :ok  defp generate(name, size, generations, board, gen) do    new = evolve(board, size)    print_board(new, name, size, gen)    cond do      barren?(new) -> :all_dead      board == new -> :static      true         -> generate(name, size, generations, new, gen+1)    end  end   defp evolve(board, n) do    for x <- 1..n, y <- 1..n, into: %{}, do: {{x,y}, fate(board, x, y, n)}  end   defp fate(board, x, y, n) do    irange = max(1, x-1) .. min(x+1, n)    jrange = max(1, y-1) .. min(y+1, n)    sum = ((for i <- irange, j <- jrange, do: board[{i,j}]) |> Enum.sum) - board[{x,y}]    cond do      sum == 3                       -> 1      sum == 2 and board[{x,y}] == 1 -> 1      true                           -> 0    end  end   defp barren?(board) do    Enum.all?(board, fn {_,v} -> v == 0 end)  end   defp print_board(board, name, n, generation) do    IO.puts "#{name}: generation #{generation}"    Enum.each(1..n, fn y ->      Enum.map(1..n, fn x -> if board[{x,y}]==1, do: "#", else: "." end)      |> IO.puts    end)  endend Conway.game_of_life("blinker", 3, 2, [{2,1},{2,2},{2,3}])Conway.game_of_life("glider", 4, 4, [{2,1},{3,2},{1,3},{2,3},{3,3}])Conway.game_of_life("random", 5, 10)`
Output:
```blinker: generation 0
.#.
.#.
.#.
...
###
...
.#.
.#.
.#.

glider: generation 0
.#..
..#.
###.
....
glider: generation 1
....
#.#.
.##.
.#..
glider: generation 2
....
..#.
#.#.
.##.
glider: generation 3
....
.#..
..##
.##.
glider: generation 4
....
..#.
...#
.###

random: generation 0
.#...
#.#..
#...#
###.#
..#..
random: generation 1
.#...
#....
#.#..
#.#..
..##.
random: generation 2
.....
#....
#....
..#..
.###.
random: generation 3
.....
.....
.#...
..##.
.###.
random: generation 4
.....
.....
..#..
...#.
.#.#.
random: generation 5
.....
.....
.....
...#.
..#..
random: generation 6
.....
.....
.....
.....
.....
no more life.
```

## Emacs Lisp

`#!/usr/bin/env emacs -script;; -*- lexical-binding: t -*-;; run: ./conways-life conways-life.config(require 'cl-lib) (defconst blinker '("***"))(defconst toad '(".***" "***."))(defconst pentomino-p '(".**" ".**" ".*."))(defconst pi-heptomino '("***" "*.*" "*.*"))(defconst glider '(".*." "..*" "***"))(defconst pre-pulsar '("***...***" "*.*...*.*" "***...***"))(defconst ship '("**." "*.*" ".**"))(defconst pentadecathalon '("**********"))(defconst clock '("..*." "*.*." ".*.*" ".*..")) (defmacro swap (a b)  `(setq ,b (prog1 ,a (setq ,a ,b)))) (cl-defstruct world rows cols data) (defun new-world (rows cols)  (make-world :rows rows :cols cols :data (make-vector (* rows cols) nil))) (defmacro world-pt (w r c)  `(+ (* (mod ,r (world-rows ,w)) (world-cols ,w))      (mod ,c (world-cols ,w)))) (defmacro world-ref (w r c)  `(aref (world-data ,w) (world-pt ,w ,r ,c))) (defun print-world (world)  (dotimes (r (world-rows world))    (dotimes (c (world-cols world))      (princ (format "%c" (if (world-ref world r c) ?* ?.))))    (terpri))) (defun insert-pattern (world row col shape)  (let ((r row)        (c col))    (unless (listp shape)      (setq shape (symbol-value shape)))    (dolist (row-data shape)      (dolist (col-data (mapcar 'identity row-data))        (setf (world-ref world r c) (not (or (eq col-data ?.))))        (setq c (1+ c)))      (setq r (1+ r))      (setq c col)))) (defun neighbors (world row col)  (let ((n 0))    (dolist (offset '((1 . 1) (1 . 0) (1 . -1) (0 . 1) (0 . -1) (-1 . 1) (-1 . 0) (-1 . -1)))      (when (world-ref world (+ row (car offset)) (+ col (cdr offset)))        (setq n (1+ n))))    n)) (defun advance-generation (old new)  (dotimes (r (world-rows old))    (dotimes (c (world-cols old))      (let ((n (neighbors old r c)))        (setf (world-ref new r c)              (if (world-ref old r c)                  (or (= n 2) (= n 3))                (= n 3))))))) (defun read-config (file-name)  (with-temp-buffer    (insert-file-contents-literally file-name)    (read (current-buffer)))) (defun get-config (key config)  (let ((val (assoc key config)))    (if (null val)        (error (format "missing value for %s" key))      (cdr val)))) (defun insert-patterns (world patterns)  (dolist (p patterns)    (apply 'insert-pattern (cons world p)))) (defun simulate-life (file-name)  (let* ((config (read-config file-name))         (rows (get-config 'rows config))         (cols (get-config 'cols config))         (generations (get-config 'generations config))         (a (new-world rows cols))         (b (new-world rows cols)))    (insert-patterns a (get-config 'patterns config))    (dotimes (g generations)      (princ (format "generation %d\n" g))      (print-world a)      (advance-generation a b)      (swap a b)))) (simulate-life (elt command-line-args-left 0))`

Configuration file, which defines the size starting patterns and how long the simulation will run.

`((rows . 8) (cols . 10) (generations . 3) (patterns  ;; Blinker is defined in the script.  (1 1 blinker)  ;; This is a custom pattern.  (4 4 (".***"        "***."))))`
Output:
```generation 0
..........
.***......
..........
..........
.....***..
....***...
..........
..........
generation 1
..*.......
..*.......
..*.......
......*...
....*..*..
....*..*..
.....*....
..........
generation 2
..........
.***......
..........
..........
.....***..
....***...
..........
..........
```

## Erlang

`  -module(life). -export([bang/1]).  -define(CHAR_DEAD,   32).  % " "-define(CHAR_ALIVE, 111).  % "o"-define(CHAR_BAR,    45).  % "-" -define(GEN_INTERVAL, 100).  -record(state, {x            :: non_neg_integer()               ,y            :: non_neg_integer()               ,n            :: pos_integer()               ,bar          :: nonempty_string()               ,board        :: array()               ,gen_count    :: pos_integer()               ,gen_duration :: non_neg_integer()               ,print_time   :: non_neg_integer()               }).  %% ============================================================================%% API%% ============================================================================ bang(Args) ->    [X, Y] = [atom_to_integer(A) || A <- Args],    {Time, Board} = timer:tc(fun() -> init_board(X, Y) end),    State = #state{x            = X                  ,y            = Y                  ,n            = X * Y                  ,bar          = [?CHAR_BAR || _ <- lists:seq(1, X)]                  ,board        = Board                  ,gen_count    = 1  % Consider inital state to be generation 1                  ,gen_duration = Time                  ,print_time   = 0  % There was no print time yet    },    life_loop(State).  %% ============================================================================%% Internal%% ============================================================================ life_loop(    #state{x            = X          ,y            = Y          ,n            = N          ,bar          = Bar          ,board        = Board          ,gen_count    = GenCount          ,gen_duration = Time          ,print_time   = LastPrintTime    }=State) ->     {PrintTime, ok} = timer:tc(        fun() ->            do_print_screen(Board, Bar, X, Y, N, GenCount, Time, LastPrintTime)        end    ),     {NewTime, NewBoard} = timer:tc(        fun() ->            next_generation(X, Y, Board)        end    ),     NewState = State#state{board        = NewBoard                          ,gen_count    = GenCount + 1                          ,gen_duration = NewTime                          ,print_time   = PrintTime    },     NewTimeMil = NewTime / 1000,    NextGenDelay = at_least_zero(round(?GEN_INTERVAL - NewTimeMil)),    timer:sleep(NextGenDelay),     life_loop(NewState).  at_least_zero(Integer) when Integer >= 0 -> Integer;at_least_zero(_) -> 0.  do_print_screen(Board, Bar, X, Y, N, GenCount, Time, PrintTime) ->    ok = do_print_status(Bar, X, Y, N, GenCount, Time, PrintTime),    ok = do_print_board(Board).  do_print_status(Bar, X, Y, N, GenCount, TimeMic, PrintTimeMic) ->    TimeSec = TimeMic / 1000000,    PrintTimeSec = PrintTimeMic / 1000000,    ok = io:format("~s~n", [Bar]),    ok = io:format(        "X: ~b Y: ~b CELLS: ~b GENERATION: ~b DURATION: ~f PRINT TIME: ~f~n",        [X, Y, N, GenCount, TimeSec, PrintTimeSec]    ),    ok = io:format("~s~n", [Bar]).  do_print_board(Board) ->    % It seems that just doing a fold should be faster than map + to_list    % combo, but, after measuring several times, map + to_list has been    % consistently (nearly twice) faster than either foldl or foldr.    RowStrings = array:to_list(        array:map(            fun(_, Row) ->                array:to_list(                    array:map(                        fun(_, State) ->                            state_to_char(State)                        end,                        Row                    )                )            end,            Board        )    ),     ok = lists:foreach(        fun(RowString) ->            ok = io:format("~s~n", [RowString])        end,        RowStrings    ).  state_to_char(0) -> ?CHAR_DEAD;state_to_char(1) -> ?CHAR_ALIVE.  next_generation(W, H, Board) ->    array:map(        fun(Y, Row) ->            array:map(                fun(X, State) ->                    Neighbors = filter_offsides(H, W, neighbors(X, Y)),                    States = neighbor_states(Board, Neighbors),                    LiveNeighbors = lists:sum(States),                    new_state(State, LiveNeighbors)                end,                Row            )        end,        Board    ).  new_state(1, LiveNeighbors) when LiveNeighbors  <  2 -> 0;new_state(1, LiveNeighbors) when LiveNeighbors  <  4 -> 1;new_state(1, LiveNeighbors) when LiveNeighbors  >  3 -> 0;new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;new_state(State, _LiveNeighbors) -> State.  neighbor_states(Board, Neighbors) ->    [array:get(X, array:get(Y, Board)) || {X, Y} <- Neighbors].  filter_offsides(H, W, Coordinates) ->    [{X, Y} || {X, Y} <- Coordinates, is_onside(X, Y, H, W)].  is_onside(X, Y, H, W) when (X >= 0) and (Y >= 0) and (X < W) and (Y < H) -> true;is_onside(_, _, _, _) -> false.  neighbors(X, Y) ->    [{X + OffX, Y + OffY} || {OffX, OffY} <- offsets()].  offsets() ->    [offset(D) || D <- directions()].  offset('N')  -> { 0, -1};offset('NE') -> { 1, -1};offset('E')  -> { 1,  0};offset('SE') -> { 1,  1};offset('S')  -> { 0,  1};offset('SW') -> {-1,  1};offset('W')  -> {-1,  0};offset('NW') -> {-1, -1}.  directions() ->    ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'].  init_board(X, Y) ->    array:map(fun(_, _) -> init_row(X) end, array:new(Y)).  init_row(X) ->    array:map(fun(_, _) -> init_cell_state() end, array:new(X)).  init_cell_state() ->    crypto:rand_uniform(0, 2).  atom_to_integer(Atom) ->    list_to_integer(atom_to_list(Atom)).  `

## ERRE

This is a simple implementation of Conway's game of Life with an endless world. Test pattern configuration is 'glider'.

` PROGRAM LIFE !\$INTEGER !\$KEY!for C-64 compatibility CONST Xmax=38,Ymax=20 DIM x,y,NDIM WORLD[39,21],NextWORLD[39,21] BEGIN ! Glider test!------------------------------------------WORLD[1,1]=1 WORLD[1,2]=0 WORLD[1,3]=0WORLD[2,1]=0 WORLD[2,2]=1 WORLD[2,3]=1WORLD[3,1]=1 WORLD[3,2]=1 WORLD[3,3]=0!------------------------------------------ PRINT(CHR\$(12);"Press any key to interrupt")LOOP  PRINT(CHR\$(11);) PRINT  PRINT(STRING\$(Xmax+2,"-"))  !---------- endless world ---------  FOR y=1 TO Ymax DO    WORLD[0,y]=WORLD[Xmax,y]    WORLD[Xmax+1,y]=WORLD[1,y]  END FOR  FOR x=1 TO Xmax DO    WORLD[x,0]=WORLD[x,Ymax]    WORLD[x,Ymax+1]=WORLD[x,1]  END FOR  WORLD[0,0]=WORLD[Xmax,Ymax]  WORLD[Xmax+1,Ymax+1]=WORLD[1,1]  WORLD[Xmax+1,0]=WORLD[1,Ymax]  WORLD[0,Ymax+1]=WORLD[Xmax,1]  !---------- endless world ---------  FOR y=1 TO Ymax DO    PRINT("|";)    FOR x=1 TO Xmax DO      PRINT(CHR\$(32+WORLD[x,y]*3);)      N=WORLD[x-1,y-1]+WORLD[x-1,y]+WORLD[x-1,y+1]+WORLD[x,y-1]      N=N+WORLD[x,y+1]+WORLD[x+1,y-1]+WORLD[x+1,y]+WORLD[x+1,y+1]      IF (WORLD[x,y]<>0 AND (N=2 OR N=3)) OR (WORLD[x,y]=0 AND N=3) THEN        NextWORLD[x,y]=1      ELSE        NextWORLD[x,y]=0      END IF    END FOR    PRINT("|")  END FOR  PRINT(STRING\$(Xmax+2,"-"))  PAUSE(0.1)   FOR x=0 TO Xmax+1 DO    FOR y=0 TO Ymax+1 DO      WORLD[x,y]=NextWORLD[x,y]      NextWORLD[x,y]=0    END FOR  END FOR  REPEAT      GET(A\$)  UNTIL A\$<>""  EXIT IF A\$=CHR\$(27)END LOOP PRINT("Press any key to exit")REPEATUNTIL GETKEY\$<>""END PROGRAM  `

## F#

The following F# implementation uses for visualization and is easily compiled into a standalone executable:
`let count (a: _ [,]) x y =  let m, n = a.GetLength 0, a.GetLength 1  let mutable c = 0  for x in x-1..x+1 do    for y in y-1..y+1 do      if x>=0 && x<m && y>=0 && y<n && a.[x, y] then        c <- c + 1  if a.[x, y] then c-1 else c let rule (a: _ [,]) x y =  match a.[x, y], count a x y with  | true, (2 | 3) | false, 3 -> true  | _ -> false open System.Windowsopen System.Windows.Media.Imaging [<System.STAThread>]do  let rand = System.Random()  let n = 256  let game = Array2D.init n n (fun _ _ -> rand.Next 2 = 0) |> ref  let image = Controls.Image(Stretch=Media.Stretch.Uniform)  let format = Media.PixelFormats.Gray8  let pixel = Array.create (n*n) 0uy  let update _ =    game := rule !game |> Array2D.init n n    for x in 0..n-1 do      for y in 0..n-1 do        pixel.[x+y*n] <- if (!game).[x, y] then 255uy else 0uy    image.Source <-      BitmapSource.Create(n, n, 1.0, 1.0, format, null, pixel, n)  Media.CompositionTarget.Rendering.Add update  Window(Content=image, Title="Game of Life")  |> (Application()).Run |> ignore`

## Forth

gencell uses an optimization for the core Game of Life rules: new state = (old state | neighbors == 3).

` \ The fast wrapping requires dimensions that are powers of 2. 1 6 lshift constant w \ 64 1 4 lshift constant h \ 16  : rows    w * 2* ; 1 rows constant row h rows constant size  create world size allot world   value old old w + value new  variable gens : clear  world size erase     0 gens ! ; : age  new old to new to old  1 gens +! ;  : col+  1+ ; : col-  1- dup w and + ; \ avoid borrow into row : row+  row + ; : row-  row - ; : wrap ( i -- i ) [ size w - 1- ] literal and ; : [email protected] ( i -- 0/1 ) wrap old + [email protected] ; : w! ( 0/1 i -- ) wrap old + c! ;  : foreachrow ( xt -- )   size 0 do  I over execute  row +loop drop ;  : showrow ( i -- ) cr   old + w over + swap do I [email protected] if [char] * else bl then emit loop ; : show  ['] showrow foreachrow  cr ." Generation " gens @ . ;  : sum-neighbors ( i -- i n )   dup  col- row- [email protected]   over      row- [email protected] +   over col+ row- [email protected] +   over col-      [email protected] +   over col+      [email protected] +   over col- row+ [email protected] +   over      row+ [email protected] +   over col+ row+ [email protected] + ; : gencell ( i -- )   sum-neighbors  over old + [email protected]   or 3 = 1 and   swap new + c! ; : genrow ( i -- )   w over + swap do I gencell loop ; : gen  ['] genrow foreachrow  age ;  : life  begin gen 0 0 at-xy show key? until ;  \ patterns char | constant '|' : pat ( i addr len -- )   rot dup 2swap  over + swap do     I [email protected] '|' = if drop row+ dup else     I [email protected] bl  = 1+ over w!  col+ then   loop 2drop ;  : blinker s" ***" pat ; : toad s" ***| ***" pat ; : pentomino s" **| **| *" pat ; : pi s" **| **|**" pat ; : glider s"  *|  *|***" pat ; : pulsar s" *****|*   *" pat ; : ship s"  ****|*   *|    *|   *" pat ; : pentadecathalon s" **********" pat ; : clock s"  *|  **|**|  *" pat ;  clear  0 glider show  *   * ***  Generation 0  ok gen show  * *  **  * Generation 1  ok`

## Fortran

Works with: Fortran version 90 and later
` PROGRAM LIFE_2D   IMPLICIT NONE    INTEGER, PARAMETER :: gridsize = 10   LOGICAL :: cells(0:gridsize+1,0:gridsize+1) = .FALSE.   INTEGER :: i, j, generation=0   REAL :: rnums(gridsize,gridsize)  !  Start patterns !  ************** !  cells(2,1:3) = .TRUE.                                                  ! Blinker !  cells(3,4:6) = .TRUE. ; cells(4,3:5) = .TRUE.                          ! Toad !  cells(1,2) = .TRUE. ; cells(2,3) = .TRUE. ; cells(3,1:3) = .TRUE.      ! Glider    cells(3:5,3:5) = .TRUE. ; cells(6:8,6:8) = .TRUE.                      ! Figure of Eight !  CALL RANDOM_SEED !  CALL RANDOM_NUMBER(rnums) !  WHERE (rnums>0.6) cells(1:gridsize,1:gridsize) = .TRUE.                ! Random universe    CALL Drawgen(cells(1:gridsize, 1:gridsize), generation)   DO generation = 1, 8      CALL NextgenV2(cells)      CALL Drawgen(cells(1:gridsize, 1:gridsize), generation)   END DO  CONTAINS    SUBROUTINE Drawgen(cells, gen)     LOGICAL, INTENT(IN OUT) :: cells(:,:)     INTEGER, INTENT(IN) :: gen      WRITE(*, "(A,I0)") "Generation ", gen      DO i = 1, SIZE(cells,1)        DO j = 1, SIZE(cells,2)           IF (cells(i,j)) THEN              WRITE(*, "(A)", ADVANCE = "NO") "#"           ELSE              WRITE(*, "(A)", ADVANCE = "NO") " "           END IF        END DO        WRITE(*,*)     END DO     WRITE(*,*)   END SUBROUTINE Drawgen   SUBROUTINE Nextgen(cells)     LOGICAL, INTENT(IN OUT) :: cells(0:,0:)     LOGICAL :: buffer(0:SIZE(cells, 1)-1, 0:SIZE(cells, 2)-1)     INTEGER :: neighbours, i, j      buffer = cells   ! Store current status     DO j = 1, SIZE(cells, 2)-2        DO i = 1, SIZE(cells, 1)-2          if(buffer(i, j)) then            neighbours = sum(count(buffer(i-1:i+1, j-1:j+1), 1)) - 1          else            neighbours = sum(count(buffer(i-1:i+1, j-1:j+1), 1))          end if           SELECT CASE(neighbours)            CASE (0:1, 4:8)               cells(i,j) = .FALSE.             CASE (2)               ! No change             CASE (3)               cells(i,j) = .TRUE.          END SELECT         END DO     END DO  END SUBROUTINE Nextgen !###########################################################################!   In this version instead of cycling through all points an integer array! is used the sum the live neighbors of all points. The sum is done with! the entire array cycling through the eight positions of the neighbors.!   Executing a grid size of 10000 in 500 generations this version gave a ! speedup of almost 4 times.!###########################################################################  PURE SUBROUTINE NextgenV2(cells)     LOGICAL, INTENT(IN OUT) :: cells(:,:)     INTEGER(KIND=1) :: buffer(1:SIZE(cells, 1)-2,1:SIZE(cells, 2)-2)     INTEGER :: gridsize, i, j      gridsize=SIZE(cells, 1)     buffer=0      DO j=-1, 1        DO i=-1,1           IF(i==0 .AND. j==0) CYCLE           WHERE(cells(i+2:gridsize-i-1,j+2:gridsize-j-1)) buffer=buffer+1        END DO     END DO      WHERE(buffer<2 .or. buffer>3) cells(2:gridsize-1,2:gridsize-1) = .FALSE.     WHERE(buffer==3) cells(2:gridsize-1,2:gridsize-1) = .TRUE.  END SUBROUTINE NextgenV2!########################################################################### END PROGRAM LIFE_2D`
Output:
```Blinker
Generation 0

###

Generation 1
#
#
#

Generation 2

###
Figure of Eight (a period eight oscillator)
Generation 0

###
###
###
###
###
###

Generation 1

#
# #
#   #
#   #
#   #
#   #
# #
#

Generation 2

#
###
### #
#   #
#   #
# ###
###
#

Generation 3

###
#
#   #
#  # #
# #  #
#   #
#
###

Generation 4
#
##
# ##
###  #
# # #
# # #
#  ###
## #
##
#

Generation 5
##

#   #
#    #
# # #
# # #
#    #
#   #

##

Generation 6

#

# ###
## #
# ##
### #

#

Generation 7

##
## #
#
#
# ##
##

Generation 8

###
###
###
###
###
###
```

## FunL

`import lists.zipWithIndeximport util.Regex data Rule( birth, survival ) val Mirek = Regex( '([0-8]+)/([0-8]+)' )val Golly = Regex( 'B([0-8]+)/S([0-8]+)' ) def decode( rule ) =  def makerule( b, s ) = Rule( [int(d) | d <- b], [int(d) | d <- s] )   case rule    Mirek( s, b ) -> makerule( b, s )    Golly( b, s ) -> makerule( b, s )    _ -> error( "unrecognized rule: \$rule" ) def fate( state, crowding, rule ) = crowding in rule( int(state) ) def crowd( buffer, x, y ) =  res = 0   def neighbour( x, y ) =    if x >= 0 and x < N and y >= 0 and y < N      res += int( buffer(x, y) )   for i <- x-1..x+1    neighbour( i, y - 1 )    neighbour( i, y + 1 )   neighbour( x - 1, y )  neighbour( x + 1, y )  res def display( buffer ) =  for j <- 0:N    for i <- 0:N      print( if buffer(i, j) then '*' else '\u00b7' )     println() def generation( b1, b2, rule ) =  for i <- 0:N, j <- 0:N    b2(i, j) = fate( b1(i, j), crowd(b1, i, j), rule ) def pattern( p, b, x, y ) =  for (r, j) <- zipWithIndex( list(WrappedString(p).stripMargin().split('\n')).drop(1).dropRight(1) )    for i <- 0:r.length()      b(x + i, y + j) = r(i) == '*' var current = 0val LIFE = decode( '23/3' )val N = 4val buffers = (array( N, N, (_, _) -> false ), array( N, N )) def reset =  for i <- 0:N, j <- 0:N    buffers(0)(i, j) = false   current = 0 def iteration =  display( buffers(current) )  generation( buffers(current), buffers(current = (current + 1)%2), LIFE )  println( 5'-' ) // two patterns to be testedblinker = '''  |  |***  ''' glider = '''  | *  |  *  |***  ''' // load "blinker" pattern and run for three generationspattern( blinker, buffers(0), 0, 0 ) repeat 3  iteration() // clear grid, load "glider" pattern and run for five generationsreset()pattern( glider, buffers(0), 0, 0 ) repeat 5  iteration()`
Output:
```····
***·
····
····
-----
·*··
·*··
·*··
····
-----
····
***·
····
····
-----
·*··
··*·
***·
····
-----
····
*·*·
·**·
·*··
-----
····
··*·
*·*·
·**·
-----
····
·*··
··**
·**·
-----
····
··*·
···*
·***
-----
```

## Futhark

` fun bint(b: bool): int = if b then 1 else 0fun intb(x: int): bool = if x == 0 then False else True fun to_bool_board(board: [][]int): [][]bool =  map (fn (r: []int): []bool  => map intb r) board fun to_int_board(board: [][]bool): [][]int =  map (fn (r: []bool): []int  => map bint r) board fun cell_neighbors(i: int, j: int, board: [n][m]bool): int =  unsafe  let above = (i - 1) % n  let below = (i + 1) % n  let right = (j + 1) % m  let left = (j - 1) % m in  bint board[above,left] + bint board[above,j]  + bint board[above,right] +  bint board[i,left] + bint board[i,right] +  bint board[below,left] + bint board[below,j] + bint board[below,right] fun all_neighbours(board: [n][m]bool): [n][m]int =  map (fn (i: int): []int  =>        map (fn (j: int): int  => cell_neighbors(i,j,board)) (iota m))      (iota n) fun iteration(board: [n][m]bool): [n][m]bool =  let lives = all_neighbours(board) in  zipWith (fn (lives_r: []int) (board_r: []bool): []bool  =>            zipWith (fn (neighbors: int) (alive: bool): bool  =>                      if neighbors < 2                      then False                      else if neighbors == 3 then True                      else if alive && neighbors < 4 then True                      else False)                    lives_r board_r)           lives board fun main(int_board: [][]int, iterations: int): [][]int =  -- We accept the board as integers for convenience, and then we  -- convert to booleans here.  let board = to_bool_board int_board in  loop (board) = for i < iterations do    iteration board in  to_int_board board `

## Go

`package main import (	"bytes"	"fmt"	"math/rand"	"time") type Field struct {	s    [][]bool	w, h int} func NewField(w, h int) Field {	s := make([][]bool, h)	for i := range s {		s[i] = make([]bool, w)	}	return Field{s: s, w: w, h: h}} func (f Field) Set(x, y int, b bool) {	f.s[y][x] = b} func (f Field) Next(x, y int) bool {	on := 0	for i := -1; i <= 1; i++ {		for j := -1; j <= 1; j++ {			if f.State(x+i, y+j) && !(j == 0 && i == 0) {				on++			}		}	}	return on == 3 || on == 2 && f.State(x, y)} func (f Field) State(x, y int) bool {	for y < 0 {		y += f.h	}	for x < 0 {		x += f.w	}	return f.s[y%f.h][x%f.w]} type Life struct {	w, h int	a, b Field} func NewLife(w, h int) *Life {	a := NewField(w, h)	for i := 0; i < (w * h / 2); i++ {		a.Set(rand.Intn(w), rand.Intn(h), true)	}	return &Life{		a: a,		b: NewField(w, h),		w: w, h: h,	}} func (l *Life) Step() {	for y := 0; y < l.h; y++ {		for x := 0; x < l.w; x++ {			l.b.Set(x, y, l.a.Next(x, y))		}	}	l.a, l.b = l.b, l.a} func (l *Life) String() string {	var buf bytes.Buffer	for y := 0; y < l.h; y++ {		for x := 0; x < l.w; x++ {			b := byte(' ')			if l.a.State(x, y) {				b = '*'			}			buf.WriteByte(b)		}		buf.WriteByte('\n')	}	return buf.String()} func main() {	l := NewLife(80, 15)	for i := 0; i < 300; i++ {		l.Step()		fmt.Print("\x0c")		fmt.Println(l)		time.Sleep(time.Second / 30)	}}`

Running this program will compute and draw the first 300 "frames". The final frame looks like this:

```        ** ****        *
* **
*                                         **
*                                                  *  *
*                   **        ****                 **                         *
*                  **       *  **
*                      **        *
**      *
****              *        *                                     **         *
***   **           *         ** ****                             *  *        **
* **     *  **      *              **                              **
*    *** ***
* **
**   **       **
** *  *      * *
```

## Groovy

` class GameOfLife { 	int generations	int dimensions	def board 	GameOfLife(generations = 5, dimensions = 5) {		this.generations = generations		this.dimensions = dimensions		this.board = createBlinkerBoard()	} 	static def createBlinkerBoard() {		[			[].withDefault{0},			[0,0,1].withDefault{0},			[0,0,1].withDefault{0},			[0,0,1].withDefault{0}		].withDefault{[]}	} 	static def createGliderBoard() {		[			[].withDefault{0},			[0,0,1].withDefault{0},			[0,0,0,1].withDefault{0},			[0,1,1,1].withDefault{0}		].withDefault{[]}	} 	static def getValue(board, point) {		def x,y		(x,y) = point		if(x < 0 || y < 0) {			return 0		}		board[x][y] ? 1 : 0	} 	static def countNeighbors(board, point) {		def x,y		(x,y) = point		def neighbors = 0		neighbors += getValue(board, [x-1,y-1])		neighbors += getValue(board, [x-1,y])		neighbors += getValue(board, [x-1,y+1])		neighbors += getValue(board, [x,y-1])		neighbors += getValue(board, [x,y+1])		neighbors += getValue(board, [x+1,y-1])		neighbors += getValue(board, [x+1,y])		neighbors += getValue(board, [x+1,y+1])		neighbors	} 	static def conwaysRule(currentValue, neighbors) {		def newValue = 0		if(neighbors == 3 || (currentValue && neighbors == 2)) {			newValue = 1		}		newValue	} 	static def createNextGeneration(currentBoard, dimensions) {		def newBoard = [].withDefault{[].withDefault{0}}		(0..(dimensions-1)).each { row ->			(0..(dimensions-1)).each { column ->				def point = [row, column]				def currentValue = getValue(currentBoard, point)				def neighbors = countNeighbors(currentBoard, point)				newBoard[row][column] = conwaysRule(currentValue, neighbors)			}		}		newBoard	} 	static def printBoard(generationCount, board, dimensions) {		println "Generation \${generationCount}"		println '*' * 80		(0..(dimensions-1)).each { row ->			(0..(dimensions-1)).each { column ->				print board[row][column] ? 'X' : '.'			}			print System.getProperty('line.separator')		}		println ''	} 	def start() {		(1..generations).each { generation ->			printBoard(generation, this.board, this.dimensions)			this.board = createNextGeneration(this.board, this.dimensions)		}	} } // Blinkerdef game = new GameOfLife()game.start() // Glidergame = new GameOfLife(10, 10)game.board = game.createGliderBoard()game.start() `

The output of this program:

```Generation 1
********************************************************************************
.....
..X..
..X..
..X..
.....

Generation 2
********************************************************************************
.....
.....
.XXX.
.....
.....

Generation 3
********************************************************************************
.....
..X..
..X..
..X..
.....

Generation 4
********************************************************************************
.....
.....
.XXX.
.....
.....

Generation 5
********************************************************************************
.....
..X..
..X..
..X..
.....

Generation 1
********************************************************************************
..........
..X.......
...X......
.XXX......
..........
..........
..........
..........
..........
..........

Generation 2
********************************************************************************
..........
..........
.X.X......
..XX......
..X.......
..........
..........
..........
..........
..........

Generation 3
********************************************************************************
..........
..........
...X......
.X.X......
..XX......
..........
..........
..........
..........
..........

Generation 4
********************************************************************************
..........
..........
..X.......
...XX.....
..XX......
..........
..........
..........
..........
..........

Generation 5
********************************************************************************
..........
..........
...X......
....X.....
..XXX.....
..........
..........
..........
..........
..........

Generation 6
********************************************************************************
..........
..........
..........
..X.X.....
...XX.....
...X......
..........
..........
..........
..........

Generation 7
********************************************************************************
..........
..........
..........
....X.....
..X.X.....
...XX.....
..........
..........
..........
..........

Generation 8
********************************************************************************
..........
..........
..........
...X......
....XX....
...XX.....
..........
..........
..........
..........

Generation 9
********************************************************************************
..........
..........
..........
....X.....
.....X....
...XXX....
..........
..........
..........
..........

Generation 10
********************************************************************************
..........
..........
..........
..........
...X.X....
....XX....
....X.....
..........
..........
..........

```

`import Data.Array.Unboxed type Grid = UArray (Int,Int) Bool -- The grid is indexed by (y, x). life :: Int -> Int -> Grid -> Grid{- Returns the given Grid advanced by one generation. -}life w h old =    listArray b (map f (range b))  where b@((y1,x1),(y2,x2)) = bounds old        f (y, x) = ( c && (n == 2 || n == 3) ) || ( not c && n == 3 )          where c = get x y                n = count [get (x + x') (y + y') |                    x' <- [-1, 0, 1], y' <- [-1, 0, 1],                    not (x' == 0 && y' == 0)]         get x y | x < x1 || x > x2 = False                | y < y1 || y > y2 = False                | otherwise       = old ! (y, x) count :: [Bool] -> Intcount = length . filter id`

Example of use:

`import Data.List (unfoldr) grid :: [String] -> (Int, Int, Grid)grid l = (width, height, a)  where (width, height) = (length \$ head l, length l)        a = listArray ((1, 1), (height, width)) \$ concatMap f l        f = map g        g '.' = False        g _   = True printGrid :: Int -> Grid -> IO ()printGrid width = mapM_ f . split width . elems  where f = putStrLn . map g        g False = '.'        g _     = '#' split :: Int -> [a] -> [[a]]split n = takeWhile (not . null) . unfoldr (Just . splitAt n) blinker = grid   [".#.",    ".#.",    ".#."] glider = grid   ["............",    "............",    "............",    ".......###..",    ".......#....",    "........#...",    "............"] printLife :: Int -> (Int, Int, Grid) -> IO ()printLife n (w, h, g) = mapM_ f \$ take n \$ iterate (life w h) g  where f g = do            putStrLn "------------------------------"            printGrid w g main = printLife 10 glider`

Here's the gridless version. It could probably be improved with some light use of `Data.Set`, but I leave that as an exercise for the reader. Note that the function `lifeStep` is the solution in its entirety. The rest of this code deals with printing and test data for the particular model of the world we're using.

`module Main whereimport Data.List lifeStep :: [(Int, Int)] -> [(Int, Int)]lifeStep cells = [head g | g <- grouped cells, viable g]  where grouped = group . sort . concatMap neighbors        neighbors (x, y) = [(x+dx, y+dy) | dx <- [-1..1], dy <- [-1..1], (dx,dy) /= (0,0)]        viable [_,_,_] = True        viable [c,_] = c `elem` cells        viable _ = False  showWorld :: [(Int, Int)] -> IO ()showWorld cells = mapM_ putStrLn \$ worldToGrid cells  where worldToGrid cells = [[cellChar (x, y) | x <- [least..greatest]] | y <- [least..greatest]]        cellChar cell = if cell `elem` cells then '#' else '.'        (least, greatest) = worldBounds cells worldBounds cells = (least, greatest)  where least = min x y        greatest = max x' y'        (x, y) = head cells        (x', y') = last cells runLife :: Int -> [(Int, Int)] -> IO ()runLife steps cells = rec (steps - 1) cells  where rec 0 cells = showWorld cells        rec s cells = do showWorld cells                         putStrLn ""                         rec (s - 1) \$ lifeStep cells glider = [(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)]blinker = [(1, 0), (1, 1), (1, 2)] main :: IO ()main = do  putStrLn "Glider >> 10"  putStrLn "------------"  runLife 10 glider  putStrLn ""  putStrLn "Blinker >> 3"  putStrLn "------------"  runLife 3 blinker`

## HolyC

Conway's Game of Life in HolyC for TempleOS ported from Conway's_Game_of_Life#C.

Also see the TempleOS implementation of Conway's Game of Life which makes use of the graphics engine.

## Icon and Unicon

`global limit procedure main(args)    n := args[1] | 50        # default is a 50x50 grid    limit := args[2] | &null #  optional limit to number of generations    write("Enter the starting pattern, end with EOF")    grid := getInitialGrid(n)    play(grid)end # This procedure reads in the initial pattern, inserting it#   into an nXn grid of cells.  The nXn grid also gets a#   new border of empty cells, which just makes the test simpler#   for determining what do with a cell on each generation.# It would be better to let the user move the cursor and click#   on cells to create/delete living cells, but this version#   assumes a simple ASCII terminal.procedure getInitialGrid(n)    static notBlank, allStars    initial {        notBlank := ~' '                    allStars := repl("*",*notBlank)        }     g := []                # store as an array of strings     put(g,repl(" ",n))    while r := read() do {                        # read in rows of grid        r := left(r,n)                            #   force each to length n        put(g," "||map(r,notBlank,allStars)||" ") #   and making any life a '*'        }    while *g ~= (n+2) do        put(g,repl(" ",n))     return gend # Simple-minded procedure to 'play' Life from a starting grid.procedure play(grid)    while not allDone(grid) do {        display(grid)        grid := onePlay(grid)        }end # Display the gridprocedure display(g)    write(repl("-",*g[1]))    every write(!g)    write(repl("-",*g[1]))end # Compute one generation of Life from the current one.procedure onePlay(g)    ng := []    every put(ng, !g)        # new generation starts as copy of old    every ng[r := 2 to *g-1][c := 2 to *g-1] := case sum(g,r,c) of {                            3:       "*"     # cell lives (or is born)                            2:       g[r][c] # cell unchanged                            default: " "     # cell dead                            }    return ngend # Return the number of living cells surrounding the current cell.procedure sum(g,r,c)    cnt := 0    every (i := -1 to 1, j := -1 to 1) do        if ((i ~= 0) | (j ~= 0)) & (g[r+i][c+j] == "*") then cnt +:= 1    return cntend # Check to see if all the cells have died or we've exceeded the #   number of allowed generations.procedure allDone(g)   static count   initial count := 0   return ((count +:= 1) > \limit) | (trim(!g) == " ")end`

A sample run:

```->life 3 3
Enter the starting pattern, end with EOF

***
---

***

---
---

*
*
*

---
---

***

---
->
```

## J

Solution:

`pad=: 0,0,~0,.0,.~]life=: (3 3 (+/ e. 3+0,4&{)@,;._3 ])@padNB. the above could also be a one-line solution:life=: (3 3 (+/ e. 3+0,4&{)@,;._3 ])@(0,0,~0,.0,.~]) `

In other words, given a life instance, the next generation can be found by:

1. . adding extra empty cells, surrounding the life instance,
2. . tessellating the result, finding every overlapping 3 by 3 subinstance,
3. . totaling the number of live cells in each subinstance,
4. . treating a subinstance as a live cell iff that total is a member of the sequence 3,x where x is 3 if the center cell was previously dead, and 4 if the center cell was previously alive (that said, note that 4 is also the index of the center cell, with the sub instance arranged as a flat list).

Example (showing generations 0, 1 and 2 of a blinker):

`   life^:0 1 2 #:0 7 00 0 01 1 10 0 0 0 1 00 1 00 1 0 0 0 01 1 10 0 0`

Example (showing start and six following generations of a glider)

`   blocks=: (2 2\$2) ((7 u:' ▗▖▄▝▐▞▟▘▚▌▙▀▜▛█') {~ #[email protected],);._3 >.&.-:@\$ {. ]`
```   blocks"2 life^:(i.7) 4 5{.#:1 5 3
▖▌
▝▘

▝▄
▝▘

▚
▝▀

▗▗
▛

▗
▝▟

▖
▟▘

▗
▄▌```

## JAMES II/Rule-based Cellular Automata

Library: JAMES II
`@caversion 1; dimensions 2; //using Moore neighborhoodneighborhood moore; //available statesstate DEAD, ALIVE; /* if current state is ALIVE and the  neighborhood does not contain 2 or  3 ALIVE states the cell changes to  DEAD*/rule{ALIVE}:!ALIVE{2,3}->DEAD; /* if current state is DEAD and there  are exactly 3 ALIVE cells in the  neighborhood the cell changes to  ALIVE*/rule{DEAD}:ALIVE{3}->ALIVE;`

Animated output for the blinker example:

## Java

`public class GameOfLife{	public static void main(String[] args){		String[] dish= {				"_#_",				"_#_",				"_#_",};		int gens= 3;		for(int i= 0;i < gens;i++){			System.out.println("Generation " + i + ":");			print(dish);			dish= life(dish);		}	} 	public static String[] life(String[] dish){		String[] newGen= new String[dish.length];		for(int row= 0;row < dish.length;row++){//each row			newGen[row]= "";			for(int i= 0;i < dish[row].length();i++){//each char in the row				String above= "";//neighbors above				String same= "";//neighbors in the same row				String below= "";//neighbors below				if(i == 0){//all the way on the left					//no one above if on the top row					//otherwise grab the neighbors from above					above= (row == 0) ? null : dish[row - 1].substring(i,									i + 2);					same= dish[row].substring(i + 1, i + 2);					//no one below if on the bottom row					//otherwise grab the neighbors from below					below= (row == dish.length - 1) ? null : dish[row + 1]									.substring(i, i + 2);				}else if(i == dish[row].length() - 1){//right					//no one above if on the top row					//otherwise grab the neighbors from above					above= (row == 0) ? null : dish[row - 1].substring(i - 1,									i + 1);					same= dish[row].substring(i - 1, i);					//no one below if on the bottom row					//otherwise grab the neighbors from below					below= (row == dish.length - 1) ? null : dish[row + 1]									.substring(i - 1, i + 1);				}else{//anywhere else					//no one above if on the top row					//otherwise grab the neighbors from above					above= (row == 0) ? null : dish[row - 1].substring(i - 1,									i + 2);					same= dish[row].substring(i - 1, i)									+ dish[row].substring(i + 1, i + 2);					//no one below if on the bottom row					//otherwise grab the neighbors from below					below= (row == dish.length - 1) ? null : dish[row + 1]									.substring(i - 1, i + 2);				}				int neighbors= getNeighbors(above, same, below);				if(neighbors < 2 || neighbors > 3){					newGen[row]+= "_";//<2 or >3 neighbors -> die				}else if(neighbors == 3){					newGen[row]+= "#";//3 neighbors -> spawn/live				}else{					newGen[row]+= dish[row].charAt(i);//2 neighbors -> stay				}			}		}		return newGen;	} 	public static int getNeighbors(String above, String same, String below){		int ans= 0;		if(above != null){//no one above			for(char x: above.toCharArray()){//each neighbor from above				if(x == '#') ans++;//count it if someone is here			}		}		for(char x: same.toCharArray()){//two on either side			if(x == '#') ans++;//count it if someone is here		}		if(below != null){//no one below			for(char x: below.toCharArray()){//each neighbor below				if(x == '#') ans++;//count it if someone is here			}		}		return ans;	} 	public static void print(String[] dish){		for(String s: dish){			System.out.println(s);		}	}}`
Output:
```Generation 0:
_#_
_#_
_#_
Generation 1:
___
###
___
Generation 2:
_#_
_#_
_#_```

### Stretch

This fills in a random 10% of the grid, then activates the Game on it. Uncomment the call to the setCustomConfig function to use your own input. Just mind the grid limits. Use the input file given below to create a cool screensaver on your terminal.

` //package conway; import java.util.*;import java.io.*; public class GameOfLife {	//Set grid size	int l=20,b=60;	public static void main(String[] args)	{ 		GameOfLife now=new GameOfLife();		now.setGame();	}	void setGame()	{		char[][] config=new char[l][b];		startGame(config,l,b);	}	void startGame(char[][] mat,int l, int b)	{		Scanner s=new Scanner(System.in);		String ch="";		float per=0;		while(!ch.equals("y"))		{			per=setConfig(mat);			//setCustomConfig(mat,"GOLglidergun.txt");			display2D(mat);			System.out.println((per*100)+"% of grid filled.");			System.out.println("Begin? y/n");			ch=s.nextLine();		}		while(!ch.equals("x"))		{			mat=transform(mat,l,b);			display2D(mat); 			System.out.println("Ctrl+Z to stop."); 			try			{				Thread.sleep(100);			}			catch(Exception e)			{				System.out.println("Something went horribly wrong.");			} 			//ch=s.nextLine();		}		s.close();		System.out.println("Game Over");	} 	char[][] transform(char[][] mat,int l, int b)	{ 		char[][] newmat=new char[l][b];		for(int i=0;i<l;i++)			for(int j=0;j<b;j++)				newmat[i][j]=flip(mat,i,j);		return newmat;	}	char flip(char[][] mat,int i, int j)	{		int count=around(mat,i,j);		if(mat[i][j]=='*')		{			if(count<2||count>3)				return '_';			return '*';		}		else		{			if(count==3)				return '*';			return '_';		}	}	int around(char[][] mat, int i, int j)	{		int count=0;		for(int x=i-1;x<=i+1;x++)			for(int y=j-1;y<=j+1;y++)			{				if(x==i&&y==j)					continue;				count+=eval(mat,x,y);			}		return count;	}	int eval(char[][] mat, int i, int j)	{		if(i<0||j<0||i==l||j==b)			return 0;		if(mat[i][j]=='*')			return 1;		return 0;	} 	float setCustomConfig(char[][] arr,String infile)	{		try		{			BufferedReader br=new BufferedReader(new FileReader(infile));			String line;			for(int i=0;i<arr.length;i++)			{				line=br.readLine();				for(int j=0;j<arr[0].length;j++)					arr[i][j]=line.charAt(j);			}			br.close();		}		catch(Exception e)		{			System.out.println(e.getMessage());		}		return 0;	} 	float setConfig(char[][] arr)	{		//Enter percentage of grid to be filled.		float per=0.10f;//(float)Math.random();		for(int i=0;i<arr.length;i++)			setConfig1D(arr[i],per);		return per;	}	void setConfig1D(char[] arr,float per)	{		for(int i=0;i<arr.length;i++)		{			if(Math.random()<per)				arr[i]='*';			else				arr[i]='_';		}	}	void display2D(char[][] arr)	{		for(int i=0;i<arr.length;i++)			display1D(arr[i]);		System.out.println();	}	void display1D(char[] arr)	{		for(int i=0;i<arr.length;i++)			System.out.print(arr[i]);		System.out.println();	}} `

Glider Gun design. Save it in GOLglidergun.txt and uncomment the setCustomConfig function.

```____________________________________________________________
_________________________*__________________________________
_______________________*_*__________________________________
_____________**______**____________**_______________________
____________*___*____**____________**_______________________
_**________*_____*___**_____________________________________
_**________*___*_**____*_*__________________________________
___________*_____*_______*__________________________________
____________*___*___________________________________________
_____________**_____________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
```

### Java 10

` import static java.util.List.of; class GameOfLife {    boolean[][] board = new boolean[3][3];    GameOfLife() {}    GameOfLife(String[] board) {      set((i, j, s) -> board[i].charAt(j * 2) == '■');   }    void set(Setter setter) {      for (int i = 0; i < board.length; i++) {         for (int j = 0; j < board[i].length; j++) {            board[i][j] = setter.set(i, j, board[i][j]);         }      }   }    void get(Getter getter) {      set((i, j, s) -> {         getter.get(i, j, s);         return s;      });   }    int countNeighbors(int i, int j) {      var counter = new Getter() {         int count;          @Override         public void get(int li, int lj, boolean state) {            if (distance(i, j, li, lj) == 1 && board[li][lj])               count++;         }      };      get(counter);      return counter.count;   }    int distance(int i, int j, int li, int lj) {      return Math.max(           Math.abs(i - li),           Math.abs(j - lj));   }    GameOfLife makeNextGeneration() {      var n = new GameOfLife();      n.set((i, j, s) -> {         var alive = board[i][j];         int c = countNeighbors(i, j);         if (alive) {            return c == 2 || c == 3;         } else {            return c == 3;         }      });      return n;   }    void print() {      get((i, j, s) -> {         if (j == 0)            System.out.println();         System.out.print(s ? "■ " : "□ ");      });   }    interface Setter {      boolean set(int i, int j, boolean state);   }    interface Getter {      void get(int i, int j, boolean state);   }    public static void main(String[] args) {      String[] board = {           "□ ■ □ ",           "□ ■ □ ",           "□ ■ □ ",      };      var gol = new GameOfLife(board);      for (var generation : of(0, 1, 2)) {         gol.print();         System.out.println("\n");         gol = gol.makeNextGeneration();      }   } } `

Outputs:

```□ ■ □
□ ■ □
□ ■ □

□ □ □
■ ■ ■
□ □ □

□ ■ □
□ ■ □
□ ■ □
```

## JavaScript

Works with: SpiderMonkey
Works with: V8
`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();`
Output:
```---
3x3 Blinker over three turns.
---
Turn 1

XXX

---
Turn 2
X
X
X
---
Turn 3

XXX

---
10x6 Glider over five turns.
---
Turn 1

X
X
XXX

---
Turn 2

X X
XX
X

---
Turn 3

X
X X
XX

---
Turn 4

X
XX
XX

---
Turn 5

X
X
XXX

---
Random 5x10
---
Turn 1
XXXX
XX
X
XX X
XX
X   X
X
X   X
X
X  XX
---
Turn 2
XXXX
X  XX
XX X
XX
X X
X X
XX
XX
XX XX

---
Turn 3
XX X
X
X   X

XX X

XXX

---
Turn 4
X
X  X

X
X
X
---
Turn 5

XXX
```
Library: HTML5

Essentially the same as the above straight JavaScript but displayed in an HTML5 Canvas.

` <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>`
Output:

More functional style:

` 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); `

## jq

Works with: jq version 1.4

In this implementation, a "world" is simply a suitably constructed string as illustrated by world3 and world11 below. The "game" can be played either by creating separate frames (using frames(n)) or by calling animation(n; sleep) with sleep approximately equal to the number of milliseconds between refreshes.

`# Notes on the implementation: # 1. For efficiency, the implementation requires that the world#    has boundaries, as illustrated in the examples.# 2. For speed, the simulation uses the exploded string.# 3. The ASCII values of the "alive" and "empty" symbols are #    hardcoded: "." => 46; " " => 32# 4. To adjust the refresh rate, adjust the input to "spin". def lines: split("\n")|length; def cols: split("\n")[0]|length + 1;  # allow for the newline # Is there a "." (46) at [x,y] relative to position i,# assuming the width is w?# Input is an array; result is 0 or 1 so we can easily count the total.def isAlive(x; y; i; w): if .[i+ w*y + x] == 46 then 1 else 0 end; def neighborhood(i;w):  isAlive(-1; -1; i; w) + isAlive(0; -1; i; w) + isAlive(1; -1; i; w) +  isAlive(-1;  0; i; w)                        + isAlive(1;  0; i; w) +  isAlive(-1;  1; i; w) + isAlive(0;  1; i; w) + isAlive(1;  1; i; w) ; # The basic rules:def evolve(cell; sum) :   if   cell == 46 then if sum == 2 or sum == 3 then 46 else 32 end  elif cell == 32 then if sum == 3 then 46 else 32 end  else cell  end ; # [world, lines, cols] | next(w) => [world, lines, cols]def next:  .[0] as \$world | .[1] as \$lines | .[2] as \$w  | reduce range(0; \$world|length) as \$i    (\$world;      .[\$i] as \$c      | if \$c == 32 or \$c == 46 then           # updates are "simultaneous" i.e. relative to \$world, not "."           (\$world | neighborhood(\$i; \$w)) as \$sum           | evolve(\$c; \$sum) as \$next           | if \$c == \$next then . else .[\$i] = \$next end        else .        end )  | [., \$lines, \$w] ; `

Animation:

`# "clear screen":def cls: "\u001b[2J"; # Input: an integer; 1000 ~ 1 secdef spin:  reduce range(1; 500 * .) as \$i    (0; . + (\$i|cos)*(\$i|cos) + (\$i|sin)*(\$i|sin) )  |  "" ; # Animate n steps;# if "sleep" is non-negative then cls and # sleep about "sleep" ms between frames.def animate(n; sleep):  if n == 0 then empty  else (if sleep >= 0 then cls else "" end),       (.[0]|implode), n, "\n",       (sleep|spin),       ( next|animate(n-1; sleep) )  end ; # Input: a string representing the initial statedef animation(n; sleep):  [ explode, lines, cols] | animate(n; sleep) ; # Input: a string representing the initial statedef frames(n):  animation(n; -1); `

Examples:

`def world3:"+---+\n" +"|   |\n" +"|...|\n" +"|   |\n" +"+---+\n" ; def world11:"+-----------+\n" +"|           |\n" +"|   ..      |\n" +"|    ...    |\n" +"|      ..   |\n" +"|           |\n" +"+-----------+\n" ;`

`world3 | frames(3)`
Output:
`\$ jq -n -r -f Game_of_life.jq +---+|   ||...||   |+---+ 3    +---+| . || . || . |+---+ 2    +---+|   ||...||   |+---+ 1`

Animation example

`# Animation of 100 frames with approximately 1 second between each update:world11 | animation(100; 1000)`

## Julia

Works with: julia version 0.3.5

Using the CellularAutomata package: https://github.com/natj/CellularAutomata.jl

`julia> Pkg.add("CellularAutomata")INFO: Installing CellularAutomata v0.1.2INFO: Package database updated julia> using CellularAutomata julia> gameOfLife{T<:Int}(n::T, m::T, gen::T) = CA2d([3], [2,3], int(randbool(n, m)), gen)gameOfLife (generic function with 1 method) julia> gameOfLife(15, 30, 5)30x15x5 Cellular Automaton`
```       # ##   #   ######  ###  ### ##
#    ####    #  #  # ##  #####
## #   # ## ###   ## # # ##
#  # ##### # # # ## #     #  #
##  ## # #  ##     ###
#   #### ## ## ### # # # # # #
# ## ##### #  ##### # ## ### #
### #   ##        ####  ## #
#### #   ## ##   ### ###  ###
## ####   #######  #    ## #
# ## ##### ## #### # #####
##  ## ##### # # # #  #   # #
#   # ## ##   ## ##   ##### #
##   # #   #  # #############
#      ## #    # ###  ##  ##
```

```                # ###          #
#    # ## #      # #   #
###     # #      #     ##
#   #   # # #   # ##  ## ####
###       #       # # ## # #
#           ##     #     # # #
####        #
##  #
# ##    #      ##    #
#      #
#  #                #  ##   #
## #         #      # #     #
#       ##              #
##   #   #   ##     #       #
#  # ##
```

```             #    ##
##     #         #    # #
## # #  # ##    #  #    #####
#      #       ## #   # ##
##       ## #     #  ##
###           #     # ## #####
#  #
#   ##       ##
# ##
#             # #
# #                  # #
#####       ##       # #   ##
# #       #        #     ##
# #  ### #   ###
#    ### ####
```

```        ##      ###
#### # #  #             ## ##
##  #    ###     # ##  ##   #
#            ### ## ##  #
##      ###      #    ##
# #       #####      #### ####
##           ##       #  #####
# ####
# #           #
#            ##
## #                  # ##
#   #       ##      ##     ##
#  #       #    #
##### # ##    #             #
#   #   # #   #
```

```          ## # ## #
## #     #           ### ##
# #    ##      # ###    # #
## ##             #   ##  #
####      #       ##
##      #   #      ##
#        #          ## ##
#         ###  #          ####
# ####        ##
#           #  #
###                  ## ##   #
##          ##      ## #   ##
# # ##      # #      #     #
####### ####  ###
##    #
```

## Kotlin

This is based on the Go entry but has been altered in several respects.

In particular, it now allows for the blinker and glider patterns as well as an initially random pattern.

Also any cells beyond the boundary are now treated as dead as per the current task description.

The particular random pattern used now needs only 99 generations to reach stability.

`// version 1.2.0 import java.util.Random val rand = Random(0) // using a seed to produce same output on each run enum class Pattern { BLINKER, GLIDER, RANDOM } class Field(val w: Int, val h: Int) {    val s = List(h) { BooleanArray(w) }     operator fun set(x: Int, y: Int, b: Boolean) {        s[y][x] = b    }     fun next(x: Int, y: Int): Boolean {        var on = 0        for (i in -1..1) {            for (j in -1..1) {                if (state(x + i, y + j) && !(j == 0 && i == 0)) on++            }        }        return on == 3 || (on == 2 && state(x, y))    }     fun state(x: Int, y: Int): Boolean {        if ((x !in 0 until w) || (y !in 0 until h)) return false        return s[y][x]    }} class Life(val pattern: Pattern) {    val w: Int    val h: Int    var a: Field    var b: Field     init {        when (pattern) {            Pattern.BLINKER -> {                w = 3                h = 3                a = Field(w, h)                b = Field(w, h)                a[0, 1] = true                a[1, 1] = true                a[2, 1] = true            }             Pattern.GLIDER -> {                w = 4                h = 4                a = Field(w, h)                b = Field(w, h)                a[1, 0] = true                a[2, 1] = true                for (i in 0..2) a[i, 2] = true            }             Pattern.RANDOM -> {                w = 80                h = 15                a = Field(w, h)                b = Field(w, h)                for (i in 0 until w * h / 2) {                    a[rand.nextInt(w), rand.nextInt(h)] = true                }            }        }    }     fun step() {        for (y in 0 until h) {            for (x in 0 until w) {                b[x, y] = a.next(x, y)            }        }        val t = a        a = b        b = t    }     override fun toString(): String {        val sb = StringBuilder()        for (y in 0 until h) {            for (x in 0 until w) {                val c = if (a.state(x, y)) '#' else '.'                sb.append(c)            }            sb.append('\n')        }        return sb.toString()    }} fun main(args: Array<String>) {    val lives = listOf(        Triple(Life(Pattern.BLINKER), 3, "BLINKER"),        Triple(Life(Pattern.GLIDER), 4, "GLIDER"),        Triple(Life(Pattern.RANDOM), 100, "RANDOM")    )    for ((game, gens, title) in lives) {        println("\$title:\n")        repeat(gens + 1) {            println("Generation: \$it\n\$game")            Thread.sleep(30)            game.step()        }        println()    }}`
Output:

In the interests of brevity, only generations 0 and 100 are shown for the 'random' pattern:

```BLINKER:

Generation: 0
...
###
...

Generation: 1
.#.
.#.
.#.

Generation: 2
...
###
...

Generation: 3
.#.
.#.
.#.

GLIDER:

Generation: 0
.#..
..#.
###.
....

Generation: 1
....
#.#.
.##.
.#..

Generation: 2
....
..#.
#.#.
.##.

Generation: 3
....
.#..
..##
.##.

Generation: 4
....
..#.
...#
.###

RANDOM:

Generation: 0
....###....#.#....###...#.#..#.###...#..#......#.#..#####......######...##.#.##.
####..###.#....#.#####.##.....####.##..####.####.........#.#.###...#.##.#.#.....
..##.##.#.##...#..#.#..#.#.#.#..####.#...#..##....#..##.........#.#..#....#...#.
..##..#..##.#.#.....#.##.##...#####...##.##.....##...#.....##......###..##.#..##
......##..........#.#.#.......#..#.##.#.##....#.#...#.#.#.#.....#..#.......#.#.#
.#.#.....#####..#..##............#.#.#...###..#...##.....#..##...#.#.##.#..##...
.##.#.#.##.#..#####.....##..#.####..#.#..#...#.#..#...#.#.#.#....#.#..#.#.#..##.
.#..#..#.....#...###..###.....####..........##.##.####.....#..##..####..#...##..
..##.#...#.#..#.#....#..#...##.#..##.......#.#..##..##..##.#.....##.#......#.#.#
#.######..#.#.#.###.#....###.....#.....#..#......####.#.#..#....#...#.......#.#.
#...#..###.##....#.#..##..#..#.#.#...#..#....#....##...#..#..#.#....#...##....#.
#....##.......#.####.##..#....#.#....#....#######.#..####.#..#.#.##....#.#####..
...#.......##..##.##......##....#..#.####.......#..#.#..##.###.#.#.#.#..##.....#
#.##..####....#..#..##..#.#..#..#....#.###.##.....#.....##....#.#..#.##.....##..
...#.#..#.#.....###...#..##.#.....#......#...........###...#.#....#..#.#..##.#.#

Generation: 100
................................................................................
................................................................................
.........................................#......................................
......................#.................#.#.....................................
.##..................#.#................#.#.....................................
.##..................#.#.................#...#..................................
......................#.....................#.#.................................
...........................................#..#.................................
............................................##..................................
.............##.................................................................
............#..#................................................................
.##..........#.#.......................................##..........#............
.##...........#........................................##.........#.#...........
..........##.......................................................##...........
..........##....................................................................
```

## Lua

`function Evolve( cell )    local m = #cell    local cell2 = {}    for i = 1, m do        cell2[i] = {}        for j = 1, m do            cell2[i][j] = cell[i][j]        end    end     for i = 1, m do        for j = 1, m do            local count            if cell2[i][j] == 0 then count = 0 else count = -1 end            for x = -1, 1 do                for y = -1, 1 do                    if i+x >= 1 and i+x <= m and j+y >= 1 and j+y <= m and cell2[i+x][j+y] == 1 then count = count + 1 end                end            end            if count < 2 or count > 3 then cell[i][j] = 0 end            if count == 3 then cell[i][j] = 1 end        end    end     return cellend      m = 3                   -- number rows / columsnum_iterations = 10 cell = {}for i = 1, m do    cell[i] = {}    for j = 1, m do        cell[i][j] = 0    endend cell[2][2], cell[2][1], cell[2][3] = 1, 1, 1  for l = 1, num_iterations do    for i = 1, m do        for j = 1, m do            if cell[i][j] == 1 then io.write( "#" ) else io.write( " " ) end        end        io.write( "\n" )    end         cell = Evolve( cell )end     `

## Mathematica / Wolfram Language

Mathematica has cellular automaton functionality built in, so implementing Conway's Game of Life is a one-liner:

`CellularAutomaton[{224,{2,{{2,2,2},{2,1,2},{2,2,2}}},{1,1}}, startconfiguration, steps];`

Example of a glyder progressing 8 steps and showing the 9 frames afterwards as grids of hashes and dots:

`results=CellularAutomaton[{224,{2,{{2,2,2},{2,1,2},{2,2,2}}},{1,1}},{{{0,1,0},{0,0,1},{1,1,1}},0},8]; Do[Print[i-1];Print[Grid[results[[i]]/.{1->"#",0->"."}]];,{i,1,Length[results]}]`

gives back:

```0
.#...
..#..
###..
.....
.....

1
.....
#.#..
.##..
.#...
.....

2
.....
..#..
#.#..
.##..
.....

3
.....
.#...
..##.
.##..
.....

4
.....
..#..
...#.
.###.
.....

5
.....
.....
.#.#.
..##.
..#..

6
.....
.....
...#.
.#.#.
..##.

7
.....
.....
..#..
...##
..##.

8
.....
.....
...#.
....#
..###
```

## MATLAB

MATLAB has a builtin Game of Life GUI. Type
`life`
to run it. To view the code, type
`open(fullfile(matlabroot, 'toolbox', 'matlab', 'demos', 'life.m'))`

Here is an example code, more simple (runs the game of life for N generations in a square of side S) :

`function GoL(S, N) %    colormap copper; whitebg('black');    G= round(rand(S));    A = [S 1:S-1]; B = [2:S 1];    for k=1:N        Sum = G(A,:)+G(B,:)+G(:,B)+G(:,A)+G(A,B)+G(A,A)+G(B,B)+G(B,A);        G = double((G & (Sum == 2)) | (Sum == 3));        surf(G); view([0 90]); pause(0.001)    endend`

## Maxima

`life(A) := block(   [p, q, B: zerofor(A), s],   [p, q]: matrix_size(A),   for i thru p do (      for j thru q do (         s: 0,         if j > 1 then s: s + A[i, j - 1],         if j < q then s: s + A[i, j + 1],         if i > 1 then (            s: s + A[i - 1, j],            if j > 1 then s: s + A[i - 1, j - 1],            if j < q then s: s + A[i - 1, j + 1]         ),         if i < p then (            s: s + A[i + 1, j],            if j > 1 then s: s + A[i + 1, j - 1],            if j < q then s: s + A[i + 1, j + 1]         ),         B[i, j]: charfun(s = 3 or (s = 2 and A[i, j] = 1))      )   ),   B)\$  /* a glider */ L: matrix([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],          [0, 0, 0, 1, 0, 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, 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])\$ gen(A, n) := block(thru n do A: life(A), A)\$ gen(L, 4);matrix([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 1, 0, 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, 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])`

## Nim

Translation of: C
`import os, strutils, random randomize()var w, h: intif paramCount() >= 2:  w = parseInt(paramStr(1))  h = parseInt(paramStr(2))if w <= 0: w = 30if h <= 0: h = 30 # Initializevar univ, utmp = newSeq[seq[bool]](h)for y in 0..<h:  univ[y].newSeq w  utmp[y].newSeq w  for x in 0 .. <w:    if random(10) < 1:      univ[y][x] = true while true:  # Show  stdout.write "\e[H"  for y in 0..<h:    for x in 0..<w:      stdout.write if univ[y][x]: "\e[07m  \e[m" else: "  "    stdout.write "\e[E"  stdout.flushFile   # Evolve  for y in 0..<h:    for x in 0..<w:      var n = 0      for y1 in y-1..y+1:        for x1 in x-1..x+1:          if univ[(y1+h) mod h][(x1 + w) mod w]:            inc n       if univ[y][x]: dec n      utmp[y][x] = n == 3 or (n == 2 and univ[y][x])  swap(univ,utmp)   sleep 200`

## OCaml

`let get g x y =  try g.(x).(y)  with _ -> 0 let neighbourhood g x y =  (get g (x-1) (y-1)) +  (get g (x-1) (y  )) +  (get g (x-1) (y+1)) +  (get g (x  ) (y-1)) +  (get g (x  ) (y+1)) +  (get g (x+1) (y-1)) +  (get g (x+1) (y  )) +  (get g (x+1) (y+1))  let next_cell g x y =  let n = neighbourhood g x y in  match g.(x).(y), n with  | 1, 0 | 1, 1                      -> 0  (* lonely *)  | 1, 4 | 1, 5 | 1, 6 | 1, 7 | 1, 8 -> 0  (* overcrowded *)  | 1, 2 | 1, 3                      -> 1  (* lives *)  | 0, 3                             -> 1  (* get birth *)  | _ (* 0, (0|1|2|4|5|6|7|8) *)     -> 0  (* barren *) let copy g = Array.map Array.copy g let next g =  let width = Array.length g  and height = Array.length g.(0)  and new_g = copy g in  for x = 0 to pred width do    for y = 0 to pred height do      new_g.(x).(y) <- (next_cell g x y)    done  done;  (new_g) let print g =  let width = Array.length g  and height = Array.length g.(0) in  for x = 0 to pred width do    for y = 0 to pred height do      if g.(x).(y) = 0      then print_char '.'      else print_char 'o'    done;    print_newline()  done`

put the code above in a file named "life.ml", and then use it in the ocaml toplevel like this:

```# #use "life.ml";;
val get : int array array -> int -> int -> int = <fun>
val neighbourhood : int array array -> int -> int -> int = <fun>
val next_cell : int array array -> int -> int -> int = <fun>
val copy : 'a array array -> 'a array array = <fun>
val next : int array array -> int array array = <fun>
val print : int array array -> unit = <fun>

# let g = [|
[| 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; 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; 0; 0; 0; 0; 0; 0; 0; 0; |];
[| 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; |];
|] ;;
val g : int array array =
[|[|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; 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; 0; 0; 0; 0; 0; 0; 0; 0|];
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]|]
# print g;;
..........
..........
..........
..........
....ooo...
...ooo....
..........
..........
..........
..........
- : unit = ()
# print (next g) ;;
..........
..........
..........
.....o....
...o..o...
...o..o...
....o.....
..........
..........
..........
- : unit = ()
```

### A graphical version

This implementation has 6 starting patterns (most get quite large) and a random option, and you can set the grid size.

`let alive = 0let dead = 0xFFFFFF let iteration ib ob m n =   let rule = function 3,_ | 2,true -> alive | _ -> dead in   let f x y =      if x >= 0 && x < m && y >= 0 && y < n && ib.(x).(y) = alive      then 1 else 0 in   let count b q =      let a, c, p, r = b-1, b+1, q-1, q+1 in      f a p + f a q + f a r + f b p + f b r + f c p + f c q + f c r in   for i = 0 to m-1 do      for j = 0 to n-1 do         ob.(i).(j) <- rule (count i j, ib.(i).(j) = alive)      done   done let make_random w h bd =   Random.self_init ();   for i = 0 to w-1 do      for j = 0 to h-1 do         bd.(i).(j) <- if Random.bool () then alive else dead      done   done let set_cells a b cells w h bd =   let w', h' = w/2 - a, h/2 - b in   List.iter (fun (i,j) -> bd.(i+w').(j+h') <- alive) cells let make_blinker = set_cells 1 1 [(1,0); (1,1); (1,2)] let make_acorn =   set_cells 1 3 [(0,1); (1,3); (2,0); (2,1); (2,4); (2,5); (2,6)] let make_growth =   set_cells 2 3   [(0,6); (1,4); (1,6); (1,7); (2,4); (2,6); (3,4); (4,2); (5,0); (5,2)] let make_rabbits =   set_cells 1 3   [(0,0); (0,4); (0,5); (0,6); (1,0); (1,1); (1,2); (1,5); (2,1)] let make_engine =   set_cells (-100) (-100)   [(0,1); (0,3); (1,0); (2,1); (2,4); (3,3); (3,4); (3,5); (4,26); (4,27); (5,26); (5,27)] let make_line w h bd =   let w', h', l = w/2, h/2, w/3 in   for i = -l to l do bd.(i+w').(h') <- alive done let () =   let argc = Array.length Sys.argv in   let init =      let default () = (print_endline "Using random start"; make_random) in      if argc < 2 then default () else      match Sys.argv.(1) with         | "acorn" -> make_acorn         | "blinker" -> make_blinker         | "growth" -> make_growth         | "engine" -> make_engine         | "line" -> make_line         | "rabbits" -> make_rabbits         | "random" -> make_random         | "-h" -> Printf.printf            "Usage: %s [acorn|growth|blinker|engine|line|rabbits|random] width height\n" Sys.argv.(0);            exit 0         | _ -> default () in   let width = if argc > 2 then int_of_string Sys.argv.(2) else 300 in   let height = if argc > 3 then int_of_string Sys.argv.(3) else 300 in   let bd1 = Array.make_matrix width height dead in   let bd2 = Array.make_matrix width height dead in   let border = 5 in   let disp m = Graphics.draw_image (Graphics.make_image m) border border in   init width height bd1;   Graphics.open_graph (Printf.sprintf " %dx%d" (height+2*border) (width+2*border));   while true do      disp bd1;      iteration bd1 bd2 width height;      disp bd2;      iteration bd2 bd1 width height   done`
Compile with:
`ocamlopt -o life graphics.cmxa life.ml`
and run with
`./life acorn 250 250`
If you run the blinker it will probably blink too fast to see unless you choose a large grid size.

## ooRexx

`/* REXX ---------------------------------------------------------------* 07.08.2014 Walter Pachl Conway's Game of life  graphical* Input is a file containing the initial pattern.* The compute area is extended when needed*   (i.e., when cells are born outside the current compute area)* When computing the pattern sequence is complete, the graphical* output starts and continues until Cancel is pressed.* 10.08.2014 WP fixed the output of what.txt*--------------------------------------------------------------------*/ Parse Arg what speed If what='?' Then Do   Say 'Create a file containing the pattern to be processed'   Say 'named somename.in (octagon.in such as this for the octagon):'   Say '   **   '   Say '  *  *  '   Say ' *    * '   Say '*      *'   Say '*      *'   Say ' *    * '   Say '  *  *  '   Say '   **   '   Say 'Run the program by entering "rexx conlife somename [pause]"',                                                 'on the command line.'   Say '(pause is the amount of milliseconds between 2 pictures.',                                                     'default is 1000)'   Say 'A file somename.lst will be created.'   Say 'Hereafter you will see the pattern''s development',                                                     'in a new window.'   Say 'Press the Cancel button to end the presentation.'   Exit   End Parse Version interpreter '_' level '(' If interpreter<>'REXX-ooRexx' Then Do   Say interpreter  level   Say 'This program must be run with object Rexx.'   Exit   End If what='' Then what='octagon' If right(what,3)='.in' then   what=left(what,length(what)-3) infile=what'.in' If lines(infile)=0 Then Do   Say 'Input file' infile 'not found.'   Say 'Enter conlife ? for help.'   Exit   End If speed='' Then   speed=1000 .local~myspeed=speed  Call tl what --'type' what'.lst'  .local~title=what array=.local~myarrayData d = .drawDlg~new if d~initCode <> 0 then do    say 'The Draw dialog was not created correctly.  Aborting.'    return d~initCode end d~execute("SHOWTOP") return 0 ::requires "ooDialog.cls" ::class 'drawDlg' subclass UserDialog ::attribute interrupted unguarded ::method init   expose walterFont    forward class (super) continue   -- colornames:   --  1 dark red      7 light grey   13 red   --  2 dark green    8 pale green   14 light green   --  3 dark yellow   9 light blue   15 yellow   --  4 dark blue    10 white        16 blue   --  5 purple       11 grey         17 pink   --  6 blue grey    12 dark grey    18 turquoise    self~interrupted = .true    -- Create a font to write the nice big letters and digits   opts = .directory~new   opts~weight = 700   walterFont = self~createFontEx("Arial",14,opts)   walterFont = self~createFontEx("Courier",18,opts)    if \self~createcenter(200, 235,"Walter's Clock", , ,"System",14) then      self~initCode = 1 ::method defineDialog    self~createPushButton(/*IDC_PB_DRAW*/100,  0,  0,240,200,"DISABLED NOTAB")  -- The drawing surface.    self~createPushButton(IDCANCEL,160,220, 35, 12,,"&Cancel") ::method initDialog unguarded   expose x y dc myPen change al. fid nn what array   change = 0   x = self~factorx   y = self~factory   dc = self~getButtonDC(100)   myPen   = self~createPen(1,'solid',0)   t    = .TimeSpan~fromMicroSeconds(500000) -- .5 seconds   msg  = .Message~new(self,'life')   alrm = .Alarm~new(t, msg)   array=.local~myArrayData   Do s=1 to array~items     al.s=array[s]     Parse Var al.s ' == ' al.s     End   nn=s-2   --say 'nn'nn   Call lineout fid ::method interrupt unguarded    self~interrupted = .true ::method cancel unguarded   -- Stop the drawing program and quit.   expose x y   self~hide   self~interrupted = .true   return self~cancel:super ::method leaving unguarded  -- Best place to clean up resources   expose dc myPen walterFont    self~deleteObject(myPen)   self~freeButtonDC(/*IDC_PB_DRAW*/100,dc)   self~deleteFont(walterFont) ::method life unguarded                           /* draw individual pixels */   expose x y dc myPen change walterFont al. nn what   mx = trunc(20*x); my = trunc(20*y); size = 400    curPen = self~objectToDC(dc, myPen)    -- Select the nice big letters and digits into the device context to use to   -- to write with:   curFont = self~fontToDC(dc, walterFont)    -- Create a white brush and select it into the device to paint with.   whiteBrush = self~createBrush(10)   curBrush   = self~objectToDC(dc, whiteBrush)    -- Paint the drawing area surface with the white brush   self~rectangle(dc, 1, 1, 500, 600, 'FILL')   self~writeDirect(dc,  10, 20,'Conway''s Game of Life')   self~writeDirect(dc,  10, 40,.local~title)   self~writeDirect(dc,  10,460,'Walter Pachl, 8 Aug 2014')   dx=.local~dxval   dy=.local~dyval   do s=1 By 1 until self~interrupted     self~transparentText(dc)     self~interrupted = .false     sm=s//nn+1     If s>1 Then Do       ali=al.sb       Do While ali<>''         Parse Var ali x ',' y ali         zxa=(x+dx)*10         zya=(y+dy)*10         self~draw_square(dc,zxa,zya,3,10)         End       End     self~draw_square(dc, 380, 10,100,10)     self~writeDirect(dc, 340, 20,time())     self~writeDirect(dc, 340, 40,right(sm,2) 'of' right(nn,2))     ali=al.sm     Do While ali<>''       Parse Var ali x ',' y ali       zxa=(x+dx)*10       zya=(y+dy)*10       self~draw_square(dc,zxa,zya,3,5)       End --    self~interrupted = .true     sb=sm     self~objectToDC(dc, curPen)     self~objectToDC(dc, curBrush)     call msSleep .local~myspeed   --self~pause     End ::method pause   j = msSleep(10) ::method draw_square  Use Arg dc, x, y, d, c  Do zx=x-d to x+d    Do zy=y-d to y+d      self~drawPixel(dc, zx, zy, c)      End    End ::method quot  Parse Arg x,y  If y=0 Then Return '???'  Else Return x/y ::routine tl/* REXX ---------------------------------------------------------------* 02.08.2014 Walter Pachl* Input is a file containing the initial pattern* The compute area is extended when needed*   (cells are born outside the current compute area)* The program stops when the picture shown is the same as the first*   or equal to the previous one*--------------------------------------------------------------------*/Parse Arg fIf f='' Then f='bipole'fid=f'.in'oid=f'.txt'; 'erase' oidoil=f'.lst'; 'erase' oildebug=0If debug Then Do  dbg=f'.xxx'; 'erase' dbg  Endml=0l.=''ol.=''Parse Value '10 10' With xb ybxc=copies(' ',xb)Do ri=yb+1 By 1 While lines(fid)>0  l.ri=xc||linein(fid)  ml=max(ml,length(strip(l.ri,'T')))  Endri=ri-1ml=ml+xbri=ri+ybyy=ria.=' 'b.=' 'm.=''x.=''list.=''Parse Value 1 ml 1 yy With xmi xma ymi ymaParse Value '-10 30 -10 30' With xmi xma ymi ymaParse Value '999 -999  999 -999 999 -999 999 -999',       With xmin xmax ymin ymax xlo xhi  ylo yhiDo y=1 To yy  z=yy-y+1  l=l.z  Do x=1 By 1 While l<>''    Parse Var l c +1 l    If c='*' Then      a.x.z='*'    End  EndCall showDo step=1 To  60  Call store  If step>1 & is_equal(step,1) Then Leave  If step>1 & is_equal(step,step-1) Then Leave  Call show_neighbors  Do y=yma To ymi By -1    ol=format(x,3)' '    Do x=xmi To xma      neighbors=neighbors(x,y)      If a.x.y=' ' Then Do             /* dead cell                  */        If neighbors=3 Then Do          b.x.y='*'                    /*  gets life                 */          mmo=xmi xma ymi yma          xmi=min(xmi,x-1)          xma=max(xma,x+1)          ymi=min(ymi,y-1)          yma=max(yma,y+1)          mm=xmi xma ymi yma          If mm<>mmo Then            Call debug mmo '1->' mm          End        Else                           /* life cell                  */          b.x.y=' '                    /*  remains dead              */        End      Else Do                          /* life cell                  */        If neighbors=2 |,           neighbors=3 Then Do          b.x.y='*'  /*  remains life              */          mmo=xmi xma ymi yma          xmi=min(xmi,x-1)          xma=max(xma,x+1)          ymi=min(ymi,y-1)          yma=max(yma,y+1)          mm=xmi xma ymi yma          If mm<>mmo Then            Call debug mmo '2->' mm          End        Else          b.x.y=' '  /*  dies                      */        End      End    End  /* b. is the new state and is now copied to a.                     */  Do y=yma To ymi By -1    Do x=xmi To xma      a.x.y=b.x.y      End    End  End/* Output name and all states                                        */Call lineout oid,' 'fst=' +'                                /* top and bottom border      */sb=' +'                                /* top and bottom border      */Do s=1 To step  st=st||'-'right(s,2,'-')||copies('-',xmax-xmin-2)'+'  sb=sb||copies('-',xmax-xmin+1)'+'  Endarray=.array~newDo y=ymin To ymax  Do s=1 To step    Do x=xmin To xmax      If substr(m.s.y,x,1)='*' Then Do        xlo=min(xlo,x)        xhi=max(xhi,x)        ylo=min(ylo,y)        yhi=max(yhi,y)        End      End    End  EndDo y=ymin To ymax  ol=''  Do s=1 To step    Do x=xmin To xmax      If substr(m.s.y,x,1)='*' Then Do        list.s=list.s (x-xlo+1)','||(y-ylo+1)        End      End    array[s]=s '-' words(list.s) '==' list.s    End  --Call lineout oid,ol '|'  .local~myArrayData=array  Endheight=yhi-ylo+1width=xhi-xlo+1.local~dxval=(48-width)%2.local~dyval=(48-height)%2Call o st                        /* top border                 */xl.='|'Do y=ymax To ymin By -1  Do s=1 To step    xl.y=xl.y||substr(ol.s.y,xmin,xmax-xmin+1)'|'    End  EndDo y=ymax To ymin By -1  Call o ' 'xl.y  EndCall o sb                    /* bottom border              */Call lineout oidSay 'frames are shown in' oidIf debug Then Do  Say 'original area' 1 ml '/' 1 yy  Say 'compute area ' xmi xma '/' ymi yma  Say 'used area    ' xlo xhi '/' ylo yhi  EndDo s=1 To step  call lineout oil,s '==>' words(list.s) '=='  list.s  EndReturn o: Parse Arg lili   Call lineout oid,lili   Return set: Parse Arg x,y     a.x.y='*'     Return neighbors: Procedure Expose a. debug  Parse Arg x,y  neighbors=0  do xa=x-1 to x+1    do ya=y-1 to y+1      If xa<>x | ya<>y then        If a.xa.ya='*' Then          neighbors=neighbors+1      End    End  Return neighbors store:/* store current state (a.) in lines m.step.*                        */Do y=yma To ymi By -1  ol=''  Do x=xmi To xma    z=a.x.y    ol=ol||z    End  x.step.y=ol  If ol<>'' then Do    ymin=min(ymin,y)    ymax=max(ymax,y)    p=pos('*',ol)    q=length(strip(ol,'T'))    If p>0 Then      xmin=min(xmin,p)    xmax=max(xmax,q)    End  m.step.y=ol  ol.step.y=ol  --If pos('*',ol)>0 Then Do  --  Say '====>' right(step,2) right(y,3) '>'ol'<'  xmin xmax  --  Say '              'copies('1234567890',3)  --  End  EndReturn is_equal:/* test ist state a.b is equal to state a.a                          */  Parse Arg a,b  Do y=yy To 1 By -1    If x.b.y<>x.a.y Then      Return 0    End  Return 1 show: Procedure Expose dbg a. yy ml debugDo y=-5 To 13  ol='>'  Do x=-5 To 13    ol=ol||a.x.y    End  Call debug ol  EndReturn show_neighbors: Procedure Expose a. xmi xma ymi yma dbg debug  Do y=yma To ymi By -1    ol=format(y,3)' '    Do x=xmi To xma      ol=ol||neighbors(x,y)      End    Call debug ol    End  Return debug:  If debug Then    Return lineout(dbg,arg(1))  Else    Return `
Output:
```blinker.txt
+--1+--2+--3+
|   | * |   |
|***| * |***|
|   | * |   |
+---+---+---+
1 ==> 3 ==  1,2 2,2 3,2
2 ==> 3 ==  2,1 2,2 2,3
3 ==> 3 ==  1,2 2,2 3,2
```

## Oz

`declare  Rules = [rule(c:1 n:[0 1]             new:0)  %% Lonely           rule(c:1 n:[4 5 6 7 8]       new:0)  %% Overcrowded           rule(c:1 n:[2 3]             new:1)  %% Lives           rule(c:0 n:[3]               new:1)  %% It takes three to give birth!           rule(c:0 n:[0 1 2 4 5 6 7 8] new:0)  %% Barren          ]   Blinker = ["..."             "###"             "..."]   Toad = ["...."          ".###"          "###."          "...."]   Glider = [".#.........."            "..#........."            "###........."            "............"            "............"            "............"            "............"            "............"            "............"            "............"            "............"]   Init = Blinker  MaxGen = 2   %% G(i) -> G(i+1)  fun {Evolve Gi}     fun {Get X#Y}        Row = {CondSelect Gi Y unit}     in        {CondSelect Row X 0} %% cells beyond boundaries are dead (0)     end     fun {GetNeighbors X Y}        {Map [X-1#Y-1  X#Y-1  X+1#Y-1              X-1#Y           X+1#Y              X-1#Y+1  X#Y+1  X+1#Y+1]         Get}     end  in     {Record.mapInd Gi      fun {\$ Y Row}         {Record.mapInd Row          fun {\$ X C}             N = {Sum {GetNeighbors X Y}}          in             for Rule in Rules return:Return do                if C == Rule.c andthen {Member N Rule.n} then                   {Return Rule.new}                end             end          end}      end}  end   %% For example: [".#"  %%                "#."] -> grid(1:row(1:0 2:1) 2:row(1:1 2:0))  fun {ReadG LinesList}     {List.toTuple grid      {Map LinesList       fun {\$ Line}          {List.toTuple row           {Map Line            fun {\$ C}               if C == &. then 0               elseif C == &# then 1               end            end}}       end}}  end   %% Inverse to ReadG  fun {ShowG G}     {Map {Record.toList G}      fun {\$ Row}         {Map {Record.toList Row}          fun {\$ C}             if C == 0 then &.             elseif C == 1 then &#             end          end}      end}  end   %% Helpers  fun {Sum Xs} {FoldL Xs Number.'+' 0} end  fun lazy {Iterate F V} V|{Iterate F {F V}} end   G0 = {ReadG Init}  Gn = {Iterate Evolve G0}in  for     Gi in Gn     I in 0..MaxGen  do     {System.showInfo "\nGen. "#I}     {ForAll {ShowG Gi} System.showInfo}  end`

## PARI/GP

Basic implementation; prints a matrix representing the state of the game directly. Supports large games but this example uses only the required 3 X 3 blinker.

`step(M)={  my(N=M,W=matsize(M)[1],L=#M,t);  for(l=1,W,for(w=1,L,    t=sum(i=l-1,l+1,sum(j=w-1,w+1,if(i<1||j<1||i>W||j>L,0,M[i,j])));    N[l,w]=(t==3||(M[l,w]&&t==4))  ));  N};M=[0,1,0;0,1,0;0,1,0];for(i=1,3,print(M);M=step(M))`

## Pascal

Uses crt for console output. Can make use of a "torus"-world. Optimized for speed on a Haswell CPU (without PrintGen ~ 8.5 Cpu-cyles/coordinate )

`program Gol;// Game of life{\$IFDEF FPC}   //save as gol.pp/gol.pas  {\$Mode delphi}{\$ELSE}  //for Delphi save as gol.dpr  {\$Apptype Console}{\$ENDIF}uses  crt; const  colMax = 76;  rowMax = 22;  dr = colMax+2; // element count of one row   cDelay = 20;  // delay in ms (*expand field by one row/column before and afterfor easier access no special treatment of torus*) type  tFldElem  = byte;//0..1  tpFldElem = ^tFldElem;  tRow = array[0..colMax+1] of tFldElem;  tpRow = ^tRow;  tBoard = array[0..rowMax+1] of tRow;  tpBoard = ^tBoard;  tpBoards = array[0..1] of tpBoard; type  tIntArr = array[0..2*dr+2] of tFldElem;  tpIntArr = ^tIntArr; var  aBoard,bBoard : tBoard;  pBoards :tpBoards;  gblActBoard : byte;  gblUseTorus :boolean;  gblGenCnt   : integer; procedure PrintGen;const  cChar: array[0..1] of char = (' ','#');var  p0 : tpIntArr;  col,row: integer;  s : string[colMax];begin  setlength(s,colmax);  gotoxy(1,1);  writeln(gblGenCnt:10);  For row := 1 to rowMax do  begin    p0 := @pBoards[gblActBoard]^[row,0];;    For col := 1 to colMax do      s[col] := cChar[p0[col]];    writeln(s);  end;  delay(cDelay);end; procedure Init0(useTorus:boolean);begin  gblUseTorus := useTorus;  gblGenCnt := 0;  fillchar(aBoard,SizeOf(aBoard),#0);  pBoards[0] := @aBoard;  pBoards[1] := @bBoard;  gblActBoard := 0;   clrscr;end; procedure InitRandom(useTorus:boolean);var  col,row : integer;begin  Init0(useTorus);  For row := 1 to rowMax do    For col := 1 to colMax do      aBoard[row,col]:= tFldElem(random>0.9);end; procedure InitBlinker(useTorus:boolean);var  col,row : integer;begin  Init0(useTorus);  For col := 1 to colMax do  begin    IF (col+2) mod 4 = 0 then      begin      For row := 1 to rowmax do        IF row mod 4 <> 0 then          aBoard[row,col]:= 1;      end;  end;end; procedure Torus;var  p0 : tpIntArr;  row: integer;begin  //copy column 1-> colMax+1 and colMax-> 0  p0 := @pBoards[gblActBoard]^[1,0];  For row := 1 to rowMax do    begin    p0^[0] := p0^[colMax];    p0^[colmax+1] := p0^[1];    //next row    p0 := Pointer(PtrUint(p0)+SizeOf(tRow));    end;  //copy row  1-> rowMax+1  move(pBoards[gblActBoard]^[1,0],pBoards[gblActBoard]^[rowMax+1,0],sizeof(trow));  //copy row  rowMax-> 0  move(pBoards[gblActBoard]^[rowMax,0],pBoards[gblActBoard]^[0,0],sizeof(trow));end; function Survive(p: tpIntArr):tFldElem;//p points to actual_board [row-1,col-1]//calculates the sum of alive around [row,col] aka p^[dr+1]//really fast using fpc 2.6.4 no element on stackconst  cSurvives : array[boolean,0..8] of byte =              //0,1,2,3,4,5,6,7,8     sum of alive neighbours              ((0,0,0,1,0,0,0,0,0),   {alive =false 1->born}               (0,0,1,1,0,0,0,0,0));  {alive =true  0->die }var  sum : integer;begin  // row above  // sum := byte(aBoard[row-1,col-1])+byte(aBoard[row-1,col])+byte(aBoard[row-1,col+1]);  sum :=     integer(p^[     0])+integer(p^[     1])+integer(p^[     2]);  sum := sum+integer(p^[  dr+0])                    +integer(p^[  dr+2]);  sum := sum+integer(p^[2*dr+0])+integer(p^[2*dr+1])+integer(p^[2*dr+2]);  survive := cSurvives[boolean(p^[dr+1]),sum];end; procedure NextGen;var  p0,p1 : tpFldElem;  row: NativeInt;  col :NativeInt;begin  if gblUseTorus then    Torus;  p1 := @pBoards[1-gblActBoard]^[1,1];  //One row above and one column before because of survive  p0 := @pBoards[  gblActBoard]^[0,0];  For row := rowMax-1 downto 0 do  begin    For col := colMax-1 downto 0 do    begin      p1^ := survive(tpIntArr(p0));      inc(p0);      inc(p1);    end;    // jump over the borders    inc(p1,2);    inc(p0,2);  end;  //aBoard := bBoard;  gblActBoard :=1-gblActBoard;  inc(gblGenCnt);end; begin  InitBlinker(false);  repeat    PrintGen;    NextGen;  until keypressed;  PrintGen;end. `

## Perl

This a perl example the simulates Conway's life starting with a random grid of the given size for the given number of steps. Example:

```life.pl numrows numcols numiterations
life.pl 5 10 15
```

would do 15 iterations over 5 rows and 10 columns.

`my (\$width, \$height, \$generations) = @ARGV; my \$printed; sub generate {   (map       {[ (map { rand () < 0.5 } 1 .. \$width), 0 ]}       1 .. \$height),   [(0) x (\$width + 1)];} sub nexgen {   my @prev = map {[@\$_]} @_;   my @new = map {[ (0) x (\$width + 1) ]} 0 .. \$height;   foreach my \$row ( 0 .. \$height - 1 ) {       foreach my \$col ( 0 .. \$width - 1 ) {           my \$val =             \$prev[ \$row - 1 ][ \$col - 1 ] +             \$prev[ \$row - 1 ][ \$col     ] +             \$prev[ \$row - 1 ][ \$col + 1 ] +             \$prev[ \$row     ][ \$col - 1 ] +             \$prev[ \$row     ][ \$col + 1 ] +             \$prev[ \$row + 1 ][ \$col - 1 ] +             \$prev[ \$row + 1 ][ \$col     ] +             \$prev[ \$row + 1 ][ \$col + 1 ];           \$new[\$row][\$col] =               ( \$prev[\$row][\$col] && \$val == 2 || \$val == 3 );       }   }   return @new;} sub printlife {   my @life = @_;   if (\$printed) {	# Move the cursor up to print over prior generation.	print "\e[1A" x \$height;   }   \$printed = 1;   foreach my \$row ( 0 .. \$height - 1 ) {       foreach my \$col ( 0 .. \$width - 1 ) {           print(\$life[\$row][\$col]             ? "\e[33;45;1m \e[0m"             : "\e[1;34;1m \e[0m");       }       print "\n";   }} my @life = generate;print "Start\n";printlife @life;foreach my \$stage ( 1 .. \$generations ) {   sleep 1;   print "Generation \$stage\n\e[1A";   @life = nexgen @life;   printlife @life;}print "\n";`
Another version, takes up the whole area of your terminal. Using warping edges.
`my \$w = `tput cols` - 1;my \$h = `tput lines` - 1;my \$r = "\033[H"; my @universe = map([ map(rand(1) < .1, 1 .. \$w) ], 1 .. \$h);sub iterate {	my @new = map([ map(0, 1 .. \$w) ], 1 .. \$h);	for my \$i (0 .. \$h - 1) {	for my \$j (0 .. \$w - 1) {		my \$neighbor = 0;		for (	[-1, -1], [-1, 0], [-1, 1],			[ 0, -1], 	   [ 0, 1],			[ 1, -1], [ 1, 0], [ 1, 1] )		{			my \$y = \$_->[0] + \$i;			my \$x = \$_->[1] + \$j;			\$neighbor += \$universe[\$y % \$h][\$x % \$w];			last if \$neighbor > 3;		} 		\$new[\$i][\$j] = \$universe[\$i][\$j]			? (\$neighbor == 2 or \$neighbor == 3)			: \$neighbor == 3;	}}	@universe = @new;} while(1) {	print \$r;	print map((map(\$_ ? "#" : " ", @\$_), "\n"), @universe);	iterate;}`

## Perl 6

`class Automaton {    subset World of Str where {        .lines>>.chars.unique == 1 and m/^^<[.#\n]>+\$\$/    }    has Int (\$.width, \$.height);    has @.a;     multi method new (World \$s) {        self.new:            :width(.pick.chars), :height(.elems),            :a( .map: { [ .comb ] } )                given \$s.lines.cache;    }     method gist { join "\n", map { .join }, @!a }     method C (Int \$r, Int \$c --> Bool) {        @!a[\$r % \$!height][\$c % \$!width] eq '#';    }    method N (Int \$r, Int \$c --> Int) {        +grep ?*, map { self.C: |@\$_ },            [ \$r - 1, \$c - 1], [ \$r - 1, \$c ], [ \$r - 1, \$c + 1],            [ \$r    , \$c - 1],                 [ \$r    , \$c + 1],            [ \$r + 1, \$c - 1], [ \$r + 1, \$c ], [ \$r + 1, \$c + 1];    }     method succ {        self.new: :\$!width, :\$!height,            :a(                gather for ^\$.height -> \$r {                    take [                        gather for ^\$.width -> \$c {                            take                            (self.C(\$r, \$c) == 1 && self.N(\$r, \$c) == 2|3)                                || (self.C(\$r, \$c) == 0 && self.N(\$r, \$c) == 3)                            ?? '#' !! '.'                        }                    ]                }            )    }} my Automaton \$glider .= new: q:to/EOF/;    ............    ............    ............    .......###..    .###...#....    ........#...    ............    EOF  for ^10 {    say \$glider++;    say '--';}`

## Phix

Translation of: basic256
Library: pGUI
`---- demo\rosetta\Conways_Game_of_Life.exw--include pGUI.e Ihandle dlg, canvas, hTimercdCanvas cddbuffer, cdcanvas sequence c = {},    -- cells         cn,        -- new cells         cl         -- last cells procedure draw(integer what)integer lx = length(c)integer ly = length(c[1])integer x = floor(lx/2)integer y = floor(ly/2)    switch what do        case ' ' : -- Clear            c = sq_mul(c,0)        case '+' : -- Blinker            {c[x-1,y],c[x,y],c[x+1,y]} @= 1        case 'G' : -- Glider            {                      c[x+1,y+4],             c[x+2,y+2],           c[x+2,y+4],                        c[x+3,y+3],c[x+3,y+4]} @= 1        case 'T' : -- Thunderbird            {c[x-1,y+1],c[x,y+1],c[x+1,y+1],             c[x,y+3],c[x,y+4],c[x,y+5]} @= 1        case 'X' : -- Cross            for x=2 to lx-1 do                y = floor(ly*x/lx)                if y>1 then                    {c[x,y],c[x,ly-y]} @= 1                end if            end for    end switchend procedure atom t0 = time() function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)integer {width, height} = IupGetIntInt(canvas, "DRAWSIZE")integer colour, cnxy, live, alive = 0    if length(c)!=height    or length(c[1])!=width then        c = repeat(repeat(0,height),width)        cn = c        cl = c        draw('X')    end if    cdCanvasActivate(cddbuffer)    for y=2 to height-1 do        for x=2 to width-1 do            integer  xm1 = x-1,              xp1 = x+1,                     ym1 = y-1,              yp1 = y+1            cnxy = c[xm1,ym1] + c[x,ym1] + c[xp1,ym1] +                    c[xm1,y]              + c[xp1,y] +                   c[xm1,yp1] + c[x,yp1] + c[xp1,yp1]            if c[x,y]=1 then                live = (cnxy>=2 and cnxy<=3)            else                 live = (cnxy=3)            end if            cn[x,y] = live            alive += live            if live then                if c[x,y] then                    colour = iff(cl[x,y]?CD_PURPLE  -- adult                                        :CD_GREEN)  -- newborn                else                     colour = iff(cl[x,y]?CD_RED     -- old                                        :CD_YELLOW) -- shortlived                end if             else                colour = CD_BLACK            end if            cdCanvasPixel(cddbuffer, x, y, colour)         end for    end for    cdCanvasFlush(cddbuffer)    if not alive then        IupStoreAttribute(hTimer, "RUN", "NO")        IupStoreAttribute(dlg, "TITLE", "died")--  elsif cl=cn then -- (made blinker stable)    elsif c=cn then        IupStoreAttribute(hTimer, "RUN", "NO")        IupStoreAttribute(dlg, "TITLE", "stable")    else        cl = c        c = cn        IupSetStrAttribute(dlg, "TITLE", "%3.2f", {time()-t0})    end if    return IUP_DEFAULTend function function map_cb(Ihandle ih)    cdcanvas = cdCreateCanvas(CD_IUP, ih)    cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)    cdCanvasSetBackground(cddbuffer, CD_WHITE)    cdCanvasSetForeground(cddbuffer, CD_RED)    return IUP_DEFAULTend function function key_cb(Ihandle /*ih*/, atom c)    if    find(c," ")   then draw(' ')  -- Clear    elsif find(c,"+bB") then draw('+')  -- Blinker    elsif find(c,"gG")  then draw('G')  -- Glider    elsif find(c,"tT")  then draw('T')  -- Thunderbird    elsif find(c,"xX")  then draw('X')  -- Cross    end if    IupStoreAttribute(hTimer, "RUN", "YES")    IupStoreAttribute(dlg, "TITLE", "Life")    if c=K_ESC then return IUP_CLOSE end if    return IUP_CONTINUEend function function timer_cb(Ihandle /*ih*/)    IupUpdate(canvas)    return IUP_IGNOREend function procedure main()    IupOpen()     canvas = IupCanvas(NULL)    IupSetAttribute(canvas, "RASTERSIZE", "200x200") -- initial size    IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))     dlg = IupDialog(canvas)    IupSetAttribute(dlg, "TITLE", "Conway's Game of Life")    IupSetCallback(dlg, "K_ANY",     Icallback("key_cb"))    IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))     hTimer = IupTimer(Icallback("timer_cb"), 30)     IupMap(dlg)    IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation    IupShowXY(dlg,IUP_CENTER,IUP_CENTER)    IupMainLoop()    IupClose()end procedure main()`

## PicoLisp

This example uses 'grid' and 'disp' from "lib/simul.l". These functions maintain an array of multiply linked objects, and are also used in the chess program and other games in the distribution.

`(load "@lib/simul.l") (de life (DX DY . Init)   (let Grid (grid DX DY)      (for This Init         (=: life T) )      (loop         (disp Grid NIL            '((This) (if (: life) "X " "  ")) )         (wait 1000)         (for Col Grid            (for This Col               (let N  # Count neighbors                  (cnt                     '((Dir) (get (Dir This) 'life))                     (quote                        west east south north                        ((X) (south (west X)))                        ((X) (north (west X)))                        ((X) (south (east X)))                        ((X) (north (east X))) ) )                  (=: next  # Next generation                     (if (: life)                        (>= 3 N 2)                        (= N 3) ) ) ) ) )         (for Col Grid  # Update            (for This Col               (=: life (: next)) ) ) ) ) ) (life 5 5  b3 c3 d3)`

Output:

``` 5
4
3   X X X
2
1
a b c d e
5
4     X
3     X
2     X
1
a b c d e
5
4
3   X X X
2
1
a b c d e```

## PL/I

`(subscriptrange):Conway: procedure options (main);        /* 20 November 2013 */   /* A grid of (1:100, 1:100) is desired; the array GRID is defined as (0:101, 0:101), */   /* to satisfy the requirement that elements off-grid are zero.                       */   declare n fixed binary; /* grid size) */    put ('What grid size do you want?');   get (n);   put skip list ('Generating a grid of size ' || trim(n) ); begin;   declare grid (0:n+1,0:n+1) bit(1) initial ((*) '0'b);   declare new  (0:n+1,0:n+1) bit(1);   declare cell(3,3) defined grid(1sub-2+i, 2sub-2+j) bit (1);   declare (i, j, k) fixed binary;    /* Initialize some cells. */   grid(2,2) = '1'b; grid(2,3) = '1'b; grid(2,4) = '1'b;    /* Print the initial state. */   put list ('Initial pattern:');   do i = 1 to n;      put skip;      do j = 1 to n;         put edit (grid(i,j)) (b(1));      end;   end;    do k = 1 to 4;      /* Do one generation of life */      new = '0'b;      /* For each C, the center of a 3 x 3 cell matrix. */      do i = 1 to n;         do j = 1 to n;            if grid(i,j) then               select (sum(cell)-1);                  when (0,1)       new(i,j) = '0'b;                  when (4,5,6,7,8) new(i,j) = '0'b;                  when (2,3)       new(i,j) = '1'b;               end;            else               select (sum(cell));                  when (3)         new(i,j) = '1'b;                  otherwise        new(i,j) = '0'b;               end;         end;      end;      grid = new; /* Update GRID with the new generation. */       /* Print the generation. */      put skip(2) list ('Generation ' || trim(k));      do i = 1 to n;         put skip;         do j = 1 to n;            put edit (grid(i,j)) (b(1));         end;      end;   end;end;	end Conway;`

Results:

```What grid size do you want?

Generating a grid of size 5

Initial conditions:
00000
01110
00000
00000
00000

Generation 1
00100
00100
00100
00000
00000

Generation 2
00000
01110
00000
00000
00000

Generation 3
00100
00100
00100
00000
00000

Generation 4
00000
01110
00000
00000
00000
```

## PostScript

`%!PS-Adobe-3.0%%BoundingBox: 0 0 400 400 /size 400 def realtime srand/rand1 { rand 2147483647 div } def /m { moveto } bind def/l { rlineto} bind def/drawboard {        0 1 n 1 sub { /y exch def        0 1 n 1 sub { /x exch def                board x get y get 1 eq {                        x c mul y c mul m                        c 0 l 0 c l c neg 0 l                        closepath fill                } if        } for } for} def /r1n { dup 0 lt { n add } if dup n ge { n sub } if } def/neighbors { /y exch def /x exch def 0        y 1 sub 1 y 1 add { r1n /y1 exch def        x 1 sub 1 x 1 add { r1n /x1 exch def                board x1 get y1 get add        } for } for        board x get y get sub} def /iter {        /board        [0 1 n 1 sub { /x exch def        [0 1 n 1 sub { /y exch def                x y neighbors                board x get y get                0 eq    { 3 eq {1}{0} ifelse }                        { dup 2 eq exch 3 eq or {1}{0} ifelse } ifelse        } for ]        } for ] def} def /n 200 def/initprob .15 def/c size n div def/board [ n {[ n { rand1 initprob le {1}{0} ifelse } repeat]} repeat ] def 1000 { drawboard showpage iter } repeat%%EOF`

## Prolog

` %----------------------------------------------------------------------%% GAME OF LIFE                                                         %%                                                                      %% Adapt the prediacte grid_size according to the grid size of the      %%   start pic.                                                         %% Modify the number of generations.                                    %% Run PROLOG and type '[gol].' to compile the source file.             %% Create a subfolder <subfolder> where your gol.pl resides and place   %%   your initial PBM '<filename>0.0000.pbm' inside <subfolder>.        %% You need to adjust the number of zeros after <filename>. The         %%   sequence of zeros after '0.' must be as long as the number of      %%   generations. This is important to obtain a propper animation.      %%   (Maybe someone knows a better solution for this)                   %% Start PROLOG and run                                                 %%                                                                      %%                cellular('./<subloder>/<filename>').                  %%                                                                      %% Inside <subfolder> run the following shell command                   %%                                                                      %%         convert -delay 25 -loop 0 <filename>* <filename>.gif         %%                                                                      %%----------------------------------------------------------------------% %----------------------------------------------------------------------%% Special thanks to René Thiemann improving the runtime performance.   %%----------------------------------------------------------------------% % Size of the 2D gridgrid_size(300).% Number of generationsgenerations(1000). %----------------------------------------------------------------------%% Main procedure: generate n generations of life and store each file.  %%   cellular( +File path )                                             %%----------------------------------------------------------------------%cellular(I) :-	grid_size(GS),	string_concat(I,'0.0000.pbm',I1),	read_pbm(I1,GS,M),	cellular_(I,M,GS,1), !. cellular_(I,M,GS,N) :-	N1 is N+1,	format(atom(N0),'~4d',N),	string_concat(I,N0,I1),	string_concat(I1,'.pbm',I2),	step(M,M1),		write_pbm(M1,GS,I2), !,	cellular_(I,M1,GS,N1).cellular_(_,_,_,GE) :-	generations(GE),!. %----------------------------------------------------------------------%% Apply the Game Of Life rule set to every cell.                       %%   step( +OldMatrix, +NewMatrix )                                     %%                                                                      %%  ss | s | ... | s       ss ... step_ss                               %% ----+---+-----+---      s  ... step_s                                %%  ii | i | ... | i       ii ... step_ii                               %% ----+---+-----+---      i  ... step_i                                %%   : | : |  :  | :       ee ... step_ee                               %% ----+---+-----+---      e  ... step_e                                %%  ii | i | ... | i                                                    %% ----+---+-----+---                                                   %%  ee | e | ... | e                                                    %%                                                                      %%----------------------------------------------------------------------%step([R1,R2|M],[H|T]) :- 	step_ss(R1,R2,H), !,	step_([R1,R2|M],T). step_([R1,R2,R3|M],[H|T]) :-	step_ii(R1,R2,R3,H),	step_([R2,R3|M],T), !.step_([R1,R2],[H]) :- 	step_ee(R1,R2,H). % Start casestep_ss([A1,A2|R1],[B1,B2|R2],[H|T]) :-	rule([0,0,0],[0,A1,A2],[0,B1,B2],H),	step_s([A1,A2|R1],[B1,B2|R2],T).step_s([A1,A2,A3|R1],[B1,B2,B3|R2],[H|T]) :-		rule([0,0,0],[A1,A2,A3],[B1,B2,B3],H),	step_s([A2,A3|R1],[B2,B3|R2],T).step_s([A1,A2],[B1,B2],[H]) :-	rule([0,0,0],[A1,A2,0],[B1,B2,0],H). % Immediate casestep_ii([A1,A2|R1],[B1,B2|R2],[C1,C2|R3],[H|T]) :-	rule([0,A1,A2],[0,B1,B2],[0,C1,C2],H),	step_i([A1,A2|R1],[B1,B2|R2],[C1,C2|R3],T).step_i([A1,A2,A3|R1],[B1,B2,B3|R2],[C1,C2,C3|R3],[H|T]) :-		rule([A1,A2,A3],[B1,B2,B3],[C1,C2,C3],H),	step_i([A2,A3|R1],[B2,B3|R2],[C2,C3|R3],T).step_i([A1,A2],[B1,B2],[C1,C2],[H]) :-	rule([A1,A2,0],[B1,B2,0],[C1,C2,0],H). % End casestep_ee([A1,A2|R1],[B1,B2|R2],[H|T]) :-	rule([0,A1,A2],[0,B1,B2],[0,0,0],H),	step_e([A1,A2|R1],[B1,B2|R2],T).step_e([A1,A2,A3|R1],[B1,B2,B3|R2],[H|T]) :-		rule([A1,A2,A3],[B1,B2,B3],[0,0,0],H),	step_e([A2,A3|R1],[B2,B3|R2],T).step_e([A1,A2],[B1,B2],[H]) :-	rule([A1,A2,0],[B1,B2,0],[0,0,0],H). %----------------------------------------------------------------------%% o Any dead cell with exactly three live neighbours becomes a live    %%   cell, as if by reproduction.                                       %% o Any other dead cell remains dead.                                  %% o Any live cell with fewer than two live neighbours dies, as if      %%   caused by under-population.                                        %% o Any live cell with two or three live neighbours lives on to the    %%   next generation.                                                   %% o Any live cell with more than three live neighbours dies, as if by  %%   overcrowding.                                                      %%                                                                      %% [Source: Wikipedia]                                                  %%----------------------------------------------------------------------%rule([A,B,C],[D,0,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 3.rule([_,_,_],[_,0,_],[_,_,_],0).rule([A,B,C],[D,1,F],[G,H,I],0) :- A+B+C+D+F+G+H+I < 2.rule([A,B,C],[D,1,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 2.rule([A,B,C],[D,1,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 3.rule([A,B,C],[D,1,F],[G,H,I],0) :- A+B+C+D+F+G+H+I > 3. %----------------------------------------------------------------------%% Read a 2bit Protable Bitmap into a GS x GS 2-dimensional list.       %%   read_pbm( +File path, +Grid size, -List 2D )                       %%----------------------------------------------------------------------%read_pbm(F,GS,M) :-	open(F,read,S),	skip(S,10),	skip(S,10),	get(S,C),	read_file(S,C,L),	nest(L,GS,M),	close(S). read_file(S,C,[CHR|T]) :-	CHR is C-48,	get(S,NC),	read_file(S,NC,T).read_file(_,-1,[]) :- !. %----------------------------------------------------------------------%% Morph simple list into a 2-dimensional one with size GS x GS         %%   nest( ?List simple, ?Grid size, ?2D list )                         %%----------------------------------------------------------------------%nest(L,GS,[H|T]) :-	length(H,GS),	append(H,S,L),	nest(S,GS,T).nest(L,GS,[L]) :- 	length(L,S),	S =< GS, !.	 %----------------------------------------------------------------------%% Write a GS x GS 2-dimensional list into a 2bit Protable Bitmap.      %%   write_pbm( +List 2D, +Grid size, +File path )                      %%----------------------------------------------------------------------%	write_pbm(L,GS,F) :-	open(F,write,S),	write(S,'P1'), nl(S),	write(S,GS), put(S,' '), write(S,GS), nl(S),	write_file(S,L),	close(S). write_file(S,[H|T]) :-	write_line(S,H), nl(S),	write_file(S,T).write_file(_,[]) :- !. write_line(S,[H|T]) :-	write(S,H), put(S,' '),	write_line(S,T).write_line(_,[]) :- !. `

Sample output:

## Python

### Using defaultdict

This implementation uses defaultdict(int) to create dictionaries that return the result of calling int(), i.e. zero for any key not in the dictionary. This 'trick allows celltable to be initialized to just those keys with a value of 1.

Python allows many types other than strings and ints to be keys in a dictionary. The example uses a dictionary with keys that are a two entry tuple to represent the universe, which also returns a default value of zero. This simplifies the calculation N as out-of-bounds indexing of universe returns zero.

`import randomfrom collections import defaultdict printdead, printlive = '-#'maxgenerations = 3cellcount = 3,3celltable = defaultdict(int, { (1, 2): 1, (1, 3): 1, (0, 3): 1, } ) # Only need to populate with the keys leading to life #### Start States### blinkeru = universe = defaultdict(int)u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1 ## toad#u = universe = defaultdict(int)#u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1#u[(6,6)], u[(6,7)], u[(6,8)] = 1,1,1 ## glider#u = universe = defaultdict(int)#maxgenerations = 16#u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1#u[(6,5)] = 1#u[(7,6)] = 1 ## random start#universe = defaultdict(int, #                       # array of random start values#                       ( ((row, col), random.choice((0,1)))#                         for col in range(cellcount[0])#                         for row in range(cellcount[1])#                       ) )  # returns 0 for out of bounds for i in range(maxgenerations):    print "\nGeneration %3i:" % ( i, )    for row in range(cellcount[1]):        print "  ", ''.join(str(universe[(row,col)])                            for col in range(cellcount[0])).replace(                                '0', printdead).replace('1', printlive)    nextgeneration = defaultdict(int)    for row in range(cellcount[1]):        for col in range(cellcount[0]):            nextgeneration[(row,col)] = celltable[                ( universe[(row,col)],                  -universe[(row,col)] + sum(universe[(r,c)]                                             for r in range(row-1,row+2)                                             for c in range(col-1, col+2) )                ) ]    universe = nextgeneration`
Output:
(sample)
```Generation   0:
---
###
---

Generation   1:
-#-
-#-
-#-

Generation   2:
---
###
---```

### Boardless approach

A version using the boardless approach. A world is represented as a set of (x, y) coordinates of all the alive cells.

`from collections import Counter def life(world, N):    "Play Conway's game of life for N generations from initial world."    for g in range(N+1):        display(world, g)        counts = Counter(n for c in world for n in offset(neighboring_cells, c))        world = {c for c in counts                 if counts[c] == 3 or (counts[c] == 2 and c in world)} neighboring_cells = [(-1, -1), (-1, 0), (-1, 1),                      ( 0, -1),          ( 0, 1),                      ( 1, -1), ( 1, 0), ( 1, 1)] def offset(cells, delta):    "Slide/offset all the cells by delta, a (dx, dy) vector."    (dx, dy) = delta    return {(x+dx, y+dy) for (x, y) in cells} def display(world, g):    "Display the world as a grid of characters."    print '          GENERATION {}:'.format(g)    Xs, Ys = zip(*world)    Xrange = range(min(Xs), max(Xs)+1)    for y in range(min(Ys), max(Ys)+1):        print ''.join('#' if (x, y) in world else '.'                      for x in Xrange) blinker = {(1, 0), (1, 1), (1, 2)}block   = {(0, 0), (1, 1), (0, 1), (1, 0)}toad    = {(1, 2), (0, 1), (0, 0), (0, 2), (1, 3), (1, 1)}glider  = {(0, 1), (1, 0), (0, 0), (0, 2), (2, 1)}world   = (block | offset(blinker, (5, 2)) | offset(glider, (15, 5)) | offset(toad, (25, 5))           | {(18, 2), (19, 2), (20, 2), (21, 2)} | offset(block, (35, 7)))  life(world, 5)`
Output:
```          GENERATION 0:
##...................................
##...................................
......#...........####...............
......#..............................
......#..............................
...............##........#...........
...............#.#.......##..........
...............#.........##........##
..........................#........##
GENERATION 1:
##...................................
##.................##................
...................##................
.....###...........##................
.....................................
...............##........##..........
..............##........#............
................#..........#.......##
.........................##........##
GENERATION 2:
##...................................
##.................##................
......#...........#..#...............
......#............##................
......#..............................
..............###........#...........
..............#..........##..........
...............#.........##........##
..........................#........##
GENERATION 3:
##...................................
##.................##................
..................#..#...............
.....###...........##................
...............#.....................
..............##.........##..........
..............#.#.......#............
...........................#.......##
.........................##........##
GENERATION 4:
##...................................
##.................##................
......#...........#..#...............
......#............##................
......#.......##.....................
..............#.#........#...........
..............#..........##..........
.........................##........##
..........................#........##
GENERATION 5:
##...................................
##.................##................
..................#..#...............
.....###...........##................
..............##.....................
.............##..........##..........
...............#........#............
...........................#.......##
.........................##........##
```

### Using Array define the world

`import numpy as npfrom pandas import DataFrameimport matplotlib.pyplot as plt#import time def conway_life(len=10, wid=10, gen=5):     curr_gen = DataFrame(np.random.randint(0, 2, (len+2, wid+2)),                         index = range(len+2),                          columns = range(wid+2))    curr_gen[0] = 0    curr_gen[wid+1] = 0    curr_gen[0: 1] = 0    curr_gen[len+1: len+2] = 0         for i in range(gen):         fig, ax = plt.subplots()        draw = curr_gen[1:len+1].drop([0, wid+1], axis=1)        # 画图         image = draw        ax.imshow(image, cmap=plt.cm.cool, interpolation='nearest')        ax.set_title("Conway's game of life.")         # Move left and bottom spines outward by 10 points        ax.spines['left'].set_position(('outward', 10))        ax.spines['bottom'].set_position(('outward', 10))        # Hide the right and top spines        ax.spines['right'].set_visible(False)        ax.spines['top'].set_visible(False)        # Only show ticks on the left and bottom spines        ax.yaxis.set_ticks_position('left')        ax.xaxis.set_ticks_position('bottom')         plt.show()        # time.sleep(1)                   # 初始化空表        next_gen = DataFrame(np.random.randint(0, 1, (len+2, wid+2)),                             index = range(len+2),                              columns = range(wid+2))          # 生成下一代        for x in range(1, wid+1):            for y in range(1, len+1):                env = (curr_gen[x-1][y-1] + curr_gen[x][y-1] +                        curr_gen[x+1][y-1]+ curr_gen[x-1][y] +                        curr_gen[x+1][y] + curr_gen[x-1][y+1] +                        curr_gen[x][y+1] + curr_gen[x+1][y+1])                 if (not curr_gen[x][y] and env == 3):                    next_gen[x][y] = 1                if (curr_gen[x][y] and env in (2, 3)):                    next_gen[x][y] = 1         curr_gen = next_gen   conway_life()`

## R

`# Generates a new board - either a random one, sample blinker or gliders, or user specified.gen.board <- function(type="random", nrow=3, ncol=3, seeds=NULL){    if(type=="random")    {       return(matrix(runif(nrow*ncol) > 0.5, nrow=nrow, ncol=ncol))    } else if(type=="blinker")    {       seeds <- list(c(2,1),c(2,2),c(2,3))    } else if(type=="glider")    {       seeds <- list(c(1,2),c(2,3),c(3,1), c(3,2), c(3,3))    }    board <- matrix(FALSE, nrow=nrow, ncol=ncol)     for(k in seq_along(seeds))    {      board[seeds[[k]][1],seeds[[k]][2]] <- TRUE    }    board} # Returns the number of living neighbours to a locationcount.neighbours <- function(x,i,j) {      sum(x[max(1,i-1):min(nrow(x),i+1),max(1,j-1):min(ncol(x),j+1)]) - x[i,j]} # Implements the rulebasedetermine.new.state <- function(board, i, j){   N <- count.neighbours(board,i,j)   (N == 3 || (N ==2 && board[i,j]))} # Generates the next interation of the board from the existing oneevolve <- function(board){    newboard <- board   for(i in seq_len(nrow(board)))   {      for(j in seq_len(ncol(board)))      {         newboard[i,j] <- determine.new.state(board,i,j)               }      }   newboard} # Plays the game.  By default, the board is shown in a plot window, though output to the console if possible.game.of.life <- function(board, nsteps=50, timebetweensteps=0.25, graphicaloutput=TRUE){   if(!require(lattice)) stop("lattice package could not be loaded")      nr <- nrow(board)    for(i in seq_len(nsteps))   {      if(graphicaloutput)       {         print(levelplot(t(board[nr:1,]), colorkey=FALSE))       } else print(board)         Sys.sleep(timebetweensteps)       newboard <- evolve(board)       if(all(newboard==board))      {         message("board is static")         break      } else if(sum(newboard) < 1)      {         message("everything is dead")         break      } else board <- newboard   }      invisible(board)} # Example usagegame.of.life(gen.board("blinker"))game.of.life(gen.board("glider", 18, 20))game.of.life(gen.board(, 50, 50))`

## Racket

` #lang racket(require 2htdp/image 2htdp/universe) ;;;;;; Grid object;;; (define (make-empty-grid m n)  (build-vector m (lambda (y) (make-vector n 0)))) (define rows vector-length) (define (cols grid)  (vector-length (vector-ref grid 0))) (define (make-grid m n living-cells)  (let loop ([grid (make-empty-grid m n)]             [cells living-cells])    (if (empty? cells)        grid        (loop (2d-set! grid (caar cells) (cadar cells) 1) (cdr cells))))) (define (2d-ref grid i j)  (cond [(< i 0) 0]        [(< j 0) 0]        [(>= i (rows grid)) 0]        [(>= j (cols grid)) 0]        [else (vector-ref (vector-ref grid i) j)])) (define (2d-refs grid indices)  (map (lambda (ind) (2d-ref grid (car ind) (cadr ind))) indices)) (define (2d-set! grid i j val)  (vector-set! (vector-ref grid i) j val)  grid) ;;; cartesian product of 2 lists(define (cart l1 l2)  (if (empty? l1)      '()      (append (let loop ([n (car l1)] [l l2])                (if (empty? l) '() (cons (list n (car l)) (loop n (cdr l)))))              (cart (cdr l1) l2)))) ;;; Count living cells in the neighbourhood(define (n-count grid i j)  (- (apply + (2d-refs grid (cart (list (- i 1) i (+ i 1))                                  (list (- j 1) j (+ j 1)))))     (2d-ref grid i j))) ;;;;;; Rules and updates of the grid;;; ;;; rules are stored in a 2d array: r_i,j = new state of a cell;;; in state i with j neighboors(define conway-rules  (list->vector (list (list->vector '(0 0 0 1 0 0 0 0 0))                      (list->vector '(0 0 1 1 0 0 0 0 0))))) (define (next-state rules grid i j)  (let ([current (2d-ref grid i j)]        [N (n-count grid i j)])    (2d-ref rules current N))) (define (next-grid rules grid)  (let ([new-grid (make-empty-grid (rows grid) (cols grid))])    (let loop ([i 0] [j 0])      (if (>= i (rows grid))          new-grid          (if (>= j (cols grid))              (loop (+ i 1) 0)              (begin (2d-set! new-grid i j (next-state rules grid i j))                     (loop i (+ j 1)))))))) (define (next-grid! rules grid)  (let ([new-grid (next-grid rules grid)])    (let loop ((i 0))      (if (< i (rows grid))        (begin (vector-set! grid i (vector-ref new-grid i))               (loop (+ i 1)))        grid)))) ;;;;;; Image / Animation;;; (define (grid->image grid)  (let ([m (rows grid)] [n (cols grid)] [size 5])    (let loop ([img (rectangle (* m size) (* n size) "solid" "white")]               [i 0] [j 0])      (if (>= i (rows grid))          img          (if (>= j (cols grid))              (loop img (+ i 1) 0)              (if (= (2d-ref grid i j) 1)                  (loop (underlay/xy img (* i (+ 1 size)) (* j (+ 1 size))                                     (square (- size 2) "solid" "black"))                        i (+ j 1))                  (loop img i (+ j 1))))))))   (define (game-of-life grid refresh_time)  (animate (lambda (n)             (if (= (modulo n refresh_time) 0)                 (grid->image (next-grid! conway-rules grid))                 (grid->image grid))))) ;;;;;; Examples;;; (define (blinker)  (make-grid 3 3 '((0 1) (1 1) (2 1)))) (define (thunder)  (make-grid 70 50 '((30 19) (30 20) (30 21) (29 17) (30 17) (31 17)))) (define (cross)  (let loop ([i 0] [l '()])    (if (>= i 80)        (make-grid 80 80 l)        (loop (+ i 1) (cons (list i i) (cons (list (- 79 i) i) l)))))) ;;; To run examples:;;; (game-of-life (blinker) 30);;; (game-of-life (thunder) 2);;; (game-of-life (cross) 2) `

Output for an 80x80 cross:

## Retro

` create world  20 20 * allot create next  20 20 * allot create initial(       0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 )(  0 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  1 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 ,(  2 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 ,(  3 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ,(  4 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  5 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  6 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  7 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  8 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,(  9 )  0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 10 )  0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 11 )  0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 12 )  0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 13 )  0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 14 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ,( 15 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ,( 16 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 ,( 17 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 ,( 18 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,( 19 )  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ( Assumes anything outside the bounds is "dead" ){{  variable surrounding  : get   ( rc- )    2over [ 0 19 within ] [email protected] and    [ world + [ 20 * ] dip + @ ] [ 2drop 0 ] if ;  : neighbor?  ( rc- )  get +surrounding ;  : NW  ( rc-rc )  2over [ 1- ] [email protected]     neighbor? ;  : NN  ( rc-rc )  2over [ 1- ] dip     neighbor? ;  : NE  ( rc-rc )  2over [ 1- ] dip 1+  neighbor? ;  : WW  ( rc-rc )  2over 1-             neighbor? ;  : EE  ( rc-rc )  2over 1+             neighbor? ;  : SW  ( rc-rc )  2over [ 1+ ] dip 1-  neighbor? ;  : SS  ( rc-rc )  2over [ 1+ ] dip     neighbor? ;  : SE  ( rc-rc )  2over [ 1+ ] [email protected]     neighbor? ;  : count    ( rc-rcn )    0 !surrounding    NW NN NE    WW    EE    SW SS SE @surrounding ;  : alive  ( rc-n )    count    [ 0 1 within ] [ drop 0 ] when    [ 4 8 within ] [ drop 0 ] when    [ 2 3 within ] [ drop 1 ] when ;  : dead   ( rc-n )    count    [ 3 =        ] [ drop 1 ] when    [ 0 2 within ] [ drop 0 ] when    [ 4 8 within ] [ drop 0 ] when ;  : newState  ( rc-n )    2over get 1 = [ alive ] [ dead ] if ;  : set   ( nrc- )  next + [ 20 * ] dip + ! ;  : cols  ( r- )    20 [ over swap newState 2rot set ] iter drop ;  : output  ( n- )  [ 'o ] [ '. ] if putc space ;---reveal---  : display  ( - )    cr world 20 [ 20 [ @+ output ] times cr ] times drop ;  : start    ( - )    initial world 20 20 * copy display ;  : gen      ( - )    20 [ cols ] iter  next world 20 20 * copy ;  : delay  ( - )  time 1+ [ time over <= ] while drop ;  : run    ( n- )    [ delay clear gen display ] times ;}} start 20 run`

## REXX

### version 1

This version has been trimmed down from the original REXX program, otherwise the size of the program (with all its options and optional formatting)
would probably be on the large side for general viewing,   and maybe a wee bit complex to demonstrate how to program for this task.

`/*REXX program runs and displays the Conway's game of life, it stops after  N  repeats. */signal on halt                                   /*handle a  cell growth  interruptus.  */parse arg peeps  '(' rows  cols  empty  life!  clearScreen  repeats  generations .       rows =        p(rows               3)     /*the maximum number of cell  rows.    */       cols =        p(cols               3)     /* "     "       "    "   "  columns.  */        emp = pickChar(empty        'blank')     /*an empty cell character  (glyph).    */   clearScr =        p(clearScreen        0)     /* "1"   indicates to clear the screen.*/      life! = pickChar(life!            '☼')     /*the gylph kinda looks like an amoeba.*/       reps =        p(repeats            2)     /*stop pgm  if there are  two  repeats.*/generations =        p(generations      100)     /*the number of  generations  allowed. */sw=max(linesize()-1,cols)                        /*usable screen width for the display. */#reps=0;         \$.=emp                          /*the universe is new,  ··· and barren.*/gens=abs(generations)                            /*used for a  programming  convenience.*/x=space(peeps);  upper x                         /*elide superfluous spaces; uppercase. */if x==''         then x="BLINKER"                /*if nothing specified,  use  BLINKER. */if x=='BLINKER'  then x= "2,1 2,2 2,3"if x=='OCTAGON'  then x= "1,5 1,6 2,4 2,7 3,3 3,8 4,2 4,9 5,2 5,9 6,3 6,8 7,4 7,7 8,5 8,6"call assign.                                     /*assign the initial state of all cells*/call showCells                                   /*show the  initial state of the cells.*/                                                 /* [↓]  cell colony grows, lives, dies.*/     do life=1  for gens;      call [email protected]      /*construct  next  generation of cells.*/     if generations>0 | life==gens  then call showCells    /*should cells be displayed? */     end   /*life*/fin: exit                                        /*stick a fork in it,  we're all done. *//*──────────────────────────────────────────────────────────────────────────────────────*/showCells: if clearScr  then 'CLS'               /*  ◄───  change 'command' for your OS.*/           call showRows                         /*show the rows in the proper order.   */           say right(copies('▒', sw)  life, sw)  /*show a fence between the generations.*/           if _==''  then call fin               /*if there's no life, then stop the run*/           if !._    then #reps=#reps+1          /*we detected a repeated cell pattern. */           !._=1                                 /*existence  state and compare  later. */           if reps\==0 & #reps<=reps then return /*so far, so good,   regarding repeats.*/           say           say center('"Life" repeated itself' reps "times, simulation has ended.",sw,'▒')           call fin                              /*stick a fork in it,  we're all done. *//*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/\$:         parse arg _row,_col;                    return \$._row._col==life!assign\$:   do r=1  for rows;   do c=1  for cols;   \$.r.[email protected].r.c;                                     end;    end;       returnassign.:   do while x\==''; parse var x r "," c x; \$.r.c=life!; rows=max(rows,r); cols=max(cols,c); end; life=0; !.=0; returnassign?:   ?=\$.r.c; n=neighbors(); if ?==emp then do;if n==3 then ?=life!; end; else if n<2 | n>3 then ?=emp; @.r.c=?; return[email protected]:   @.=emp;      do r=1  for rows;  do c=1  for cols;  call assign?;  end;  end;            call assign\$;       returnhalt:      say;         say "REXX program  (Conway's Life)  halted.";    say;      exit 0neighbors: return \$(r-1,c-1)  +  \$(r-1,c)  +  \$(r-1,c+1)  +  \$(r,c-1)  +  \$(r,c+1)  +  \$(r+1,c-1)  +  \$(r+1,c)  +  \$(r+1,c+1)p:         return word(arg(1), 1)pickChar:  _=p(arg(1)); arg u .; if u=='BLANK' then _=" "; L=length(_); if L==3 then _=d2c(_); if L==2 then _=x2c(_);  return _showRows:  _=; do r=rows by -1 for rows; z=; do c=1 for cols; z=z||\$.r.c; end; z=strip(z,'T',emp); say z; _=_||z; end; return`

This REXX program makes use of   linesize   REXX program (or BIF)   which is used to determine the screen width (or linesize) of the terminal (console);   not all REXXes have this BIF.

The   LINESIZE.REX   REXX program is included here   ──►   LINESIZE.REX.

output   when using the default input:

```☼☼☼

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 0
☼
☼
☼
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 1

☼☼☼

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 2
☼
☼
☼
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 3

☼☼☼

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 4

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ "Life" repeated itself 2 times, simulation has ended. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
```

### version 2

`/* REXX ---------------------------------------------------------------* 02.08.2014 Walter Pachl* Input is a file containing the initial pattern* The compute area is extended when needed*   (cells are born outside the current compute area)* The program stops when the picture shown is the same as the first*   or equal to the previous one*--------------------------------------------------------------------*/Parse Arg fIf f='' Then f='bipole'fid=f'.in'oid=f'.txt'; 'erase' oiddebug=0If debug Then Do  dbg=f'.xxx'; 'erase' dbg  Endml=0l.=''Do ri=3 By 1 While lines(fid)>0  l.ri='  'linein(fid)  ml=max(ml,length(strip(l.ri,'T')))  Endml=ml+2ri=ri+1yy=riIf debug Then  say 'ml='ml 'yy='yyyb=1a.=' 'b.=' 'm.=''x.=''Parse Value 1 ml 1 yy With xmi xma ymi ymaParse Value '999 0' With xmin xmaxParse Value '999 0' With ymin ymax Do y=1 To yy  z=yy-y-1  l=l.z  Do x=1 By 1 While l<>''    Parse Var l c +1 l    If c='*' Then Do      a.x.z='*'      End    End  EndCall showDo step=1 To 60  Call store  If step>1 & is_equal(step,1) Then Leave  If step>1 & is_equal(step,step-1) Then Leave    Call show_neighbors  Do y=yma To ymi By -1    ol=format(x,2)' '    Do x=xmi To xma      neighbors=neighbors(x,y)      If a.x.y=' ' Then Do             /* dead cell                  */        If neighbors=3 Then Do          b.x.y='*'                    /*  gets life                 */          mmo=xmi xma ymi yma          xmi=min(xmi,x-1)          xma=max(xma,x+1)          ymi=min(ymi,y-1)          yma=max(yma,y+1)          mm=xmi xma ymi yma          If mm<>mmo Then            Call debug mmo '->' mm          End        Else                           /* life cell                  */          b.x.y=' '                    /*  remains dead              */         End      Else Do                          /* life cell                  */        If neighbors=2 |,           neighbors=3 Then b.x.y='*'  /*  remains life              */                       Else b.x.y=' '  /*  dies                      */        End      End    End  /* b. is the new state and is now copied to a.                     */  Do y=yma To ymi By -1    Do x=xmi To xma      a.x.y=b.x.y      End    End  End/* Output name and all states                                        */Call lineout oid,' 'fst=' +'                                /* top and bottom border      */sb=' +'                                /* top and bottom border      */Do s=1 To step  st=st||'-'right(s,2,'-')||copies('-',xmax-xmin)'+'  sb=sb||copies('-',xmax-xmin+3)'+'  EndCall lineout oid,st                    /* top border                 */Do y=ymin To ymax  ol=''  Do s=1 To step    ol=ol '|' substr(m.s.y,xmin,xmax-xmin+1)    End  Call lineout oid,ol '|'  EndCall lineout oid,sb                    /* bottom border              */Call lineout oid'type' oidIf debug Then Do  Say 'original area' 1 ml '/' 1 yy  Say 'compute area ' xmi xma '/' ymi yma  EndExit set: Parse Arg x,y     a.x.y='*'     Return neighbors: Procedure Expose a. debug  Parse Arg x,y  neighbors=0  do xa=x-1 to x+1    do ya=y-1 to y+1      If xa<>x | ya<>y then        If a.xa.ya='*' Then          neighbors=neighbors+1      End    End  Return neighbors store:/* store current state (a.) in lines m.step.*                        */Do y=yy To 1 By -1  ol=''  Do x=1 To ml    z=a.x.y    ol=ol||z    End  x.step.y=ol  If ol<>'' then Do    ymin=min(ymin,y)    ymax=max(ymax,y)    p=pos('*',ol)    q=length(strip(ol,'T'))    If p>0 Then      xmin=min(xmin,p)    xmax=max(xmax,q)    End  m.step.y=ol  Call debug '====>' right(step,2) y ol  xmin xmax  EndReturn is_equal:/* test ist state a.b is equal to state a.a                          */  Parse Arg a,b  Do y=yy To 1 By -1    If x.b.y<>x.a.y Then      Return 0    End  Return 1 show: Procedure Expose dbg a. yy ml debugDo y=1 To yy  ol='>'  Do x=1 To ml    ol=ol||a.x.y    End  Call debug ol  EndReturn show_neighbors: Procedure Expose a. xmi xma ymi yma dbg debug  Do y=yma To ymi By -1    ol=format(y,2)' '     Do x=xmi To xma      ol=ol||neighbors(x,y)      End    Call debug ol    End  Return debug:  If debug Then    Return lineout(dbg,arg(1))  Else    Return`
Output:
``` blinker
+--1--+--2--+--3--+
|     |  *  |     |
| *** |  *  | *** |
|     |  *  |     |
+-----+-----+-----+ ```
```oktagon
*--1-------*--2-------*--3-------*--4-------*--5-------*--6-------*
|    **    |    **    |   *  *   |          |          |    **    |
|   *  *   |   ****   |   *  *   |   *  *   |   ****   |   *  *   |
|  *    *  |  *    *  | ** ** ** |  * ** *  |  * ** *  |  *    *  |
| *      * | **    ** |   *  *   |   *  *   |  **  **  | *      * |
| *      * | **    ** |   *  *   |   *  *   |  **  **  | *      * |
|  *    *  |  *    *  | ** ** ** |  * ** *  |  * ** *  |  *    *  |
|   *  *   |   ****   |   *  *   |   *  *   |   ****   |   *  *   |
|    **    |    **    |   *  *   |          |          |    **    |
*----------*----------*----------*----------*----------*----------*```

## Ruby

`def game_of_life(name, size, generations, initial_life=nil)  board = new_board size  seed board, size, initial_life  print_board board, name, 0  reason = generations.times do |gen|    new = evolve board, size    print_board new, name, gen+1    break :all_dead if barren? new, size    break :static   if board == new    board = new  end  if    reason == :all_dead then puts "no more life."  elsif reason == :static   then puts "no movement"  else                           puts "specified lifetime ended"  end  putsend def new_board(n)  Array.new(n) {Array.new(n, 0)}end def seed(board, n, points=nil)  if points.nil?    # randomly seed board    indices = []    n.times {|x| n.times {|y| indices << [x,y] }}    indices.shuffle[0,10].each {|x,y| board[y][x] = 1}  else    points.each {|x, y| board[y][x] = 1}  endend def evolve(board, n)  new = new_board n  n.times {|i| n.times {|j| new[i][j] = fate board, i, j, n}}  newend def fate(board, i, j, n)  i1 = [0, i-1].max; i2 = [i+1, n-1].min  j1 = [0, j-1].max; j2 = [j+1, n-1].min  sum = 0  for ii in (i1..i2)    for jj in (j1..j2)      sum += board[ii][jj] if not (ii == i and jj == j)    end  end  (sum == 3 or (sum == 2 and board[i][j] == 1)) ? 1 : 0end def barren?(board, n)  n.times {|i| n.times {|j| return false if board[i][j] == 1}}  trueend def print_board(m, name, generation)  puts "#{name}: generation #{generation}"  m.each {|row| row.each {|val| print "#{val == 1 ? '#' : '.'} "}; puts}end game_of_life "blinker", 3, 2, [[1,0],[1,1],[1,2]]game_of_life "glider", 4, 4, [[1,0],[2,1],[0,2],[1,2],[2,2]]game_of_life "random", 5, 10`
Output:
```blinker: generation 0
. # .
. # .
. # .
. . .
# # #
. . .
. # .
. # .
. # .

glider: generation 0
. # . .
. . # .
# # # .
. . . .
glider: generation 1
. . . .
# . # .
. # # .
. # . .
glider: generation 2
. . . .
. . # .
# . # .
. # # .
glider: generation 3
. . . .
. # . .
. . # #
. # # .
glider: generation 4
. . . .
. . # .
. . . #
. # # #

random: generation 0
. . . # #
. . . # .
. . . # .
# . . . #
# . # # #
random: generation 1
. . . # #
. . # # .
. . . # #
. # # . #
. # . # #
random: generation 2
. . # # #
. . # . .
. # . . #
. # . . .
. # . # #
random: generation 3
. . # # .
. # # . #
. # # . .
# # . # #
. . # . .
random: generation 4
. # # # .
. . . . .
. . . . #
# . . # .
. # # # .
random: generation 5
. . # . .
. . # # .
. . . . .
. # . # #
. # # # .
random: generation 6
. . # # .
. . # # .
. . . . #
. # . # #
. # . # #
random: generation 7
. . # # .
. . # . #
. . . . #
. . . . .
. . . # #
random: generation 8
. . # # .
. . # . #
. . . # .
. . . # #
. . . . .
random: generation 9
. . # # .
. . # . #
. . # . .
. . . # #
. . . . .
random: generation 10
. . # # .
. # # . .
. . # . #
. . . # .
. . . . .
```

### Class version

The above implementation uses only methods. Below is one that is object-oriented and feels perhaps a bit more Ruby-ish.

`class Game  def initialize(name, size, generations, initial_life=nil)    @size = size    @board = GameBoard.new size, initial_life    @board.display name, 0     reason = generations.times do |gen|      new_board = evolve      new_board.display name, gen+1      break :all_dead if new_board.barren?      break :static   if @board == new_board      @board = new_board    end     case reason    when :all_dead  then puts "No more life."    when :static    then puts "No movement."    else                 puts "Specified lifetime ended."    end    puts  end   def evolve    life = @board.each_index.select {|i,j| cell_fate(i,j)}    GameBoard.new @size, life  end   def cell_fate(i, j)    left_right = [0, i-1].max .. [i+1, @size-1].min    top_bottom = [0, j-1].max .. [j+1, @size-1].min    sum = 0    for x in left_right      for y in top_bottom        sum += @board[x,y].value if x != i or y != j      end    end    sum == 3 or (sum == 2 and @board[i,j].alive?)  endend class GameBoard  include Enumerable   def initialize(size, initial_life=nil)    @size = size    @board = Array.new(size) {Array.new(size) {Cell.new false}}    seed_board initial_life  end   def seed_board(life)    if life.nil?      # randomly seed board      each_index.to_a.sample(10).each {|x,y| @board[y][x].live}    else      life.each {|x,y| @board[y][x].live}    end  end   def each    @size.times {|x| @size.times {|y| yield @board[y][x] }}  end   def each_index    return to_enum(__method__) unless block_given?    @size.times {|x| @size.times {|y| yield x,y }}  end   def [](x, y)    @board[y][x]  end   def ==(board)    self.life == board.life  end   def barren?    none? {|cell| cell.alive?}  end   def life    each_index.select {|x,y| @board[y][x].alive?}  end   def display(name, generation)    puts "#{name}: generation #{generation}"    puts @board.map {|row| row.map {|cell| cell.alive? ? '#' : '.'}.join(' ')}  end   def apocalypse    # utility function to entirely clear the game board    each {|cell| cell.die}  endend class Cell  def initialize(alive) @alive = alive  end  def alive?;           @alive          end  def value;            @alive ? 1 : 0  end  def live;             @alive = true   end  def die;              @alive = false  endend Game.new "blinker", 3, 2, [[1,0],[1,1],[1,2]]Game.new "glider", 4, 4, [[1,0],[2,1],[0,2],[1,2],[2,2]]Game.new "random", 5, 10`

## Scilab

The game's initial state can be input manually by setting the values of `Init_state`, or using a bitmap image (or other supported formats). The output can be either printed on Scilab's console, or on a graphic window. For both image input and graphic output, either SIVP or IPCV modules are required, and `console_output` should be set to false.

`Init_state=[0 0 0;...            1 1 1;...            0 0 0];console_output=%T; if (atomsIsLoaded('IPCV') | atomsIsLoaded('SIVP')) & ~console_output then    Input=imread('initial_state.bmp');  //Comment this three lines in case    Init_state=~im2bw(Input,0.1);       //there is no input image but    Init_state=1.0.*Init_state;         //you still want the graphic window    scf(0); clf();    imshow(~Init_state);    set(gca(),"isoview","on");end Curr_state=1.0.*Init_state;Grid_size=size(Init_state);Gens=4; function varargout=neighbourhood(A,i,j)    R_top=i-1;    if i==1 then        R_top=1;    end     R_bottom=i+1;    if i==Grid_size(1) then        R_bottom=Grid_size(1);    end     R_left=j-1;    if j==1 then        R_left=1;    end     C_right=j+1;    if j==Grid_size(2) then        C_right=Grid_size(2);    end     varargout=list(A(R_top:R_bottom,R_left:C_right));endfunction function []=console_print(Grid)    String_grid=string(Grid);    for i=1:size(Grid,'r')        for j=1:size(Grid,'c')            if Grid(i,j) then                String_grid(i,j)="#";            else                String_grid(i,j)=" ";            end        end    end    disp(String_grid);endfunction neighbours=[];Next_state=[]; for gen=1:Gens    Next_state=zeros(Init_state);     for i=1:Grid_size(1)        for j=1:Grid_size(2)            neighbours=zeros(3,3);            neighbours=neighbourhood(Curr_state,i,j);             Sum_neighbours=sum(neighbours)-1*Curr_state(i,j);             Alive=Curr_state(i,j);            if Alive then                if Sum_neighbours<2 then                    Next_state(i,j)=0;                elseif Sum_neighbours==2 | Sum_neighbours==3 then                    Next_state(i,j)=1;                elseif Sum_neighbours>3 then                    Next_state(i,j)=0;                end            else                if Sum_neighbours==3 then                    Next_state(i,j)=1;                end            end        end    end     if (atomsIsLoaded('IPCV') | atomsIsLoaded('SIVP')) & ~console_output then        imshow(~Next_state);        sleep(50);    else        sleep(50);        disp("Generation "+string(gen)+":")        console_print(Next_state);    end     if sum(Next_state)==0 | Curr_state==Next_state then        disp('ALL CELLS HAVE DIED OR BECAME INERT');        disp('No. of Generations: '+string(gen))        break    end     Curr_state=Next_state;end`
Output:

Output when `console_output=%T`.

``` Generation 1:

!   #     !
!         !
!   #     !
!         !
!   #     !

Generation 2:

!         !
!         !
!#  #  #  !
!         !
!         !

Generation 3:

!   #     !
!         !
!   #     !
!         !
!   #     !

Generation 4:

!         !
!         !
!#  #  #  !
!         !
!         !
```

## Scheme

Works with: Scheme version implementing R6RS (tested with PLT Scheme, Petite Chez Scheme)
` ;;An R6RS Scheme implementation of Conway's Game of Life --- assumes;;all cells outside the defined grid are dead ;if n is outside bounds of list, return 0 else value at n(define (nth n lst)  (cond ((> n (length lst)) 0)        ((< n 1) 0)        ((= n 1) (car lst))        (else (nth (- n 1) (cdr lst))))) ;return the next state of the supplied universe(define (next-universe universe)  ;value at (x, y)  (define (cell x y)    (if (list? (nth y universe))        (nth x (nth y universe))        0))  ;sum of the values of the cells surrounding (x, y)  (define (neighbor-sum x y)    (+ (cell (- x 1) (- y 1))       (cell (- x 1) y)       (cell (- x 1) (+ y 1))       (cell x (- y 1))       (cell x (+ y 1))       (cell (+ x 1) (- y 1))       (cell (+ x 1) y)       (cell (+ x 1) (+ y 1))))  ;next state of the cell at (x, y)  (define (next-cell x y)    (let ((cur (cell x y))          (ns (neighbor-sum x y)))      (cond ((and (= cur 1)                  (or (< ns 2) (> ns 3)))             0)            ((and (= cur 0) (= ns 3))             1)            (else cur))))  ;next state of row n  (define (row n out)    (let ((w (length (car universe))))      (if (= (length out) w)          out          (row n               (cons (next-cell (- w (length out)) n)                     out)))))  ;a range of ints from bot to top  (define (int-range bot top)    (if (> bot top) '()        (cons bot (int-range (+ bot 1) top))))  (map (lambda (n)         (row n '()))       (int-range 1 (length universe)))) ;represent the universe as a string(define (universe->string universe)  (define (prettify row)    (apply string-append           (map (lambda (b)                  (if (= b 1) "#" "-"))                row)))  (if (null? universe)      ""      (string-append (prettify (car universe))                     "\n"                     (universe->string (cdr universe))))) ;starting with seed, show reps states of the universe(define (conway seed reps)  (when (> reps 0)    (display (universe->string seed))    (newline)    (conway (next-universe seed) (- reps 1)))) ;; --- Example Universes --- ;; ;blinker in a 3x3 universe(conway '((0 1 0)          (0 1 0)          (0 1 0)) 5) ;glider in an 8x8 universe(conway '((0 0 1 0 0 0 0 0)          (0 0 0 1 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 0 0)          (0 0 0 0 0 0 0 0)          (0 0 0 0 0 0 0 0)) 30)`
Output:
```-#-
-#-
-#-

---
###
---

-#-
-#-
-#-

---
###
---

-#-
-#-
-#-

--#-----
---#----
-###----
--------
--------
--------
--------
--------

--------
-#-#----
--##----
--#-----
--------
--------
--------
--------

--------
---#----
-#-#----
--##----
--------
--------
--------
--------

--------
--#-----
---##---
--##----
--------
--------
--------
--------

--------
---#----
----#---
--###---
--------
--------
--------
--------

--------
--------
--#-#---
---##---
---#----
--------
--------
--------

--------
--------
----#---
--#-#---
---##---
--------
--------
--------

--------
--------
---#----
----##--
---##---
--------
--------
--------

--------
--------
----#---
-----#--
---###--
--------
--------
--------

--------
--------
--------
---#-#--
----##--
----#---
--------
--------

--------
--------
--------
-----#--
---#-#--
----##--
--------
--------

--------
--------
--------
----#---
-----##-
----##--
--------
--------

--------
--------
--------
-----#--
------#-
----###-
--------
--------

--------
--------
--------
--------
----#-#-
-----##-
-----#--
--------

--------
--------
--------
--------
------#-
----#-#-
-----##-
--------

--------
--------
--------
--------
-----#--
------##
-----##-
--------

--------
--------
--------
--------
------#-
-------#
-----###
--------

--------
--------
--------
--------
--------
-----#-#
------##
------#-

--------
--------
--------
--------
--------
-------#
-----#-#
------##

--------
--------
--------
--------
--------
------#-
-------#
------##

--------
--------
--------
--------
--------
--------
-------#
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##

--------
--------
--------
--------
--------
--------
------##
------##
```

## SequenceL

Library: CImg

SequenceL Code:

`life(Cells(2))[I, J] := 	let		numNeighbors := Cells[I-1,J-1] + Cells[I-1,J] + Cells[I-1,J+1] +				Cells[I,J-1]   +/*current cell*/Cells[I,J+1] +				Cells[I+1,J-1] + Cells[I+1,J] + Cells[I+1,J+1];	in		0 when I=1 or J=1 or I=size(Cells) or J=size(Cells[I]) //On Border	else		0 when numNeighbors < 2 or numNeighbors > 3 //Cell Dies	else		1 when Cells[I,J] = 1 and numNeighbors = 2 or numNeighbors = 3 //Cell lives on or is born.	else		Cells[I,J]; //No Change  stressTestInput(n(0))[y,x] :=		0 when not y = n / 2	else		1	foreach y within 1 ... n,		x within 1 ... n;`

C++ Driver Code:

`#include <iostream>#include <string>#include <vector>#include <fstream>#include <cerrno> #include "Cimg.h" #include "SL_Generated.h" using namespace std;using namespace cimg_library; char titleBuffer[200]; std::string get_file_contents(const char*); int main(int argc, char ** argv){	int cores = 0;	int maxSize = 700; 	int drawSkip = 1;	int drawWait = 0; 	string inputFile = "test"; 	if(argc > 1)	{		inputFile = argv[1];	}	if(argc > 2)	{		cores = atoi(argv[2]);	} 	if(argc > 3)	{		maxSize = atoi(argv[3]);	} 	if(argc > 4)	{		drawSkip = atoi(argv[4]); 		if(drawSkip < 0)		{			drawWait = abs(drawSkip);			drawSkip = 1;		}	} 	sl_init(cores); 	SLTimer drawTimer; 	sprintf(titleBuffer, "Conway's Game of Life in SequenceL: %d Cores", cores); 	int width = 0;	int height = 0; 	//Read input file--------------	Sequence<Sequence<int>> initialGrid; 	if(inputFile != "test")	{		stringstream ss(get_file_contents(inputFile.c_str()));		string stringItem;		vector<int> elems;		char delim = ','; 		getline(ss, stringItem, delim);		width = `