#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N_MAX 65 // lato della matrice
#define ELEM_RANGE2 2 // concentrazione in matrice di cellule
void riempi_Matrice(int a[][N_MAX]);
void stampa_Matrice(int a[][N_MAX]);
void aggiorna_Matrice(int a[][N_MAX]);
void riempiMatrice(int a[][N_MAX])
{
int i, j, num_random;
float temp;
int n = N_MAX; srand(time(NULL));
for(i=0;i < n;i=i+1)
{
for (j=0;j < n;j=j+1)
{
num_random = rand(); temp = (num_random * ELEM_RANGE2) / RAND_MAX ;
a[i][j]= (int)floor( temp ); if ( a[i][j] )
a[i][j]=1;
}
}
}
void stampa_Matrice(int a[][N_MAX]){
int i;
int j;
int n = N_MAX;
for(i=0;i < n+2;i=i+1) printf("_");
for(i=0;i < n;i=i+1)
{
printf("\n"); printf("|");
for (j=0;j < n;j=j+1)
{
if (a[i][j]==1)
{
printf("@");
}
else
{
printf(" ");
}
}
printf("|"); }
printf("\n"); for(i=0;i < n+2;i=i+1) printf("_"); printf("\n\n");
}
void aggiorna_Matrice (int a[][N_MAX]){
int i, j, ki, kj ,tot;
int n = N_MAX; int stato[N_MAX][N_MAX];
for(i=0;i < n;i=i+1)
{
for (j=0;j < n;j=j+1)
{ tot = 0;
for (ki=i-1; ki <= i+1;ki=ki+1)
{
for (kj=j-1; kj <= j+1;kj=kj+1)
{ if ((0 <= ki && ki < n) && (0 <= kj && kj < n))
tot = tot + a[ki][kj];
}
}
tot = tot - a[i][j] ; if ((a[i][j] == 0) && (tot == 3))
{
stato[i][j] =1;
}
else if ((a[i][j] == 1) && ( (tot == 3) || (tot == 2)) )
{
stato[i][j] =1;
}
else
{
stato[i][j] =0;
}
}
} for(i=0;i < n;i=i+1)
{
for (j=0;j < n;j=j+1)
{
a[i][j] = stato[i][j];
}
}
}