Skip to content
Snippets Groups Projects
Commit 08fa1429 authored by Leonardo Krambeck's avatar Leonardo Krambeck
Browse files

implementado todo o resto, apenas faltando corrigir checkNeighbours e dar o clear screen

parent b63d1d1b
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
#include <string.h>
typedef struct Generation {
int **board;
int **generation;
int rows;
int cols;
} generation_t;
......@@ -15,11 +15,22 @@ typedef struct Game {
int totalGenerations;
} game_t;
#define ALIVE 1
#define DEAD 0
void startGame (game_t *game, char **argv);
void defineSizes (game_t *game, char **argv);
void memmoryAlloc (game_t *game);
void memoryAlloc (game_t *game);
void firstGeneration (game_t *game);
int testSize (game_t *game, int x, int y);
void playGame (game_t *game);
void printGeneration (game_t *game);
void nextCellStatus (game_t *game, int x, int y);
int checkNeighbors (game_t *game, int x, int y);
void createLife (game_t *game, int x, int y);
void keepStatus (game_t *game, int x, int y);
void killCell (game_t *game, int x, int y);
void incrementGeneration (game_t *game);
int main (int argc, char **argv)
{
......@@ -29,8 +40,7 @@ int main (int argc, char **argv)
{
startGame (&game, argv);
if ( !firstGeneration (&game) )
return 2;
firstGeneration (&game);
playGame (&game);
......@@ -46,7 +56,9 @@ void startGame (game_t *game, char **argv)
{
defineSizes (game, argv);
memmoryAlloc (game);
memoryAlloc (game);
game->generationNumber = 1;
printf ("How many generations do you want to simulate?\n");
scanf ("%d", &game->totalGenerations);
......@@ -63,29 +75,30 @@ void defineSizes (game_t *game, char **argv)
game->this.cols = cols;
game->next.rows = rows;
game->next.rows = rows;
game->next.cols = cols;
}
void memmoryAlloc (game_t *game)
/* Allocate the generations matrices with all cells DEAD (0). */
void memoryAlloc (game_t *game)
{
int i;
game->this.board = (int**) calloc ( game->this.rows, sizeof (int*) );
game->this.generation = (int**) calloc ( game->this.rows, sizeof (int*) );
for ( i=0; i < game->this.rows; i++ )
game->this.board[i] = (int *) calloc ( game->this.cols, sizeof (int) );
game->this.generation[i] = (int *) calloc ( game->this.cols, sizeof (int) );
game->next.board = (int**) calloc ( game->next.rows, sizeof (int*) );
game->next.generation = (int**) calloc ( game->next.rows, sizeof (int*) );
for ( i=0; i < game->next.rows; i++ )
game->next.board[i] = (int *) calloc ( game->next.cols, sizeof (int) );
game->next.generation[i] = (int *) calloc ( game->next.cols, sizeof (int) );
}
void firstGeneration (game_t *game)
{
int i, alive, x, y;
int alive, x, y, i=0;
printf ("\n");
printf ("How many alive cells do you want in the first generation?\n");
......@@ -94,23 +107,120 @@ void firstGeneration (game_t *game)
printf ("\n");
printf ("Put the coordinates of the cells you want alive on the first generation:\n");
for ( i=0; i < alive; i++ )
while ( i < alive )
{
printf ("Cell [%d]: ", i+1);
printf ("Cell [%d]: ", i);
scanf ("%d %d", &x, &y);
x--;
y--;
if ( testSize (game, x, y) )
createLife (game);
{
game->this.generation[x][y] = ALIVE;
i++;
}
else
printf ("Invalid coordinates.\n");
}
}
int testSize (game_t *game, int x, int y)
{
if ( (x < 0) || (y < 0) || (x >= game->this.rows) || (y >= game->this.cols) )
return 0;
return 1;
}
void playGame (game_t *game)
{
int i, j;
printGeneration (game);
while (game->generationNumber < game->totalGenerations)
{
for ( i=0; i < game->this.rows; i++ )
for ( j=0; j < game->this.cols; j++ )
nextCellStatus (game, i, j);
incrementGeneration (game);
printGeneration (game);
}
}
void printGeneration (game_t *game)
{
int i, j;
printf ("\n"); /*clrs*/
for ( i=0; i < game->this.rows; i++ )
{
for ( j=0; j < game->this.cols; j++ )
printf ("%d ", game->this.generation[i][j]);
printf ("\n");
}
}
void nextCellStatus (game_t *game, int x, int y)
{
switch ( checkNeighbors (game, x, y) )
{
case 3:
createLife (game, x, y);
break;
case 2:
keepStatus (game, x, y);
break;
default:
killCell (game, x, y);
}
}
/* ------------- Here is the jump of the cat --------------*/
int checkNeighbors (game_t *game, int x, int y)
{
int i, j, aliveCellCount = 0;
printf ("bruh\n");
for ( i=-1; i <= 1; i++ )
for ( j=-1; j <= 1; j++ )
if ( !( (x+i < 0) || (y+j < 0) || (x+i > game->this.rows) || (y+j > game->this.cols) ) )
if ( (x!=0) || (y!=0) )
if ( game->this.generation[x+i][y+j] == 1 )
aliveCellCount++;
return aliveCellCount;
}
void createLife (game_t *game, int x, int y)
{
game->next.generation[x][y] = ALIVE;
}
void keepStatus (game_t *game, int x, int y)
{
game->next.generation[x][y] = game->this.generation[x][y];
}
void killCell (game_t *game, int x, int y)
{
game->next.generation[x][y] = DEAD;
}
void incrementGeneration (game_t *game)
{
int i, j;
for ( i=0; i < game->next.rows; i++ )
for ( j=0; j < game->next.cols; j++ )
game->this.generation[i][j] = game->next.generation[i][j];
game->generationNumber++;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment