Yeah, Salem, laserlight and adak, it's a life code. My teacher asked to do it, but without use the classic Conway's code. What is done by now: generate the 40x40 integer matrix with random numbers, create a char matrix based on the previous one respecting 40% of chances of cell be alive. But the function geracao that will control what happens on the next generation is not done yet. The main problem is define the conditions not using the i and j, but using V1 to V8. I thought in use adak suggestion but i couldnt understand it clearly. Anyway here's my code until now. Thanks for the help until now.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//Define the 8 possíbles neighbours of a cell
#define V1 (B[i-1][j])
#define V2 (B[i][j+1])
#define V3 (B[i+1][j])
#define V4 (B[i][j-1])
#define V5 (B[i-1][j-1])
#define V6 (B[i-1][j+1])
#define V7 (B[i+1][j-1])
#define V8 (B[i+1][j+1])
//Define conditions
//Condition 1: All neighbours are alive (X)
//Condição 2: Any three neighbours are alive (X)
//Condição 3: Two any neighbours are alive (X)
//Condição 4: Any dead cell (.) with three alive (X) neighbours, become alive;
#define COND1 (Write the first condition')
#define COND2 (Write the second condition'')
#define COND3 (Write the third condition')
#define COND4 (Write the fourth condition'')
//Global variables
int i,j,match=0; // Initialize the variables i,j (row and column) and match
int A[40][40]; // Define the 40x40 integer matrix
char B[40][40]; // Define the 40x40 char matrix
int main()
{
printf("Este codigo trabalha apenas com matrizes LIFE quadradas de 40x40:\n");
//This code only works with square matrix dimension 40.
printf("\nMatriz LIFE:\n\n");
srand(time(NULL)); //Activate function srand
for(i = 0; i < 40; i++) //loop - i to 40
{
for(j = 0; j < 40; j++) //loop - j to 40
{
A[i][j] = rand() % 10; // Aij value is between 0 and 9
printf("%1d", A[i][j]); // Print each Aij
if(j==39) printf("\n"); // Organize the matrix
}
}
printf("\n");
printf("\n");
for(i = 0; i < 40; i++) //loop - i to 40
{
for(j = 0; j < 40; j++) //loop - j to 40
{
if (A[i][j]<=3) B[i][j] = 'X'; //X for alive cells
else B[i][j] = '.'; // . for dead cells
printf("%1c", B[i][j]); // Print . or X
if(j==39) printf("\n"); // Organize the matrix
}
}
printf("\n");
geracao(); //Calls function geracao
imprimirmatriz(); //Calls function imprimirmatriz
getch();
}
//Funçtion geração - apply the necessary conditions.
int geracao(void)
{
printf("A funcao geracao aplica as condicoes de sobrevivencia\n");
\\Apply the conditions here using COND1, COND2, COND3 and COND4
}
//Function imprimir matriz (print the matrix after the geracao function)
int imprimirmatriz(void)
{
printf("A funcao imprimirmatriz imprime a nova matriz B após a aplicação das condicoes da geracao\n");
printf("\n");
printf("\n");
for(i = 0; i < 40; i++) //Loop - i to 40
{
for(j = 0; j < 40; j++) //Loop - j to 40
{
printf("%1c", B[i][j]);
if(j==39) printf("\n");
}
}
}