A function is a group of statements that together perform a task. Every C++ program has at least
one function, which is int main()
.
An array is a collection of homogeneous objects.
Source: Conway's Game of Life - Wikipedia
Animation above demonstrates an instance of the Conway's Game of Life, which is famous and fascinating due to the exceptionally complex patterns it can create based on extremely simple rules. If you haven't heard of it before, there are quite a lot of articles and fancy examples out there. In fact, the Google search result page itself contains an interesting demo.
Conway's Game of Life is just one example of cellular automata. In this lab, you will simulate an elementary cellular automaton. It is a 1-D version of Conway's Game of Life, where you can specify your own rules.
An elementary cellular automaton (ECA) is a 1-D array of cells, with states that evolve over time. Each state can be represented as an 1-D array containing 0 and 1s, where 0 represents a dead cell and 1 a living cell. Each ECA is also associated with an initial state and a rule. The rule determines how a cell will evolve into the next state, given the states of the cell itself and its two neighbours.
As an example, suppose we have the following initial state:
1 0 0 1 0 1 0 1 1
with the following rule:
current pattern: 111 110 101 100 011 010 001 000
new state for center cell: 1 0 0 0 1 0 0 1
In order for a cell evolve to the next state, we check the state of the cell and its two neighbours, lookup for the corresponding pattern in the rule, then update the cell to the new state. Note that the state wraps around, i.e. the left neighbour of the leftmost cell is the rightmost cell, and vice versa.
initial state: 1 0 0 1 0 1 0 1 1
patterns: 110 100 001 010 101 010 101 011 111
next state: 0 0 0 0 0 0 0 1 1
In order to describe different rules efficiently, we use the the Wolfram code system to refer to different rules. Note that each rule has the following properties:
The Wolfram code of each rule is essentially a (decimal) number between 0 and 255 inclusive, such that its i-th digit when written as a 8-digit binary number corresponds to the update value for pattern i. For example, the rule above has a Wolfram code of 137 (equivalent to 100010012).
In this lab, we will implement a console ECA simulator that sets up an ECA under some rule and initial state. After initialization, the user inputs a certain number N, and the program simulates the change of states of the ECA up to step N, and displays the result. The program takes the following form:
The parts in italics has been implemented in the skeleton code already. Your job is to complete the tasks to make the program function as intended. We will discuss the tasks in greater detail one by one.
Implement the initRule
function, which asks the user to enter the ECA
rule and store the rule number. The number is stored in binary digits, so that we can determine how to
update each ECA cell later on, depending on the pattern of the cell.
Instructions
"Please enter the rule number:"
.
"Invalid rule number, please retry:"
and keep retrieving new
input until the number is valid.
rule[]
, such that rule[0]
stores
the least-significant bit (i.e. value corresponding to the "000" pattern), and
rule[7]
stores the most-significant bit (i.e. value corresponding to
the "111" pattern).
ruleNum
.
Notes
%
.
number: 69 -> 34 -> 17 -> 8 -> 4 -> 2 -> 1 -> 0
digits: 1 -> 0 -> 1 -> 0 -> 0 -> 0 -> 1 -> 0
binary number: 1000101
contents of rule[]: [1, 0, 1, 0, 0, 0, 1, 0]
Implement the initStateFromInput
function, which initializes the ECA
cells based on manual input. The user first enters the initial no. of alive cells, then enters the
column numbers of these cells one by one. In this function, the following data structures are to be
modified:
int grid[HEIGHT][WIDTH];
- This is a 2D array, with each row
representing the ECA at a particular step. We initially set the top row to the initial state, then
fills in the rows one-by-one as we update the ECA state again and again according to the rule. Thus,
this is a 2D visualization of how the ECA state evolves over time.
WIDTH
is the number of cells in the ECA, and is by default
set to 60.
grid
has a finite number of rows, which is
defined as HEIGHT
and set to 15 by default. Whenever the
bottommost row is reached, we overwrite the top row with the the next state, and start again
from the top.
int initialState[WIDTH];
- This is a 1D array holding the initial
ECA state, which will be used when we have already run updates for multiple iterations and need to
reset the ECA. As the contents of grid
will get overwritten
constantly, we need a separate container to store the initial state.
Instructions
"Please enter the number of cells alive in the initial state:"
.
WIDTH
.
"Invalid number of living cells, please retry:"
and keep
retrieving new input until the number is valid.
"Please enter the column at which the cells are alive:"
.
(WIDTH - 1)
], print
"Column out of bound"
.
"Column duplicated"
.
col
is valid, set
initialState[col]
and
grid[0][col]
to 1.
Notes
"4 15 -2 58 27 58 23"
, where -2
and the second 58
are invalid and should be ignored.
Implement the initStateRandomly
function. In this function, the user
enters a probability value p between 0 and 1, and you should set the initial value of each ECA
cell to 1 with a probability of p.
Instructions
"Please enter the fill rate:"
.
"Invalid probability, please retry:"
and keep retrieving new
input until the number is valid.
grid[0][]
and
initialState[]
by calling
getRandNum(p)
for each element. This function will return a
value in {0, 1}, sampled from a Bernoulli distribution with probability p.
Notes
getRandNum(p)
should always be the same every time the program is
executed, regardless of the environment you compiled the program in.
Implement the getNeighbourState
function. Given a pair of row and
column, return the corresponding pattern for grid[row][col]
.
Instructions
Implement the update
function, which takes the current ECA state and
update it according to the rule. Two variables are used to store which state the ECA is currently in:
curStep
stores the current iteration number. It is a non-negative
integer.
curRow
stores the row number of
grid
that corresponds to the current row. It is an integer within the
range [0, (HEIGHT - 1)
]
Instructions
grid[curRow]
), update the next state
using getNeighbourState
and the rule. The next state should
either be the next row, or the top row when curRow
has reached
the bottommost row.
curRow
and
curStep
accordingly.
Implement the getState
function. This function is called every time
the user enters an iteration number, which is passed as the step
parameter. You can assume step
to be a non-negative integer. In this
function, the program repeatedly updates the ECA until the specified iteration is reached.
Note that the iteration numbers do not necessarily follow ascending order. If the input iteration number
is smaller than curStep
, the program should reset back to the initial
state (stored in initialState
), then start updating from the
beginning.
Instructions
update()
until curStep
reaches step
.
step < curStep
, you will need to first reset the ECA to the
original state, i.e. curStep = curRow = 0
.
curStep
directly, without restarting from the beginning.
step
is valid (i.e. a non-negative integer).
You may wish to compare your program output against the expected output using a diff checker. Additionally, we will always end our sample inputs during grading with -1 to terminate the program.
Rule 18 generates a fractal pattern resembling the SierpiĆski triangle.
Elementary Cellular Automaton
Please enter the rule number:
18
Please specify the initial state. 'R' for random generation, 'M' for manual input.
M
Please enter the number of cells alive in the initial state:
1
Please enter the column at which the cells are alive:
30
State of rule 18 after 0 steps:
============================================================
@
============================================================
Please specify the number of steps
14
State of rule 18 after 14 steps:
============================================================
@
@ @
@ @
@ @ @ @
@ @
@ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @
@ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
============================================================
Please specify the number of steps
30
State of rule 18 after 30 steps:
============================================================
@ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @
@ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
============================================================
Please specify the number of steps
100
State of rule 18 after 100 steps:
============================================================
@ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @
@ @ @ @
@ @
@ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @
@ @ @ @
@ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
============================================================
Please specify the number of steps
-1
Rule 30 is also an interesting rule: the central column displayed (i.e. the different states of the central cell over time) surprisingly exhibits randomness. In fact, you can interpret this column as a bit stream and use it as a pseudo-random number generator.
Elementary Cellular Automaton
Please enter the rule number:
30
Please specify the initial state. 'R' for random generation, 'M' for manual input.
M
Please enter the number of cells alive in the initial state:
1
Please enter the column at which the cells are alive:
30
State of rule 30 after 0 steps:
============================================================
@
============================================================
Please specify the number of steps
14
State of rule 30 after 14 steps:
============================================================
@
@@@
@@ @
@@ @@@@
@@ @ @
@@ @@@@ @@@
@@ @ @ @
@@ @@@@ @@@@@@
@@ @ @@@ @
@@ @@@@ @@ @ @@@
@@ @ @ @@@@ @@ @
@@ @@@@ @@ @ @ @@@@
@@ @ @@@ @@ @@ @ @
@@ @@@@ @@ @@@ @@@ @@ @@@
@@ @ @ @@@ @ @@@ @ @
============================================================
Please specify the number of steps
30
State of rule 30 after 30 steps:
============================================================
@ @ @ @@@ @ @@ @ @ @@@@@ @ @@@@@@ @ @ @@ @ @
@@ @ @@@ @@@@ @ @@@ @
@@ @@@@ @@ @@@ @@ @@ @ @@@
@@ @ @ @@@ @ @@ @@@ @@@@ @@ @
@@ @@@@ @@ @ @@@@@@ @ @ @@@ @@@@
@@ @ @@@ @@@@ @@@@ @@@ @@ @ @
@@ @@@@ @@ @@@ @ @@ @ @ @ @@@ @@@
@@ @ @ @@@ @ @@@ @@ @ @@@ @@ @ @ @ @
@@ @@@@ @@ @ @@@ @ @ @@@@ @ @ @@ @@@@@@
@@ @ @@@ @@@@ @@ @@@@@ @ @@@@@ @ @ @
@@ @@@@ @@ @@@ @ @@ @ @ @@ @ @@@@@ @@@
@@ @ @ @@@ @ @@ @ @@@@ @@ @ @@ @@ @ @@ @
@@ @@@@ @@ @ @@@ @ @ @ @@@ @@@@ @ @@ @ @@ @ @@@@
@@ @ @@@ @@@@ @@@@ @@ @@ @@@ @ @ @@@@ @ @ @
@@ @@@@ @@ @@@ @ @@ @ @ @@@ @ @@ @@@@ @@@ @@ @@@
============================================================
Please specify the number of steps
100
State of rule 30 after 100 steps:
============================================================
@ @@@ @ @ @ @@@ @ @@@@@ @ @@@@ @@@@@@ @ @
@ @ @@@@ @@ @@ @ @@@@@ @@ @@ @@@ @ @@@@@
@ @@ @@ @ @ @@ @@@ @ @ @@ @@@ @ @@ @ @@@ @@
@@ @ @ @ @@ @ @ @ @@ @@@ @@ @ @ @ @@@@ @@ @@@ @
@ @@@@ @@@@ @ @@@@@ @@ @@@ @ @@@@ @@ @ @ @ @@@ @@ @
@@@ @ @@@ @ @ @ @ @@ @ @ @ @@ @@ @ @ @@ @
@ @ @@@ @@ @@ @@@@@@@@ @ @@ @@@@@ @ @@@ @@@@ @ @@@
@@@@@@@ @ @ @@ @ @@ @@@@ @@@ @ @ @@@ @ @
@@ @ @@ @ @ @ @ @ @@ @ @ @@ @@@@ @ @@ @@
@ @ @@ @ @ @@@@ @ @@ @@ @ @@@@@@ @@ @ @@@@@@ @
@ @@ @@ @@@@ @ @ @ @ @@ @@@@ @ @@@@ @@ @@@@@
@@ @ @@@@ @@@@ @@ @@ @@@ @ @ @ @@@ @@ @ @@ @@@@@ @
@@ @@@@ @@@ @ @@@ @ @ @ @@ @@ @@@ @@@@ @@@ @@
@@@ @ @@ @ @@ @ @@@@ @@ @ @ @ @@@ @@@ @ @ @@
@@ @ @@ @ @@@@@@ @@@@ @ @ @@@@ @ @@@ @ @@@@@@ @@ @
============================================================
Please specify the number of steps
-1
Rule 110 is the first rule proven to be Turing-complete, similar to Conway's Game of Life. In other words, the patterns of 0s and 1s generated by rule 110 are in principle able to simulate any type of calculation or computer program.
Elementary Cellular Automaton
Please enter the rule number:
110
Please specify the initial state. 'R' for random generation, 'M' for manual input.
M
Please enter the number of cells alive in the initial state:
1
Please enter the column at which the cells are alive:
30
State of rule 110 after 0 steps:
============================================================
@
============================================================
Please specify the number of steps
14
State of rule 110 after 14 steps:
============================================================
@
@@
@@@
@@ @
@@@@@
@@ @
@@@ @@
@@ @ @@@
@@@@@@@ @
@@ @@@
@@@ @@ @
@@ @ @@@@@
@@@@@ @@ @
@@ @ @@@ @@
@@@ @@@@ @ @@@
============================================================
Please specify the number of steps
30
State of rule 110 after 30 steps:
============================================================
@@@ @@@@ @@@ @ @@ @@@
@@@@@@@@ @@ @@@
@@ @@@@ @@ @
@@@ @@ @ @@@@@
@@ @ @@@ @@@@ @
@@@@@ @@ @@@ @ @@
@@ @ @@@@@ @ @@ @@@
@@@ @@ @@ @@@@@@@@ @
@@ @ @@@@@@ @@ @@@
@@@@@@@ @ @@@ @@ @
@@ @ @@@@ @ @@@@@
@@@ @@ @@ @@@ @@ @
@@ @ @@@ @@@ @@ @ @@@ @@
@@@@@ @@ @@@ @@@@@@ @@ @ @@@
@@ @ @@@@@ @@@ @@@@@@@@ @
============================================================
Please specify the number of steps
100
State of rule 110 after 100 steps:
============================================================
@@@@@ @ @@@@@@@ @ @@ @@@@@ @@@ @@@@@ @ @@@@@ @@@
@@ @@@ @@ @ @@ @@@@@ @ @@ @@@ @@@ @@ @ @@ @
@@ @@ @ @@@ @@@@@@@ @ @@ @@@@@ @ @@ @@@@ @@ @@@@@ @
@ @@@@@@@ @ @@ @ @@ @@@@@ @@@ @@@@@ @ @@@@@ @@@
@@@@ @@@ @@@ @@ @@@@@ @ @@ @@@ @ @@@@ @ @@ @
@ @@ @ @@ @ @@@@@ @ @@ @@@@@ @ @@@@ @ @@ @@@@@
@@ @@@@@@@@@@ @@ @ @@ @@@@@ @@@ @@ @ @@ @@@@@ @
@@@ @@ @ @@@ @@ @@@@@ @ @@ @@@@ @@@@@@@ @ @@
@@ @ @@@ @@@@ @ @@@@@ @ @@ @@@@@ @@@ @ @@ @@@
@@@@@ @ @@ @@@@@ @ @@ @@@@@ @ @@ @ @@ @@@@@
@@ @@@ @@@ @@ @ @@ @@@@@ @ @@@@@@@ @@@@@ @
@ @@ @ @@@ @@ @ @@ @@@@@ @ @@@@@ @@ @@@@@ @ @@
@@ @@@@@@@ @@@@ @@ @@@@@ @ @@ @@ @@@@@@ @@@ @@@
@@@ @@ @@@ @ @@@@@ @ @@ @@@ @@@ @@ @ @@ @@@ @
@@ @ @@@ @@ @ @@@@ @ @@ @@@@@ @@@ @ @@@ @@ @@@@@ @@@
============================================================
Please specify the number of steps
-1
Elementary Cellular Automaton
Please enter the rule number:
30
Please specify the initial state. 'R' for random generation, 'M' for manual input.
R
Please enter the fill rate:
1.2
Invalid probability, please retry:
0.7
State of rule 30 after 0 steps:
============================================================
@@@ @@@ @ @@@@@ @@ @ @@@@ @@@ @@@@ @ @@@@@@@@@ @@
============================================================
Please specify the number of steps
1347
State of rule 30 after 1347 steps:
============================================================
@ @@@ @ @@@@ @@@ @ @ @ @ @@ @ @@@@@@ @@@@
@@@@ @@ @@ @@@ @ @@@ @@ @@ @@@ @@ @ @@ @ @
@@ @ @@ @ @ @@ @@@ @ @ @@ @ @ @@@ @ @ @@ @@@
@ @ @@ @ @@@ @ @ @@@ @@@@@@@ @@@@@@@@@ @@@ @@@@ @ @@ @@
@ @ @ @ @ @ @ @ @@ @ @@@ @ @@@@ @@@
@@ @@@@ @@ @@ @ @@@@ @ @ @@@ @@ @ @@@ @@ @ @
@ @ @ @ @ @ @ @@ @@ @ @@ @@@ @ @@@ @ @@@@@
@@@@@ @@@@@@@@ @@ @@ @ @ @@ @@@@ @@ @ @@@@ @@@@
@@ @@@ @ @@@ @ @ @ @ @@@ @@@@ @@ @ @@ @
@ @ @@ @ @@ @ @@@ @ @@@@@ @@ @ @ @ @@ @ @ @@@ @
@@@@ @@@@ @@ @@@@ @ @ @ @ @@@ @@ @ @ @ @ @ @
@@@ @ @ @@ @@@ @ @@ @@ @@ @ @ @@@ @ @@@@ @ @@ @@
@ @@@ @@@@@ @ @ @@ @ @ @ @@ @ @@@@ @@@ @ @ @ @
@ @@ @@ @ @@@ @@@ @@ @ @ @@ @ @ @@ @@ @ @@ @
@@@ @ @@@ @ @@@ @@@ @@@@@@ @ @@@@ @@@ @@ @ @ @ @
============================================================
Please specify the number of steps
42
State of rule 30 after 42 steps:
============================================================
@ @@ @@@ @@ @ @@ @@ @ @ @ @ @@ @ @@ @ @@ @@@@ @
@@@@ @ @@ @ @@@@@@ @ @ @ @@ @@ @ @ @ @@@ @ @ @ @@
@ @@ @@ @ @@ @ @@@@@@ @ @ @ @@@@ @ @ @@@@@ @@ @
@@@ @@ @ @ @ @@ @@ @ @@@@@@@ @ @@ @@ @ @@
@@@ @@@@@@ @@@@ @ @@ @ @@ @ @ @@ @ @ @ @@@@@
@@ @ @ @ @ @@ @@ @@ @@@@@ @@@@ @@ @@ @
@@@ @ @@@ @@@ @@ @@@@ @@@ @@@ @ @@ @@@ @ @ @ @@
@ @ @ @@ @@@ @ @ @ @@@@ @ @@ @ @@@@@ @@@@
@@ @@@@@@@ @@@ @@@@ @@@ @@@ @@ @@@@ @@@@@@ @ @
@ @@ @ @ @@@ @@@ @ @ @ @@ @ @ @@@ @@
@ @@@@ @@@@@ @ @@ @ @@@ @@ @@@@ @ @@@ @@@ @@ @
@@ @ @ @@ @@@ @ @@@ @ @ @ @@@@ @ @@ @ @ @@@
@ @@ @@@ @@ @ @@ @ @ @@ @@@@@@ @@ @@@ @ @ @@ @ @
@ @@@@@@@ @@@ @ @ @@@ @ @ @@ @ @@@@ @@@ @ @@@ @@
@@ @ @ @@ @@ @ @ @ @ @@@@ @@ @@@ @ @ @@@ @
============================================================
Please specify the number of steps
-1
Note that multiple positions can be entered on the same line separated by space (see line 15 below), instead of entering line by line.
Elementary Cellular Automaton
Please enter the rule number:
30
Please specify the initial state. 'R' for random generation, 'M' for manual input.
M
Please enter the number of cells alive in the initial state:
-1
Invalid number of living cells, please retry:
100
Invalid number of living cells, please retry:
10
Please enter the column at which the cells are alive:
-2
Column out of bound
1 23 24 17 3 9 10 49 56
24
Column duplicated
39
State of rule 30 after 0 steps:
============================================================
@ @ @@ @ @@ @ @ @
============================================================
Please specify the number of steps
100
State of rule 30 after 100 steps:
============================================================
@ @@ @@@@ @@ @@ @@@@ @@@@@@ @@ @@@@@ @@ @ @@@ @ @@
@@@ @ @@@ @@@ @ @ @@ @ @@@ @ @ @@@ @@ @ @
@@ @@@@ @@ @ @@@@ @@ @ @ @@ @ @ @@ @ @ @ @ @@ @@
@@@ @ @ @@@@@ @ @ @@ @@ @@@@@ @@ @ @@ @@ @ @ @
@@ @ @@ @ @ @ @@@@@ @ @@@ @@@ @ @@@ @ @ @ @@@@@
@ @@@@@@ @ @@ @@@@@ @ @ @ @ @@ @ @@@@@@@ @
@@ @ @@@ @ @@@ @ @@ @@ @@@@@@ @@ @@ @@ @@
@ @@ @@ @ @ @ @@@ @@ @ @ @ @@@ @ @ @@ @ @
@@@ @ @@ @ @@ @@@@@@@ @ @@@@@@@ @@ @ @@@ @@ @@ @ @
@ @ @ @ @ @ @ @@ @ @ @@ @@@@ @ @ @@ @@@ @
@@ @@ @@@@ @@@@@ @@ @ @@ @@@@@ @@@ @ @@ @ @ @ @
@ @ @@@@@@@ @ @ @@@ @@ @ @ @ @@ @@@@ @ @ @@ @ @
@ @ @@@@@@ @ @ @@ @@@@@@ @@ @ @@ @@@@@ @ @
@@@ @@ @@ @@ @@@@@ @@@ @ @@ @@@@ @@ @ @@@ @
@ @ @@ @ @@ @ @ @ @@ @ @ @@@ @@@@ @@ @
============================================================
Please specify the number of steps
7
State of rule 30 after 7 steps:
============================================================
@ @ @@ @ @@ @ @ @
@@ @@ @@ @ @@@ @@ @ @@@ @@@ @@@
@ @ @ @@ @@ @@ @ @@ @@ @@ @ @@ @ @@ @@
@@@ @ @ @@@ @@@ @@@ @ @@@ @ @@ @@@@ @@ @@@@@@ @@@
@@ @ @ @ @ @ @ @ @@ @@ @ @ @@ @ @ @
@ @@ @ @@ @@@ @@@ @@ @@ @@ @ @@ @@@@ @@ @ @@@@ @@@@@
@@@ @ @ @ @ @ @ @ @ @@ @@ @ @ @ @ @ @@
@ @@@@ @@@@@ @@@ @@@@@@@@@@@ @@@ @@@@ @@@@@ @@ @@@@@ @ @
============================================================
Please specify the number of steps
-1
lab2.cpp
and zip it as
lab2.zip
for submission to ZINC.
lab2.cpp
otherwise ZINC
cannot find the file.
lab2.cpp
, not a folder containing
lab2.cpp
.