Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Brian's Brain Part 3. In this program, you will create a cellular automata called Brians Brain. This cellular automata consists of squares (cells) on a

Brian's Brain Part 3.

In this program, you will create a cellular automata called Brians Brain. This cellular automata consists of squares (cells) on a 2D grid that are in one of three possible states: ON, OFF, or DYING. At each step, squares change their states depending on their neighbors. This assignment will guide you through this process,

Proceed by moving from the first part to the last part, in order. You must modify the supplied code. You do not need to define functions, and only need to add code to implement certain functions throughout the code. These are marked with:

// TODO: complete this function

To assist with your programming, the following testing files have been provided: list_tester.c, cell_tester.c, and cell_grid_tester.c. You may use these to compile executables that will test your code and give you feedback. Note that incorrect implementations may make these tests crash.

(This problem is separated into parts, and all code relevant to that part will be presented, along with instructions and hints provided.)

Part 3: cell_grid.c

The CellGrid type is defined as a struct in cell_grid.h. This represents the 2- dimensional grid of cells. At each phase (generation) of the simulation, all Cells in the CellGrid will be in one of the three states ON, OFF, or DYING. In Part 4, we use the current generations (current CellGrid) states to compute the next generation (a new CellGrid).

1. Implement CellGrid* CellGrid_Create(int numRows, int numCols).This should use malloc to allocate a CellGrid.

2. Implement void CellGrid_Delete(CellGrid* G). This should use free to de- allocate a CellGrid.

3. Implement CellState CellGrid_GetState(const CellGrid* G, int row, int col). This function returns the state of the cell specified by the input row and column.

4. Implement void CellGrid_SetCell(CellGrid* G, Cell C). This function sets C as a Cell of G. Note that C has x and y-coordinates that need to be used to specify which square of G to modify.

5. Implement bool CellGrid_Inbounds(const CellGrid* G, int row, int col). This checks whether or not the input row and column is in the grid G.

6. Implement void CellGrid_Print(const CellGrid* G, FILE* fp). This prints all of the cells according to their CellState. To begin, print the state of cell (0,0) first (in the top left corner) and then the rest of that row (all cells (0,j) where j is in bounds) before printing a new line and then proceed with the remaining lines.

cell_grid.c:

#include "cell_grid.h"

#include

#include

/*

* Input:

* int numRows, number of rows for new CellGrid

* int numCols, number of cols for new CellGrid

* Output:

* A CellGrid* pointing to a newly allocated CellGrid

* Summary:

* Allocates a new CellGrid with numRows rows and

* numCols cols and returns a pointer to it

*/

