Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Brian's Brain Part 2. 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 2.

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 2: list.c

1. Implementvoid List_Print(List* list).Hints:use a for loop to iterate through the nodes of the list. Use the List member head for the setup of your for loop, the ListNode member next for the increment of your for loop, and you should compare with NULL for the test case of your for loop.

List .c:

#include "list.h"

#include

#include

#include "cell.h"

#define MAX_BUFFER_SIZE 64

/*

* Output:

* Returns a newly allocated List

* Summary:

* Allocates space for an empty new List and returns it

*/

List* List_Create() {

List* result = malloc(sizeof(List));

if (result != NULL) {

result->head = NULL;

result->size = 0;

}

return result;

}

/*

* Input:

* FILE* fp, a file pointer pointing to a read-only open file

* Output:

* Returns a newly allocated list

* Summary:

* Reads Cells from a file and puts them in a new List

*/

List* List_Read(FILE* fp) {

char buffer[MAX_BUFFER_SIZE];

List* result;

if (fp == NULL) {

printf("List_Read Error: NULL parameter passed. ");

exit(0);

}

result = List_Create();

while (fgets(buffer, MAX_BUFFER_SIZE, fp)) {

Cell C = Cell_ReadCell(buffer);

List_PushFront(result, C);

}

return result;

}

/*

* Input:

* List* list, the List to delete

* Summary:

* Calls free on every node to deallocate memory

* used by the nodes of the List, then deallocates

* the list itself

*/

void List_Delete(List* list) {

ListNode* currentNode = list->head;

while (currentNode != NULL) {

ListNode* nextNode = currentNode->next;

free(currentNode);

currentNode = nextNode;

}

free(list);

return;

}

/*

* Input:

* List* list, a List to add data to

* Cell data, data to add to the list

* Output:

* Returns the size of list after adding data

* Summary:

* Adds a data to the front of list

*/

int List_PushFront(List* list, Cell data) {

ListNode* newNode = ListNode_Create(data, list->head);

if (newNode != NULL) {

list->head = newNode;

list->size++;

}

return list->size;

}

/*

* Input:

* List* list, a List to print

* Summary:

* Prints elements from list, starting with the head

*/

void List_Print(List* list) {

// TODO: complete this function

}

/*

* Input:

* Cell data, data for the ListNode

* ListNode* node, the next node after the ListNode

* Output:

* Returns a ListNode* to a newly allocated ListNode

* Summary:

* Allocates space for a ListNode and initializes its

* data and node members

*/

ListNode* ListNode_Create(Cell data, ListNode* node) {

ListNode* listNode = (ListNode*) malloc(sizeof(ListNode));

if (listNode != NULL) {

listNode->data = data;

listNode->next = node;

}

return listNode;

}

list. h:

/*

* list.h

* Name / StudentID

* An implementation of a Linked List containg Cells

*/

#ifndef _LIST_H_

#define _LIST_H_

#include "cell.h"

#include

struct ListNode;

typedef struct {

struct ListNode* head;

int size;

} List;

typedef struct ListNode {

Cell data;

struct ListNode* next;

} ListNode;

/*

* Output:

* Returns a newly allocated List

* Summary:

* Allocates space for an empty new List and returns it

*/

List* List_Create();

/*

* Input:

* Cell data, data for the ListNode

* ListNode* node, the next node after the ListNode

* Output:

* A ListNode* to a newly allocated ListNode

* Summary:

* Allocates space for a ListNode and initializes its

* data and node members

*/

ListNode* ListNode_Create(Cell data, ListNode* node);

/*

* Input:

* FILE* fp, a file pointer pointing to a read-only open file

* Output:

* Returns a newly allocated list

* Summary:

* Reads Cells from a file and puts them in a new List

*/

List* List_Read(FILE* fp);

/*

* Input:

* List* list, the List to delete

* Summary:

* Calls free on every node to deallocate memory

* used by the nodes of the List, then deallocates

* the list itself

*/

void List_Delete(List* list);

/*

* Input:

* List* list, a List to add data to

* Cell data, data to add to the list

* Output:

* The size of list after adding data

* Summary:

* Adds data to the front of list

*/

int List_PushFront(List* list, Cell data);

/*

* Input:

* List* list, a List to print

* Summary:

* Prints elements from list, starting with the head

*/

void List_Print(List* list);

#endif /* _LIST_H_ */

cell.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_ */

list_tester.c:

/*

* list_tester.c

* Name / StudentID

* A tester for list.h

* Reads Cells from stdin into a List and prints them out

*/

#include

#include

#include "cell.h"

#include "list.h"

#define MAX_BUFFER_SIZE 64

void flushStdin();

int main() {

char buffer[MAX_BUFFER_SIZE];

char action;

List* list = List_Create();

do {

printf("Type 'c' to enter a cell or 'q' to quit: ");

action = getchar();

flushStdin();

if (action != 'c' && action != 'q') {

printf("Invalid command. ");

} else if (action == 'c') {

printf("Enter a cell: ");

fgets(buffer, MAX_BUFFER_SIZE, stdin);

List_PushFront(list, Cell_ReadCell(buffer));

}

} while(action != 'q');

List_Print(list);

List_Delete(list);

return 0;

}

void flushStdin() {

char c;

while ((c = getchar()) != EOF && c != ' ') {};

}

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

Semantics Of A Networked World Semantics For Grid Databases First International Ifip Conference Icsnw 2004 Paris France June 2004 Revised Selected Papers Lncs 3226

Authors: Mokrane Bouzeghoub ,Carole Goble ,Vipul Kashyap ,Stefano Spaccapietra

2004 Edition

3540236090, 978-3540236092

More Books

Students also viewed these Databases questions

Question

a. When did your ancestors come to the United States?

Answered: 1 week ago

Question

d. What language(s) did they speak?

Answered: 1 week ago

Question

e. What difficulties did they encounter?

Answered: 1 week ago