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

Use malloc functions and pointers for this In this assignment, you\'ll implement

ID: 3761502 • Letter: U

Question

Use malloc functions and pointers for this

In this assignment, you'll implement Conway's game of life in C. You can read all about it here: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life (Links to an external site.) . There is lots of code for solving this problem on the internet, BUT, our requirements are a little bit different, so BE SURE TO READ THE INSTRUCTIONS CAREFULLY AND CODE YOUR OWN SOLUTION.

The "game" is a grid of cells that are either alive or dead. During each "move," the board is updated with the following rules:

Any live cell with fewer than two live neighbors dies, as if caused by under-population.

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

Any live cell with more than three live neighbors dies, as if by over-population.

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

For this assignment you should define a struct to represent the board, which should contain the size of the board and a pointer to the actual data (we don't necessarily know in advance how big the board will be, so we'll have to use malloc to create space for the data).

The following functions are required (you'll lose points for not including them):

Make Board : create a board struct and allocate memory for the data

Free Board: release the memory you allocated for the board

Copy Board: take an existing board as a parameter, then allocate and return a new board with the same data

Index Of Cell: takes a row and column and gives the index into the flattened array (explained below)

Print Board: self explainatory

Fill Board: fill the board randomly with dead and alive cells

Rewind cursor: To make our code look like an animation, we're going to move the cursor backward on the screen. printf("") will make the cursor go up one line (If you end each row with a newline when your print, your cursor should be on the left edge of the screen, so going straight up is what you want to do). The rewind cursor should take the number of rows to go up as a parameter

Count living neighbors: this function should return the number of alive neighbors around a given cell

SimulateOneStep: preform one update to the game (hint you probably want to make a copy of the board so that when you update the board you don't change the number of living neighbors of another cell inadvertently)

main ask the user for the board size, create the board, then simulate for a bunch of steps. You may want to call usleep(numberOfMicrosecondsToWait) in your loop to slow it down. You can use usleep by #include-ing

Since we're allocating the board dynamically, it's tricky to use multidimensional arrays directly. Most programmers allocate a single array (with row*column elements), then map the row and column to a single number in this array. (Often index = row*numColumns + column, for example).

In order to deal with the edge cases (literally, the edges), we're going to cheat a little bit and add an extra row to the top and bottom, and an extra column on each side. When we update, we'll leave those rows and columns unchanged. This should simplify the logic of your count living neighbors method. Don't draw the extra rows/columns.

Like in tic-tac-toe this is a pretty big assignment, however most of the individual pieces are fairly simple. WRITE YOUR CODE ONE FUNCTION AT A TIME, AND THEN TEST IT AS YOU GO. You should not have more than one partially written function at a time (maybe print and fill when you're first starting, but that's really it).

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "life.h"

#include "load.h"

#include "rotate.h"

#include "evolve.h"

#define MAX_CYCLES 100

#define ROTATE    20

/* Display the arena in a vt100 (xterm) terminal with the given width and height.

*/

static void

show(char **arena, int width, int height)

{

    int x, y;

    printf("");

    for (x = 0; x < width; x++)

        if (arena[x] != NULL)

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

                if (arena[x][y] & 1)

                    printf("[%d;%dH*", y, x);

    }

    printf("");

    fflush(stdout);

}

int main(int argc, char **argv)

{

    char **arena;

    int width = WIDTH, height = HEIGHT;

    int cycle;

    if (argc != 2 && argc != 4) {

        fprintf(stderr,"usage is %s filename [ width height ] ",argv[0]);

        exit(1);

    }

    if (argc == 4) {

        width = atoi(argv[2]);

        height = atoi(argv[3]);

        if (width < MINSIZE || width > MAXSIZE || height < MINSIZE || height > MAXSIZE) {

            fprintf(stderr, "%s: width and height must be in the range %d to %d ",

                    argv[0], MINSIZE, MAXSIZE);

            exit(1);

        }

    }

    if ((arena = load(argv[1], width, height)) == NULL) {

        fprintf(stderr,"%s: Cannot open %s ",argv[0],argv[1]);

        exit(1);

    }

    for (cycle = 1; cycle <= MAX_CYCLES; cycle++) {

        show(arena, width, height);

        evolve(arena, width, height);

        if (cycle % ROTATE == 0)

            arena = rotate(arena, &width, &height);

        /* Add a 1/10 sec delay so that we can see what is happening.

         */

        usleep(100000);

    }

    int x;

    for (x = 0; x < width; x++)

        if (arena[x] != NULL)

            free(arena[x]);

    free(arena);

    return 0;

}