How to resolve the algorithm Conway's Game of Life step by step in the Scilab programming language
How to resolve the algorithm Conway's Game of Life step by step in the Scilab programming language
Table of Contents
Problem Statement
The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is the best-known example of a cellular automaton. Conway's game of life is described here: A cell C is represented by a 1 when alive, or 0 when dead, in an m-by-m (or m×m) square array of cells. We calculate N - the sum of live cells in C's eight-location neighbourhood, then cell C is alive or dead in the next generation based on the following table: Assume cells beyond the boundary are always dead. The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.
Although you should test your implementation on more complex examples such as the glider in a larger universe, show the action of the blinker (three adjoining cells in a row all alive), over three generations, in a 3 by 3 grid.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Conway's Game of Life step by step in the Scilab programming language
Source code in the scilab programming language
Init_state=[0 0 0;...
1 1 1;...
0 0 0];
console_output=%T;
if (atomsIsLoaded('IPCV') | atomsIsLoaded('SIVP')) & ~console_output then
Input=imread('initial_state.bmp'); //Comment this three lines in case
Init_state=~im2bw(Input,0.1); //there is no input image but
Init_state=1.0.*Init_state; //you still want the graphic window
scf(0); clf();
imshow(~Init_state);
set(gca(),"isoview","on");
end
Curr_state=1.0.*Init_state;
Grid_size=size(Init_state);
Gens=4;
function varargout=neighbourhood(A,i,j)
R_top=i-1;
if i==1 then
R_top=1;
end
R_bottom=i+1;
if i==Grid_size(1) then
R_bottom=Grid_size(1);
end
R_left=j-1;
if j==1 then
R_left=1;
end
C_right=j+1;
if j==Grid_size(2) then
C_right=Grid_size(2);
end
varargout=list(A(R_top:R_bottom,R_left:C_right));
endfunction
function []=console_print(Grid)
String_grid=string(Grid);
for i=1:size(Grid,'r')
for j=1:size(Grid,'c')
if Grid(i,j) then
String_grid(i,j)="#";
else
String_grid(i,j)=" ";
end
end
end
disp(String_grid);
endfunction
neighbours=[];
Next_state=[];
for gen=1:Gens
Next_state=zeros(Init_state);
for i=1:Grid_size(1)
for j=1:Grid_size(2)
neighbours=zeros(3,3);
neighbours=neighbourhood(Curr_state,i,j);
Sum_neighbours=sum(neighbours)-1*Curr_state(i,j);
Alive=Curr_state(i,j);
if Alive then
if Sum_neighbours<2 then
Next_state(i,j)=0;
elseif Sum_neighbours==2 | Sum_neighbours==3 then
Next_state(i,j)=1;
elseif Sum_neighbours>3 then
Next_state(i,j)=0;
end
else
if Sum_neighbours==3 then
Next_state(i,j)=1;
end
end
end
end
if (atomsIsLoaded('IPCV') | atomsIsLoaded('SIVP')) & ~console_output then
imshow(~Next_state);
sleep(50);
else
sleep(50);
disp("Generation "+string(gen)+":")
console_print(Next_state);
end
if sum(Next_state)==0 | Curr_state==Next_state then
disp('ALL CELLS HAVE DIED OR BECAME INERT');
disp('No. of Generations: '+string(gen))
break
end
Curr_state=Next_state;
end
You may also check:How to resolve the algorithm Truncatable primes step by step in the RPL programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Java programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the Lua programming language
You may also check:How to resolve the algorithm Echo server step by step in the Go programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Clojure programming language