Program Conway\'s Game of Life using a NxN array. The starting state of the boar
ID: 3783189 • Letter: P
Question
Program Conway's Game of Life using a NxN array. The starting state of the board will be determined by one of the initialization subroutines. Leave the borders of the board empty and do not bother to update the borders of the board. A #define for N. Two arrays, one for the current board state and the other for the next board state. Store the state of each cell as an int8, dead = 0 and alive = 1. A subroutine that takes as input the current board array, a row array index, a column array index, and return the dead or alive status of that cell in the next generation. Initialize the next board array to 0. A subroutine that initializes an array with a Block (first still life) pattern near the middle of the board. A subroutine that initializes an array with a Period-2 Blinker near the middle of the board. A subroutine that initializes an array with a glider near the middle of the board. No global variables. Show that the current and next board arrays are the same at the end of the board update when the array is initialized to a Block. Do this by examining the array values for the two rows occupied by the Block. Show that the current and next board arrays show proper operation of a period-2 Blinker at the end of the board update when the array is initialized with a period-2 Blinker.Explanation / Answer
include <stdio.h>
#include <stdlib.h>
void header(void);
void survivalRule(char [][20], int, int);
void birthRule(char [][20], int, int);
void deathRule(char [][20], int, int);
int main(void)
{
char Life[20][20];
int orgs, gens;
int i, a, b, row, col;
int count = 0;
int x = 19;
int y = 19;
char ans;
header();
do{
printf(" Please enter the number of organisms you would like to live in the first generation: ");
scanf("%i", &orgs);
srand( (unsigned)time( NULL ) );
for(i = 0; i<orgs; i++)
{
row = rand();
row %= 20;
col = rand();
col %= 20;
Life[row][col] == '*';
}
for(row = 0; row<20; row++)
{
for(col = 0; col<20; col++)
{
if(Life[row][col] != '*')
Life[row][col] == ' ';
}
}
printf(" Now enter the number of generations you would like to simulate: ");
scanf("%i", &gens);
for(row = 0; row<20; row++)
{
for(col = 0; col<20; col++)
{
printf("%s", Life[row][col]);
}
puts(" ");
}
do{
birthRule(Life, x, y);
survivalRule(Life, x, y);
deathRule(Life, x, y);
for(row = 0; row<20; row++)
{
for(col = 0; col<20; col++)
{
printf("%s", Life[row][col]);
}
puts(" ");
}
count++;
}while(count<gens);
printf("Would you like to simulate another generation? y or n: ");
scanf("%c", &ans);
}while( ans != 'n' && ans != 'N');
return 0;
}
void header(void) /*function for program header*/
{
printf(" ..Welcome to the Game of Life.. ");
printf("Follow the instructions to start your own simulation of life! ");
}
void survivalRule(char Life[][20], int x, int y)
{
int row, col;
int neighbors = 0;
for(row = 1; row<19; row++)
{
for(col = 1; col<19; col++)
{
if(Life[row][col] == '*')
{
if(Life[row - 1][col - 1] == '*')
++neighbors;
if(Life[row - 1][col] == '*')
++neighbors;
if(Life[row - 1][col + 1] == '*')
++neighbors;
if(Life[row][col - 1] == '*')
++neighbors;
if(Life[row][col + 1] == '*')
++neighbors;
if(Life[row + 1][col - 1] == '*')
++neighbors;
if(Life[row + 1][col] == '*')
++neighbors;
if(Life[row + 1][col + 1] == '*')
++neighbors;
if(neighbors == 2 || neighbors == 3)
{
Life[row][col] == '*';
}
}
}
}
return;
}
void birthRule(char Life[][20], int x, int y)
{
int row, col;
int neighbors = 0;
for(row = 1; row<19; row++)
{
for(col = 1; col<19; col++)
{
if(&Life[row][col]== " ")
{
if(Life[row - 1][col - 1] == '*')
neighbors++;
if(Life[row - 1][col] == '*')
neighbors++;
if(Life[row - 1][col + 1] == '*')
neighbors++;
if(Life[row][col - 1] == '*')
neighbors++;
if(Life[row][col + 1] == '*')
neighbors++;
if(Life[row + 1][col - 1] == '*')
neighbors++;
if(Life[row + 1][col] == '*')
neighbors++;
if(Life[row + 1][col + 1] == '*')
neighbors++;
if(neighbors == 3)
{
Life[row][col] == '*';
}
}
}
}
return;
}
void deathRule(char Life[][20], int x, int y)
{
int row, col;
int neighbors = 0;
for(row = 1; row<19; row++)
{
for(col = 1; col<19; col++)
{
if(Life[row][col] == '*')
{
if(Life[row - 1][col - 1] == '*')
neighbors++;
if(Life[row - 1][col] == '*')
neighbors++;
if(Life[row - 1][col + 1] == '*')
neighbors++;
if(Life[row][col - 1] == '*')
neighbors++;
if(Life[row][col + 1] == '*')
neighbors++;
if(Life[row + 1][col - 1] == '*')
neighbors++;
if(Life[row + 1][col] == '*')
neighbors++;
if(Life[row + 1][col + 1] == '*')
neighbors++;
if(neighbors < 2 || neighbors > 4)
{
Life[row][col] == ' ';
}
}
}
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.