In this lab you will implement a cellular automaton.

Always start by thinking of the problem top-down. Split it into logical steps, and write one step at a time. Then compile it, and check if it does what you want. Write your code incrementally! This will save you much trouble.

As usual, work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.

Enjoy!

In this lab you will write a Java program that implements a simple,
one-dimensional *cellular automaton* (CA).

A one-dimensional CA can be thought of as a one-dimensional array. Each cell of the CA can have one of two values: 0 (zero) or 1 (one). It will start with a 0 in all cells except one cell, which will contain a 1. Call this time step 0. Your program will apply a simple rule, which I'll explain below, and the values in the CA will change, thus producing a new set of CA values at time step 1. The rule is applied again, resulting in a new set of CA values at time step 2. The process is repeated some number of times. Depending on the initial set of values in the CA and the rule applied, some unexpected patterns can arise, which are easily seen if the sets of values are printed out in successive rows in a certain way, also described below.

Your program should do the following:

- Allow the user to select the size of the CA, from 10 to 100 cells inclusive. Ensure that their choice is not out of this range.
- Make sure that the CA starts with zeros in every cell except for a single 1 placed by the user. Provide a way for the user to indicate where they want that 1 to go. Be sure it is in a legal cell of the CA, given the size they chose. You can force them to specify a 0-based position (like the array indices) or you can be more user friendly if you are so inclined.
- Get the number of time steps to run the CA from the user, making sure it's greater than 0.
- For each time step, change the values of the CA and print out the current values as described below.

Change the values of the CA according to the following rule. Given the current values:

- The values of the first and last cells stay the same.
- The value of the cell at index
`i`(where`i`is not the index of the first or last cell) is 1 if the sum of the**current**values of the cells at indices`i-1, i`, and`i+1`is exactly 1; otherwise, the new value at this cell should be 0.

0 1 2 3 4 5 6 7 8 |-----|-----|-----|-----|-----|-----|-----|-----|-----| CA | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | |-----|-----|-----|-----|-----|-----|-----|-----|-----|

You need to compute the new values and put them somewhere else until you have computed all the new values, then transfer them back into the CA.

To illustrate the application of the rule described above, the values in the next configuration of the CA pictured here would be:

0 1 2 3 4 5 6 7 8 |-----|-----|-----|-----|-----|-----|-----|-----|-----| new values | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | |-----|-----|-----|-----|-----|-----|-----|-----|-----|

The value at index 0 remains the same. The value at index 1 becomes 0
because the sum of its value and the values of its immediate neighbors
in `CA` (indices 0 and 2) is not 1, whereas the value at index 2
becomes 1 because the sum of its value and the values of its immediate
neighbors in `CA` (indices 1 and 3) `is` 1, and so forth.

Display the contents of the CA on the screen in the following way:

- If the array cell contains a 0, print out a single blank.
- If the array cell contains a 1, print out a single asterisk.

For example, the printout for the array {\tt CA} above would be:

** ** *

So each line of output will be a pattern of spaces and asterisks that reflects the configuration of values in the CA at a particular time step. To test your program, set the value of the cell at index 4 to 1 (all other cells have the value 0) and run it for 5000 time steps. You should see an interesting pattern of triangles of all different sizes. By the way, there's nothing special about index 4 -- try it with other locations.

**Miscellania**

You may wonder, what on earth is this problem good for? Cellular automata were studied in the 1950s as a possible model for biological systems. The theory of celular automata is rich, with simple rules and structures being capable of producing a great variety of unexpected behaviors. Check out wikipedia on Cellular automaton to get an idea of the problem and its history.

The most famous example of a cellular automaton is the Game of life devised by Conway in 1970. The Game of life is slightly more general than the version you imlement in this lab: it works on a 2-dimensional grid (rather than a 1-dimensional array). [from Wikipedia:] The universe of the Game of Life is an infinite two-dimensional grid of cells, each of which is either alive or dead. Cells interact with their eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following effects occur:

- Any live cell with fewer than two neighbours dies of loneliness.
- Any live cell with more than three neighbours dies of crowding.
- Any dead cell with exactly three neighbours comes to life.
- Any live cell with two or three neighbours lives, unchanged, to the next generation.

Though the Game of life is more general than the version in this
lab, you can easily make the connection between the simpler problem in
this lab and the rules of evolution of a simple population. Saying
that the new value at position `i` is 1 if and only if
`a[i-1] + a[i] + a[i+1] == 1` is the same as saying:

- Any live cell stays live if both its neighbors are dead (self-preservation).
- Any dead cell with precisely one live neighbor comes to life.
- Any live cell that has live neighbors dies of crowding.

- The program by e-mail and bring a hard copy to class