Milestone 1: due Friday, March 1st

Milestone 2: due Tuesday, March 19th

Getting Started

Download CS101_Assign03.zip into your H:\CS101 directory.

In Cygwin Terminal, run the commands:

cd h:
cd CS101
unzip CS101_Assign03.zip
cd CS101_Assign03

To compile and run the program:

make
./Rule30.exe

One-dimensional cellular automata, Rule 30

Your task is to implement a one-dimensional cellular automaton: specifically, one that implements Wolfram's Rule 30.

The one-dimensional cellular automaton simulates a "world" which is a row of cells. Each cell is either alive or dead. For example, here is a possible initial state of the world:

images/assign03/initialState.png

Notice that each cell has an integer index, with 0 being the index of the first cell in the row. In this initial state, a single cell (at index 7) is alive.

For any state of the world, a next state can be computed. In the next state, whether the cell at index i is alive or dead depends on the values of three cells in the current state: the ones at indices i-1, i, and i+1. In a one-dimensional cellular automaton following Rule 30, the cell at index i in the next state will be alive if, and only if, the cells at indices i-1, i, and i+1 in the current state are in one of the following configurations:

images/assign03/nextStateComputation.png

Based on the initial state above, here is the next state would that would be computed:

images/assign03/gen1State.png

One a next state is computed, it becomes the current state, from which a "next" next state can be computed. Here are the two subsequent states:

images/assign03/gen2State.png images/assign03/gen3State.png

Your task

Your task is to implement a program that will read an initial state of the world and compute a specified number of subsequent states, printing a representation of each state using text characters. Here is a sample run of the program (user input in bold):

How many cells? 30
Enter the cells: 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
How many generations? 14

Gen  0: ..............*...............
Gen  1: .............***..............
Gen  2: ............**..*.............
Gen  3: ...........**.****............
Gen  4: ..........**..*...*...........
Gen  5: .........**.****.***..........
Gen  6: ........**..*....*..*.........
Gen  7: .......**.****..******........
Gen  8: ......**..*...***.....*.......
Gen  9: .....**.****.**..*...***......
Gen 10: ....**..*....*.****.**..*.....
Gen 11: ...**.****..**.*....*.****....
Gen 12: ..**..*...***..**..**.*...*...
Gen 13: .**.****.**..***.***..**.***..
Gen 14: **..*....*.***...*..***..*..*.

Note about the input of the cell values: your program should use a for loop that reads the cell values one at a time into an array. This will work even if all of the input values are specified on a single line of text (as they are in the example session above.)

Requirements

The program should read the following input values:

  • the number of cells in the initial state
  • for each cell in the initial state, an integer value (0 or 1) indicating whether the cell is alive or dead (1 is alive)
  • the number of generations to simulate

Once the data has been read, the program should simulate the requested number of "generations".

Important: the program output must be in exactly the format shown above. In the output, each line of output should start with "Gen X:" (where X is the generation number), followed by a series of * and . characters, where * indicates a cell that is alive and . indicates a cell that is dead. Use the same prompts to read the input values.

You will submit two milestones, described below.

Milestone 1

For milestone 1, the program needs to read the input values and print generation 0, which is simply the initial data.

Here's what a test run of the Milestone 1 program should look like (user input in bold):

How many cells? 10
Enter the cells: 0 0 1 1 0 1 1 0 0 1
How many generations? 10

Gen  0: ..**.**..*

You can use the following commands to test your implementation of Milestone 1:

./Rule30.exe < test/test0.in > out
diff oracle/test0.out out

If the diff command does not produce any output, then your program's output is identical to the expected output.

Note: use this test as a "sanity check" to make sure that your program is producing output in the correct format. You should also run your program interactively to make sure it behaves the way you expect.

Milestone 2

For milestone 2, the program must simulate the requested number of generations, printed each generation as shown above.

Important special case: The first and last cells in each state have no left and right neighbors, respectively. (E.g., there is no cell at index -1, which would be the left neighbor of the cell at index 0.) Treat these missing neighbors as always being dead cells.

You can use the following commands to test your implementation of Milestone 2:

./Rule30.exe < test/test1.in > out
diff oracle/test1.out out

If the diff command does not produce any output, then your program's output is identical to the expected output.

As with Milestone 2, you will want to run your program interactive and verify that its output is correct.

Hints

Milestone 1

Declare an array with a fairly large number of integer elements (perhaps 100). Use this array to store the cells in the initial state, using the values 0 and 1 to represent cells that are dead and alive. Use a for loop to read in the initial cell values, where each iteration of the loop reads one initial value.

Use another for loop to print the output, where each iteration of the loop prints a single '*' or '.' character depending on whether a cell is alive or dead.

Milestone 2

You will need to declare an additional array with the same number of elements you used to store the initial state. This new array will, for each time step of the simulation, be used to store the cell values of the next state. A single array will not be sufficient!

After all of the cells of the next generation are computed, you will (probably) want to copy them back into the original array.

Start by simulating a single time step, meaning that your program will print only "Gen 0:" and "Gen 1:" output lines. Once you have reasonable output for Gen 0 and Gen1, use a loop to simulate an arbitrary number of generations.

Grading

Milestone 1:

  • Read number of cells: 10%
  • Read initial cell values: 40%
  • Read number of generations: 10%
  • Print generation 0 (initial state) in correct format: 40%

Milestone 2:

  • Milestone 1 functionality: 20%
  • Simulates one time step (Gen 1) with correct output: 40%
  • Simulates arbitrary number of time steps with correct output: 40%

Both milestones:

You must use good coding style. Points will be deducted for programs which are indented incorrectly, use cryptic variable names, and/or lack comments.

Submitting

To submit your work, make sure your Rule30.cpp file is saved, and in the Cygwin window type one of the following commands (depending on whether you are submitting Milestone 1 or Milestone 2).

For Milestone 1:

make submit_ms1

File Milestone 2:

make submit_ms2

Enter your Marmoset username and password (which you should have received by email.) Note that your password will not be echoed to the screen. Make sure that after you enter your username and password, you see a message indicating that the submission was successful.

Important: Make sure that you check the file(s) you submitted to ensure that they are correct. Log into the server using the following URL (also linked off the course homepage):

https://cs.ycp.edu/marmoset/

You should see a list of labs and assignments. In the row for assign03_ms1 (milestone 1) or assign03_ms2 (milestone 2), click the link labeled view. You will see a list of your submissions. Download the most recent one (which should be listed first). Verify that it contains the correct files.

You are responsible for making sure that your submission contains the correct file(s).