C programming Imagine a world of organisms living in a two-dimensional cell grid
ID: 675327 • Letter: C
Question
C programming
Imagine a world of organisms living in a two-dimensional cell grid of size n × m. Each organism can only occupy a single cell. Each cell, except those at the boundaries (the edge of the world), has exactly eight neighboring cells (up, down, left, right, and four diagonals). The cells at the boundaries have less than eight neighboring cells. The world evolves from one generation to the other using the following rules:
1. Any organism with fewer than two neighbors (a neighbor is an organism that lives in a neighboring cell) dies (out of loneliness).
2. Any organism with more than three live neighbors dies (overcrowding).
3. Any organism with two or three live neighbors lives on to the next generation
4. Any vacant cell with exactly three live neighbors becomes occupied by a new organism (birth).
Write a C program that plays the game of life. Your program should:
1. Read an initial world con?guration from a ?le world.txt. The world is of size 10×10.
2. Evolve the world to the next generation.
3. Display the old and new world generation on screen.
4. Ask the user if he/she wants to continue evolution to the next generation.
5. Display a message if the entire world is extinct.
You should ignore the cells at the boundary of the world since the do not have enough neighbors to play the game. For a modular design, you should use the following functions:
void read world(FILE *inp, int x[ ][SIZE]); //reads a world from a ?le to array x
void print world(int x[ ][SIZE]); // prints world stored in array x on screen
int evolve(int x[ ][SIZE], int row, int col);// returns the evolved cell value for cell x[row][col]
void copy world(int x[ ][SIZE], int y[ ][SIZE]); // copies world in x to y
int extinct(int x[][SIZE]); // returns a zero if all organisms are extinct
Sample initial worlds are provided.
Hints:
• Play the game by hand on a small array to understand it.
• You need 2 two-dimensional arrays: one that stores the current generation and one that stores the future one.
• Start your cell procession from cell (1,1) up to cell (1,m-2) and cell (m-2, 1) to cell (m-2, m-2).
• For better understanding of your code use #de?ne ALIVE 1 and #de?ne DEAD 0, to denote the organism state.
• Print “*” for an alive mechanism and a “0” for a dead one.
Im allowed to use arrays, but no strings!
Here is the sample execution.
CONTENTS OF THE FILES:
small_world.txt
0 1 0 0 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 1
world1.txt
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
world2.txt
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 1 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Explanation / Answer
Ok boss. I'm seeing this problem from past 2 to 3 weeks. So, I tried solving it for you. Please go through the solution. If you need any further modifications, just ping me back.
The C code for your requirement is:
#include <stdio.h>
#define SIZE 10
#define ALIVE 1
#define DEAD 0
void readWorld(FILE *inp, int x[][SIZE])
{
int i, j;
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
fscanf(inp, "%i",&x[i][j]);
}
void printWorld(int x[][SIZE])
{
int i, j;
printf(" ");
for(i = 0; i < SIZE; i++)
{
for(j = 0; j < SIZE; j++)
if(x[i][j] == ALIVE)
printf(" * ");
else
printf(" 0 ");
printf(" ");
}
}
int evolve(int x[][SIZE], int row, int col)
{
int i = row;
int j = col;
return x[i-1][j-1] + x[i-1][j] + x[i-1][j+1] + x[i][j-1] + x[i][j+1] + x[i+1][j-1] + x[i+1][j] +x[i+1][j+1];
}
void copyWorld(int x[][SIZE], int y[][SIZE])
{
int i, j;
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
y[i][j] = x[i][j];
}
int extinct(int x[][SIZE])
{
int i, j, flag = 0;
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
if(x[i][j] == 1)
return 1;
return flag;
}
int main()
{
int i, j, currentGeneration[SIZE][SIZE], nextGeneration[SIZE][SIZE];
char fileName[50], choice;
FILE *infp;
printf("Enter the file name to read the current world from: ");
scanf("%s",fileName);
infp = fopen(fileName,"r");
while(1)
{
readWorld(infp, currentGeneration);
copyWorld(currentGeneration, nextGeneration);
for(i = 1; i < SIZE-1; i++)
for(j = 1; j < SIZE-1; j++)
if(evolve(currentGeneration, i, j) < 2 || evolve(currentGeneration, i, j) > 3)
nextGeneration[i][j] = DEAD;
else if(evolve(currentGeneration, i, j) == 3)
nextGeneration[i][j] = ALIVE;
fclose(infp);
printf("OldWorld: ");
printWorld(currentGeneration);
printf("NewWorld: ");
printWorld(nextGeneration);
printf("Do you want to evolve into further generation?(Y/N): ");
scanf(" %c",&choice);
copyWorld(nextGeneration, currentGeneration);
if(choice == 'N')
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.