CellGrid* CellGrid_Create(int numRows, int numCols) {

// TODO: complete this function

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to delete

* Summary:

* De-allocates G, including grid, row by row

*/

void CellGrid_Delete(CellGrid* G) {

// TODO: complete this function

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to delete

* int row, an index for a row of *M

* int col, an index for a column of *M

* Output:

* The CellState in location row, col

*/

CellState CellGrid_GetState(const CellGrid* G, int row, int col) {

// TODO: complete this function

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to update

* Cell C, the cell to set in G

* Summary:

* Updates the (C.x, C.y) entry of G to be C

*/

void CellGrid_SetCell(CellGrid* G, Cell C) {

// TODO: complete this function

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to update

* int row, an index for a row of *M

* int col, an index for a column of *M

* Summary:

* Updates the (row,col) entry of G

*/

void CellGrid_Update(CellGrid* G, int row, int col) {

if (!CellGrid_Inbounds(G, row, col)) {

printf("Error in CellGrid_Update: index out of bounds ");

exit(0);

}

Cell_NextState(&(G->grid[row][col]));

return;

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid

* int row, an index for a row of *G

* int col, an index for a column of *G

* Summary:

* Checks if (row,col) is a Cell in G

*/

bool CellGrid_Inbounds(const CellGrid* G, int row, int col) {

// TODO: complete this function

}

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to print

* FILE* fp, a file opened for writing

* Summary:

* Prints entries of the CellGrid pointed to by G

* as O's for ONs and # for DYINGs

*/

void CellGrid_Print(const CellGrid* G, FILE* fp) {

// TODO: complete this function

}

cell_grid.h:

/*

* cell_grid.h

* Name / StudentID

* An implementation of a 2D Grid of Cells

*/

#ifndef _CELL_GRID_H_

#define _CELL_GRID_H_

#include "cell.h"

typedef struct {

Cell** grid; /* The grid */

int numRows; /* number of rows in grid */

int numCols; /* number of columns in grid */

} CellGrid;

/*

* Input:

* int numRows, number of rows for new CellGrid

* int numCols, number of cols for new CellGrid

* Output:

* A CellGrid* pointing to a newly allocated CellGrid

* Summary:

* Allocates a new CellGrid with numRows rows and

* numCols cols and returns a pointer to it

*/

CellGrid* CellGrid_Create(int numRows, int numCols);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to delete

* Summary:

* De-allocates G, including grid, row by row

*/

void CellGrid_Delete(CellGrid* G);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to delete

* int row, an index for a row of *M

* int col, an index for a column of *M

* Output:

* The CellState in location row, col

*/

CellState CellGrid_GetState(const CellGrid* G, int row, int col);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to update

* Cell C, the cell to set in G

* Summary:

* Updates the (C.x, C.y) entry of G to be C

*/

void CellGrid_SetCell(CellGrid* G, Cell C);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to update

* int row, an index for a row of *M

* int col, an index for a column of *M

* Summary:

* Updates the (row,col) entry of the CellGrid pointed to

* by G with the entry element

*/

void CellGrid_Update(CellGrid* G, int row, int col);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid

* int row, an index for a row of *G

* int col, an index for a column of *G

* Summary:

* Checks if (row,col) is a Cell in G

*/

bool CellGrid_Inbounds(const CellGrid* G, int row, int col);

/*

* Input:

* CellGrid* G, a pointer to a CellGrid to print

* FILE* fp, a file opened for writing

* Summary:

* Prints entries of the CellGrid pointed to by G

* as O's for ONs and # for DYINGs

*/

void CellGrid_Print(const CellGrid* G, FILE* fp);

#endif /* _CELL_GRID_H_ */

cell.h (used in cell_grid.h):

/*

* cell.h

* Name / StudentID

* A data structure modeling 2D integer Cartesian Cells

*/

#ifndef _COORDINATE_H_

#define _COORDINATE_H_

#include

#include

typedef enum {OFF, ON, DYING} CellState;

typedef struct {

int x, y;

CellState s;

} Cell;

/*

* Input:

* int x, an x-Cell

* int y, a y-Cell

* Output:

* A Cell

* Summary:

* Initializes a Cell to (x,y)

*/

Cell Cell_Create(int x, int y, CellState s);

/*

* Input:

* Cell C1, a Cell

* Cell C2, another Cell

* Output:

* true if C2 is a neighbor of C1, false otherwise

* Summary:

* Checks if C1 and C2 are neighbors, that is, if they

* are in adjacent squares (including squares that are

* diagonally adjacent) and not equal

*/

bool Cell_AreNeighbors(Cell C1, Cell C2);

/*

* Input:

* Cell* C, a pointer to a Cell

* Summary:

* Changes the Cell state of C to the next state.

* OFF -> ON -> DYING -> OFF

*/

void Cell_NextState(Cell* C);

/*

* Input:

* char* str, a string to read the cell from

* Output:

* Returns a newly allocated list

* Summary:

* Creates a cell from the file data

*/

Cell Cell_ReadCell(char* str);

/*

* Input;

* Cell C

* Ouptut:

* true if C has the corresponding CellState

*/

bool Cell_IsOff(Cell C);

bool Cell_IsOn(Cell C);

bool Cell_IsDying(Cell C);

#endif /* _COORDINATE_H_ */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

What are some illegal actions associated with prostitution?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago