Assume an array of cells with an initial distribution of live and dead cells,
and imaginary cells off the end of the array having fixed values.

Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation.

If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:

000 -> 0  # 
001 -> 0  #
010 -> 0  # Dies without enough neighbours
011 -> 1  # Needs one neighbour to survive
100 -> 0  #
101 -> 1  # Two neighbours giving birth
110 -> 1  # Needs one neighbour to survive
111 -> 0  # Starved to death.

 
class
	APPLICATION
 
create
	make
 
feature
 
	make
			-- First 10 states of the cellular automata.
		local
			r: RANDOM
			automata: STRING
		do
			create r.make
			create automata.make_empty
			across
				1 |..| 10 as c
			loop
				if r.double_item < 0.5 then
					automata.append ("0")
				else
					automata.append ("1")
				end
				r.forth
			end
			across
				1 |..| 10 as c
			loop
				io.put_string (automata + "%N")
				automata := update (automata)
			end
		end
 
	update (s: STRING): STRING
			-- Next state of the cellular automata 's'.
		require
			enough_states: s.count > 1
		local
			i: INTEGER
		do
			create Result.make_empty
				-- Dealing with the left border.
			if s [1] = '1' and s [2] = '1' then
				Result.append ("1")
			else
				Result.append ("0")
			end
				-- Dealing with the middle cells.
			from
				i := 2
			until
				i = s.count
			loop
				if (s [i] = '0' and (s [i - 1] = '0' or (s [i - 1] = '1' and s [i + 1] = '0'))) or ((s [i] = '1') and ((s [i - 1] = '1' and s [i + 1] = '1') or (s [i - 1] = '0' and s [i + 1] = '0'))) then
					Result.append ("0")
				else
					Result.append ("1")
				end
				i := i + 1
			end
				-- Dealing with the right border.
			if s [s.count] = '1' and s [s.count - 1] = '1' then
				Result.append ("1")
			else
				Result.append ("0")
			end
		ensure
			has_same_length: s.count = Result.count
		end
 
end
 