Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C langugage please help in this code, you need to centinue on the parts where it says put your code here. Conway's Game of

In C langugage

please help in this code, you need to centinue on the parts where it says "put your code here".

Conway's Game of Life

Mathematician John Conway invented a game in 1970 called the Game of Life (Links to an external site.)Links to an external site.. It is a simple simulation where one state determines the next. In particular, it is a two-dimensional cellular automaton.

What is interesting about cellular automaton is that sometimes simple rules create complex effects. In particular you will construct several interesting objects in the Game of Life world.

You will be writing a program to play the Game of Life and animate it.

Your Program

You will start with the code at the end of this assignment and progressively fill in pieces until you have a working Game of Life simulator. To aid in this process a lot more structure will be given than previous assignments. Accordingly, it is important to read the entire assignment instructions! It will answer a lot of the questions you might have.

Suggested Approach

It is suggested that you proceed with your program in the following order, testing at each step. Many of the steps have more detailed comments of what to do inside the starting code.

Copy the starting code at the bottom of this page.

Write the code for the two prompting functions.

Write code to allocate the two 2D arrays used.

Write code to free up the memory allocated for the two 2D arrays.

Write code to initialize the two arrays. It is recommended that you have current be all false and next be all true.

Write the code to print out a 2D array.

Write the code to swap the two 2D arrays. By setting one to true and the other false, testing this function will be easy as you will see an observable difference.

Uncomment the call to the setup_blinker function. This is the simplest Game of Life structure.

Write the alive function.

Write the game_of_life function.

Uncomment the remaining setup function and verifying that each new structure works one at a time.

The Patterns

We have provided for you multiple interesting patterns to watch as you develop your program. The functions to populate these patterns are provided to you, so you don't need to meticulously program them in. The locations in the provided code has been carefully chosen to allow for all of the patterns to exist on the screen.

Each of the following images are from Wikipedia and licensed under a CC-SA license or public domain.

Blinker (Period 2)

image text in transcribed

Toad (Period 2)

image text in transcribed

Pulsar (Period 3)

image text in transcribed

Pentadecathlon (Period 15)

image text in transcribed

Gosper Glider Gun (Period 30)

image text in transcribed

Hints

In the starting code there are two locations where ANSI escape code is suggested. These are special sequences that allow for modifying the output of a program in a terminal. They allow for changing colors and manipulating the cursor. It is recommended that you just use the provided sequences as is. To use them, youy code should do something like:

