Spring 2010 CS61CL Homework 2 : Life 1D
TA: Long Wei

Due on 02-03-2010


The Simulation Process

For hw2, you will be implementing an elementary cellular automaton, also known as Life 1D. A cellular automaton is composed of a space of cells, which can be dead or alive, and a rule describing the transition from one generation of cells to the next; each cell in the next generation depends on the 5 cells surrounding its position in the current generation. In Life 1D, the cells live in a 1-dimensional array.

Note: Some of you might be familiar with this game/assignment, but take notice that this version depends on 5 cells above it instead of 3.

For example, here is an array of 7 cells. The value 7 was chosen arbitrarily for this example.

Cells are shaded if they are alive, otherwise they are dead. Therefore, only the center cell in this example is alive (Your initial row will look similar no matter how long it is; only the center cell starts out alive).

We produce the next generation of cells using a rule that ranges from 0-4294967295. Note that such a number can be represented with 32 bits. For our example, we will consider the rule 3141592653, which is equal to 0b10111011010000001110011001001101. Each bit in this binary number corresponds to whether the child cell of the pattern representing its position will be dead or alive.
Position (decimal) Position (binary)Dead/Alive
0 0b00000 1
1 0b00001 0
2 0b00010 1
3 0b00011 1
4 0b00100 0
5 000b101 0
6 0b00110 1
7 0b00111 0
and so on...
 
30 0b11110 0
31 0b11111 1
Looking at the 3rd column from top to bottom is the same as looking at the binary representation of your rule from right to left.

Here is the same table graphically. Note again that the 5 cells above represent its place in binary and the one below represents whether the corresponding bit is on or off in the rule number.


We have to check each cell in the array to see whether it will be alive or dead in the next generation. We use the dead/aliveness of that cell along with the dead/aliveness of its 2 left and 2 right neighbors as a 5-bit number, which we use to index the rule.

For example, starting with the same 7 cell array, we first look at the leftmost cell.

It's dead, as well as its four neighbors (for our purposes, we will assume all cells off the world are dead). This corresponds to 0b00000, which for rule 3141592653 is 1 (i.e. on, alive). This means that in the corresponding cell for the array representing the next generation, it will be alive.

Repeat the process for the second cell from the left. This one is 0b00001, so in the next generation it will be dead. What about the third cell?

We can see that this cell is dead as well as its left neighbors, but its immediate right neighbor is alive. This corresponds to 0b00010, which for rule 3141592653 is 1 (i.e. on, alive). Therefore, the third cell of the next generation will also be alive.

The fourth cell corresponds to 0b00100, which is dead. We can repeat this process for the remaining cells to produce the following next generation array.

By repeatedly applying this generation process and putting each new generation below the previous one, we can produce a pattern.

You will be writing a C program to do this for any rule and number of rows.

For context and a full description of this problem, read Wolfram's Elementary Cellular Automata .

Problem

You are to implement a one-dimensional variant of Conway's Game of Life, heretofore called "Life 1D". Specifically, you will write the following program in C (from scratch), which takes 2 command line arguments, rows and rule. The rows value determines the size of the picture, which will be (rows+1) high and (2*rows+1) wide. The first row consists of all dead cells except the center cell, which is alive. Note that since the number of columns is always odd, there's always a definite center. We use rows+1 because rows really refers to the number of generated rows, which doesn't include the aforementioned initial row. which supports the following:
Usage: Life1D <rows> <rule>
    This program simulates 1D Life: the simplest class of one-dimensional
    cellular automata in a <ROWS=rows+1> x <COLS=2*rows+1> grid starting
    with a single live cell in the middle of the top row using rule <rule>.
    These 1D rules are defined in Wolfram's Elementary Cellular Automata:
    http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
    This program will print to stdout data in plain PBM file format.
    This output can be easily viewed using the display command or 
    converted to a another format using the pbmto* and ppmto* utilities. 
    A plain ascii PBM file can be created by adding a header line 
    "P1 <WIDTH> <HEIGHT>" and followed by a grid of data 
    (0 = dead = white, 1 = live = black).  Add a comment on the first 
    line with a brief description of the image.
  Arguments:
    <rows> is a positive integer specifying the number of rows to generate
    (not counting the first "seed row" which is all dead except for a central
    live cell). The columns are computed automatically -- enough so that
    the rule, if it were to grow in the normal triangular pattern, would
    just perfectly reach the edge. Off the board is considerered "dead".
    <rule> is a number from 0-4294967295 specifying the rule to use.

