Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

!! IN C LANGUAGE PLEASE !! Project 2 - Conway\'s Game of Life CONWAY\'S GAME OF

ID: 3722722 • Letter: #

Question

!! IN C LANGUAGE PLEASE !!

Project 2 - Conway's Game of Life

CONWAY'S GAME OF LIFE

Submit your solution to Zybook Project 2 https://learn.zybooks.com/zybook/UICCS107TheysSpring2018/chapter/11/section/15

before 11:59pm on March 9th

From Wikipedia https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.

Any live cell with two or three live neighbours lives on to the next generation.

Any live cell with more than three live neighbours dies, as if by overpopulation.

Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

Your project is to create a game of life on a 2-d grid that will be no more than 50 x 50.

Your code should be able to simulate and display up to 50 timesteps.

The exact size of the grid and the number of timesteps to be simulated will be specified by the user.

Your code must have a 3d array declared in main of size [MAXSIZE][MAXSIZE][MAXTIME+1], where MAXSIZE is 50 and MAXTIME is 50

Your code must include the following two functions

For example:

Welcome to Conway's game of Life                                                                                              

Please enter the n for the n by n grid to simulate, max size for n is 50. :10                                                 

Thank You, now enter the initial x y coordinates (the board is indexed starting from 0 0) for the initial live cells, enter   

-1 -1 when done                                                                                                               

7 7

7 8

7 9

8 7

9 8

-1 -1                                                                                                     

Please enter the number of time steps you wish to simulate, max number allowed is 50. 12                                      

Num time steps 12                                                                                                             

After 12 timesteps the grid is:                                                                                               

  0  0  0  0  0  0  0  0  0  0                                                                                                

  0  0  0  0  0  0  0  0  0  0                                                                                                

  0  0  0  0  0  0  0  0  0  0                                                                                                

  0  0  0  0  0  0  0  0  0  0                                                                                                

  0  0  0  0  1  1  1  0  0  0                                                                                                

  0  0  0  0  1  0  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  0  0  0  0  0  0  0                                                                                                

  0  0  0  0  0  0  0  0  0  0                                                                                                

                                                                                                                              

                        

!! IN C LANGUAGE PLEASE !!

Grading:

15 points printGrid function

15 points neighbor function

15 points apply Conway's rules for user supplied number of time steps and output results

15 points read in input values and seed (initial locations of live cells) as described above (making sure size is < 50 and timesteps is < 50, if 50 or over code should print ERROR and exit)

5 points make sure the initial seed i,j values are less than the size of the board the user specified above, if not less than size of board print ERROR and exit

35 points style per @7

#pin

Explanation / Answer

#include <stdio.h>

#define HEIGHT 25

#define WIDTH 25

#define LIFE_YES 1

#define LIFE_NO 0

// make the rest of the function calls easier to read

typedef int TableType[HEIGHT][WIDTH];

void printTable(TableType table) {

int height, width;

for (height = 0; height < HEIGHT; height++) {

for (width = 0; width < WIDTH; width++) {

if (table[height][width] == LIFE_YES) {

printf("X");

} else {

printf("-");

}

}

printf(" ");

}

printf(" ");

}

// you already have a printTable, no need for this

// clear was a better name

void clearTable(TableType table) {

int height, width;

for (height = 0; height < HEIGHT; height++) {

for (width = 0; width < WIDTH; width++) {

table[height][width] = LIFE_NO;

}

}

}

void askUser(TableType tableA) {

int i;

int n;

int height, width;

printf("Enter the amount of initial organisms: ");

scanf("%d", &n);

for (i = 0; i < n; i++) {

printf("Enter dimensions (x y) where organism %d will live: ", i + 1);

scanf("%d %d", &height, &width);

tableA[height][width] = LIFE_YES;

}

printTable(tableA);

printf("Generation 0");

}

int getNeighborValue(TableType table, int row, int col) {

if (row < 0 || row >= HEIGHT

|| col < 0 || col >= WIDTH

|| table[row][col] != LIFE_YES )

{

return 0;

} else {

return 1;

}

}

int getNeighborCount(TableType table, int row, int col) {

int neighbor = 0;

neighbor += getNeighborValue(table, row - 1, col - 1);

neighbor += getNeighborValue(table, row - 1, col);

neighbor += getNeighborValue(table, row - 1, col + 1);

neighbor += getNeighborValue(table, row, col - 1);

neighbor += getNeighborValue(table, row, col + 1);

neighbor += getNeighborValue(table, row + 1, col - 1);

neighbor += getNeighborValue(table, row + 1, col);

neighbor += getNeighborValue(table, row + 1, col + 1);

return neighbor;

}

void calculate(TableType tableA) {

TableType tableB;

int neighbor, height, width;

for (height = 0; height < HEIGHT; height++) {

for (width = 0; width < WIDTH; width++) {

neighbor = getNeighborCount(tableA, height, width);

// change this arund to remove the ? : notation

if (neighbor==3) {

tableB[height][width] = LIFE_YES;

} else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {

tableB[height][width] = LIFE_YES;

} else {

tableB[height][width] = LIFE_NO;

}

}

}

// used to be swap

for (height = 0; height < HEIGHT; height++) {

for (width = 0; width < WIDTH; width++) {

tableA[height][width] = tableB[height][width];

}

}

}

// user entry is a pain for testing

// here's some code to load test data

void loadTestData(TableType table) {

// toggle

table[3][4] = LIFE_YES;

table[3][5] = LIFE_YES;

table[3][6] = LIFE_YES;

// glider

table[10][4] = LIFE_YES;

table[10][5] = LIFE_YES;

table[10][6] = LIFE_YES;

table[11][6] = LIFE_YES;

table[12][5] = LIFE_YES;

}

int main(void) {

TableType table;

char end;

int generation = 0;

clearTable(table);

// askUser(table);

loadTestData(table);

printTable(table);

do {

calculate(table);

printTable(table);

printf("Generation %d ", ++generation);

printf("Press q to quit or 1 to continue: ");

scanf(" %c", &end);

} while (end != 'q') ;

return 0;

}