if (board[i][j]) { // print provided ANSI sequence } else { // print a space } 

To allocate a 2D array, you first need to allocate the rows as in:

int **arr2d; arr2d = malloc(num_rows * sizeof(int *)); 

then for each row, you need to allocate columns as in:

for (int i = 0; i  

To free a 2D array, you follow the same process as allocating, except you do it in reverse, as in:

for (int i = 0; i  

To have a prompt that continually prompts a user to enter something valid, it can be done like:

while (true) { printf("Prompt (A/B): "); char answer[21]; scanf("%20s", answer); if (strcmp(answer, "A") == 0 || strcmp(answer, "a") == 0) { return true; } if (strcmp(answer, "B") == 0 || strcmp(answer, "b") == 0) { return false; } printf("That is neither an A nor a B. "); } 

Starting Code

#include  #include  #include  #include  //  is needed for a call to usleep which is needed for animation. // This is not standard C, but from what is known as POSIX, which is // supported by OSX and Cygwin #include  bool animate_or_prompt(bool animate); bool animation_prompt(void); bool more_generations_prompt(void); void print_board(bool **board, int rows, int cols); void swap(bool ***a, bool ***b); void game_of_life(bool **current, bool **next, int rows, int cols); bool alive(bool **grid, int row, int col); void setup_blinker(bool **board, int row_off, int col_off); void setup_toad(bool **board, int row_off, int col_off); void setup_pentadecathlon(bool **board, int row_off, int col_off); void setup_gosper_glider_gun(bool **board); void setup_pulsar(bool **board, int row_off, int col_off); // int main(int argc, char **argv) { int main() { const int ROWS = 33; const int COLS = 44; // Initialize the boards bool **current; bool **next; // Allocate memory for current and next using malloc // --- Put your code here --- // Initialize current and next so that each entry of current is false // and each entry of next is true. This is so that initially you can // ensure current and next are correctly allocated and printing before // game_of_life is working since it should show a simple alternating // sequence. // --- Put your code here --- // Setup fancy board stuff // Uncomment these one at a time as you are working. Only introduce a new // one once the previous one is working // setup_blinker(current, 8, 40); // Period 2 // setup_toad(current, 12, 38); // Period 2 // setup_pulsar(current, 16, 13); // Period 3 // setup_pentadecathlon(current, 13, 0); // Period 15 // setup_gosper_glider_gun(current); // Period 30 // Ask the user if they would like to see step-by-step or an animation bool animate = animation_prompt(); // Loop through until the user says otherwise for (int generation_count = 1; animate_or_prompt(animate); ++generation_count) { printf("Generation %d (Press CTRL+C to quit) ", generation_count); // Print the board // --- Put your code here --- // Call the game_of_life // --- Put your code here --- // Swap current and next // --- Put your code here --- } printf("Simulation complete "); // Free up memory used by current and next // --- Put your code here --- return 0; } bool animate_or_prompt(bool animate) { if (!animate) { return more_generations_prompt(); } // Clear screen using ANSI escape code // escape, clear screen, escape move cursor to 1,1 printf("\033[2J\033[1;1H"); // Sleep for 0.1 seconds usleep(100000); // 100000 ms = 0.1 seconds return true; } bool animation_prompt(void) { // This function should prompt the user with: // "Would you like to see an animation (Y) Or step-by-step (N)? " // It should return true if the answer is "Y" or "y" // It should return false if the answer is "N" or "n" // If invalid input is entered, a message indicating invalid input // should be printed and the prompt should be repeated until // valid input is given. // --- Put your code here --- } bool more_generations_prompt() { // This function should prompt the user with: // "Would you like to continue? (Y/N) " // It should return true if the answer is "Y" or "y" // It should return false if the answer is "N" or "n" // If invalid input is entered, a message indicating invalid input // should be printed and the prompt should be repeated until // valid input is given. // --- Put your code here --- } void print_board(bool **board, int rows, int cols) { // This function should print out the 2D array board. // If you would like to print out a color block instead of a character, // use // printf("\033[7m \033[0m"); // This is an ANSI escape sequence which will print out a solid block of // color that is the opposite color of your terminal. That is if the // background of your terminal is normally black, it will be white, and // vice versa. // --- Put your code here --- } void swap(bool ***a, bool ***b) { // This function should swap the contents of a and b // --- Put your code here --- } void game_of_life(bool **current, bool **next, int rows, int cols) { // Clear out next by going through each row and column and set // every value to false // --- Put your code here --- // Calculate next by going through each row and column, except for the // borders, and set the ith, jth entry in next to the result of the // function alive based upon current at i, j // --- Put your code here --- } bool alive(bool **grid, int row, int col) { // Rules: // * If a cell has less than 2 live neighbors, it will die (or stay dead) // * If a cell has 2 live neighbors, it will stay its current state // * If a cell has 3 live neighbors, it will be alive // (either by staying alive or coming alive) // * If a cell has move than 3 live neighbors, it will die (or stay dead) // // Live neighbors are calculated by looking at its octile neighbors. That is // for a cell X, you look at the neighbors A-H as in: // // A B C // D X E // F G H // // Note that a cell does not itself count towards its own live neighbor count // --- Put your code here --- } // The following setup functions do not need to be modified. void setup_blinker(bool **board, int row_off, int col_off) { board[row_off][col_off - 1] = board[row_off][col_off] = board[row_off][col_off + 1] = true; } void setup_toad(bool **board, int row_off, int col_off) { board[2 + row_off][2 + col_off] = board[2 + row_off][3 + col_off] = board[2 + row_off][4 + col_off] = true; board[3 + row_off][1 + col_off] = board[3 + row_off][2 + col_off] = board[3 + row_off][3 + col_off] = true; } void setup_pentadecathlon(bool **board, int row_off, int col_off) { board[4 + row_off][5 + col_off] = board[5 + row_off][5 + col_off] = true; board[7 + row_off][5 + col_off] = board[8 + row_off][5 + col_off] = board[9 + row_off][5 + col_off] = board[10 + row_off][5 + col_off] = true; board[12 + row_off][5 + col_off] = board[13 + row_off][5 + col_off] = true; board[6 + row_off][4 + col_off] = board[6 + row_off][6 + col_off] = true; board[11 + row_off][4 + col_off] = board[11 + row_off][6 + col_off] = true; } void setup_gosper_glider_gun(bool **board) { board[6][1] = board[7][1] = board[6][2] = board[7][2] = true; board[4][35] = board[5][35] = board[4][36] = board[5][36] = true; board[4][13] = board[4][14] = true; board[5][12] = board[5][16] = true; board[6][11] = board[6][17] = true; board[7][11] = board[7][15] = board[7][17] = board[7][18] = true; board[8][11] = board[8][17] = true; board[9][12] = board[9][16] = true; board[10][13] = board[10][14] = true; board[2][25] = true; board[3][23] = board[3][25] = true; board[4][21] = board[4][22] = true; board[5][21] = board[5][22] = true; board[6][21] = board[6][22] = true; board[7][23] = board[7][25] = true; board[8][25] = true; } void setup_pulsar(bool **board, int row_off, int col_off) { board[2 + row_off][4 + col_off] = board[2 + row_off][5 + col_off] = board[2 + row_off][6 + col_off] = true; board[2 + row_off][10 + col_off] = board[2 + row_off][11 + col_off] = board[2 + row_off][12 + col_off] = true; board[7 + row_off][4 + col_off] = board[7 + row_off][5 + col_off] = board[7 + row_off][6 + col_off] = true; board[7 + row_off][10 + col_off] = board[7 + row_off][11 + col_off] = board[7 + row_off][12 + col_off] = true; board[9 + row_off][4 + col_off] = board[9 + row_off][5 + col_off] = board[9 + row_off][6 + col_off] = true; board[9 + row_off][10 + col_off] = board[9 + row_off][11 + col_off] = board[9 + row_off][12 + col_off] = true; board[14 + row_off][4 + col_off] = board[14 + row_off][5 + col_off] = board[14 + row_off][6 + col_off] = true; board[14 + row_off][10 + col_off] = board[14 + row_off][11 + col_off] = board[14 + row_off][12 + col_off] = true; board[4 + row_off][2 + col_off] = board[5 + row_off][2 + col_off] = board[6 + row_off][2 + col_off] = true; board[4 + row_off][7 + col_off] = board[5 + row_off][7 + col_off] = board[6 + row_off][7 + col_off] = true; board[4 + row_off][9 + col_off] = board[5 + row_off][9 + col_off] = board[6 + row_off][9 + col_off] = true; board[4 + row_off][14 + col_off] = board[5 + row_off][14 + col_off] = board[6 + row_off][14 + col_off] = true; board[10 + row_off][2 + col_off] = board[11 + row_off][2 + col_off] = board[12 + row_off][2 + col_off] = true; board[10 + row_off][7 + col_off] = board[11 + row_off][7 + col_off] = board[12 + row_off][7 + col_off] = true; board[10 + row_off][9 + col_off] = board[11 + row_off][9 + col_off] = board[12 + row_off][9 + col_off] = true; board[10 + row_off][14 + col_off] = board[11 + row_off][14 + col_off] = board[12 + row_off][14 + col_off] = true; } 

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

What is the impact of doing that?

Answered: 1 week ago