Examples:

    unix% ./Life1D 3 58979
    P1 7 4 ## 3 rows of Life1D (Rule 58979) by Yourfirstname Yourlastname
    0 0 0 1 0 0 0
    1 1 0 0 0 0 1
    1 0 0 0 1 0 0
    0 0 0 0 0 0 0
    unix% ./Life1D 5 32384626
    P1 11 6 ## 5 rows of Life1D (Rule 32384626) by Yourfirstname Yourlastname
    0 0 0 0 0 1 0 0 0 0 0
    0 0 0 1 0 1 0 0 0 0 0
    0 1 0 1 1 0 0 0 0 0 0
    0 1 0 1 0 1 0 0 0 0 0
    0 1 1 1 1 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0
    unix% ./Life1D 4 4338327
    P1 9 5 ## 4 rows of Life1D (Rule 4338327) by Yourfirstname Yourlastname
    0 0 0 0 1 0 0 0 0
    1 1 1 1 1 0 0 1 1
    1 0 0 0 0 0 0 0 1
    1 0 0 1 1 1 1 1 1
    1 1 0 1 0 0 0 0 0
    unix% ./Life1D 17 3141592653
    P1 35 18 ## 17 rows of Life1D (Rule 3141592653) by Yourfirstname Yourlastname
    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 0 0 0
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1
    1 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0
    0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 0
    1 0 1 0 1 1 1 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 1 1
    0 1 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 1 1 0
    1 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 1 0 1 1 0 1
    0 0 0 1 1 1 0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0
    1 0 1 0 1 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0
    0 1 0 0 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 0
    1 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 0
    0 0 0 1 0 0 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 1
    1 0 1 0 0 0 1 0 1 1 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0
    0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0 0
    1 0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0 0 0
    0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0 0 0 1
    1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0 0 0 1 0

Output Format

In order for your assignment to be graded properly, it must follow these output guidelines:
  1. The first line printed out in your program MUST follow this format
      P1 [a] [b] ## [c] rows of Life1D (Rule [d]) by [e]
    where [a] is the number of displayed cols, [b] is the number of displayed rows, [c] is the <row> parameter. [d] is the <rule> parameter, [e] is YOUR name

  2. Do not have any extraneous whitespace characters at the end of the row. Meaning, each row print out ends with "0\n" or "1\n", not "0 \n" or "1 \n".

  3. All rows end with a newline character. Meaning, after your program exits, the UNIX prompt starts on a new line and not on the same line as your last row.

  4. You must have spaces in between your columns (just like you see in the examples above) when you print out a row. Meaning, this is invalid (no spaces between columns):
       000010000
       000010100
       000010010
    
    and this is valid:
       0 0 0 0 1 0 0 0 0
       0 0 0 0 1 0 1 0 0
       0 0 0 0 1 0 0 1 0
    
  5. There can be any number of command line arguments and they are not necessarily integers. If the input doesn't satisfy the constraints (i.e., rows or rule aren't valid integers in the specified range, or too many/few arguments), you should print the Usage string above (just copy it into your code). It can also be found at ~cs61c/hw/02/usage.txt or here.

Test your code!

Test your code to see if they generate the examples above, then try a few rules of your own and verify that they're correct for a few rows. (If they are, then there's little reason to be wrong when producing more rows.) You can generate a gif image by piping the output into ppmtogif and redirecting the output to a file. E.g.,
unix% ./Life1D 3 58979 | ppmtogif > Life1D_3_60.gif
Here are the gifs resulting from the examples above:

Life1D 3 58979

Life1D 5 32384626

Life1D 4 4338327

Submission

Submit a single file Life1D.c by creating a directory called hw2 with your Life1D.c file in it. From within this directory run "submit hw2". Be certain that your program accepts command line arguments for <rows> and <rule>, and that you do not interactively prompt for these values. Otherwise, your program will hang and fail the autograder.