Conway's Game of Life

From Rosetta Code
Revision as of 22:18, 10 October 2008 by rosettacode>Paddy3118 (→‎{{header|Python}}: defaultdict comment)
Task
Conway's Game of Life
You are encouraged to solve this task according to the task description, using any language you may know.

Conways 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 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.

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

Python

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.

<python>import random from collections import defaultdict

printdead, printlive = '-#' maxgenerations = 3 cellcount = 3,3 celltable = defaultdict(int, {

(1, 2): 1,
(1, 3): 1,
(0, 3): 1,
} ) # Only need to populate with the keys leading to life
    1. Start States
  1. blinker

u = universe = defaultdict(int) u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1

    1. toad
  1. u = universe = defaultdict(int)
  2. u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
  3. u[(6,6)], u[(6,7)], u[(6,8)] = 1,1,1
    1. glider
  1. u = universe = defaultdict(int)
  2. maxgenerations = 16
  3. u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
  4. u[(6,5)] = 1
  5. u[(7,6)] = 1
    1. random start
  1. universe = defaultdict(int,
  2. # array of random start values
  3. ( ((row, col), random.choice((0,1)))
  4. for col in range(cellcount[0])
  5. for row in range(cellcount[1])
  6. ) ) # 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</python>

Sample output:

Generation   0:
   ---
   ###
   ---

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

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