Question

Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT...

Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT DESCRIBED BELOW).

Title: Game of Life Description In this assignment, you will code a simulation called "Game of Life". It is a very famous 'game' and the formed the basis of an entire field of simulation models called "cellular automata". Before continuing reading this assignment, I suggest you read a bit more about Game of Life at: https://en.wikipedia.org/wiki/Conway's_Game_of_Life or http://www.math.com/students/wonders/life/life.html (the latter also has a nice Java applet of the game that is quite fun to play with!)

You should now know about the basics of game of life: it is a 'simulation' of cells that are 'dead' or 'alive' in discrete time steps, at every step all of the cells will get computed a new value in parallel (i.e., based on the values of the neighbours at the *previous* time step - this will require a little bit of thinking!), and the rules to determine if a cell is dead or alive are: -Any live cell with fewer than two live neighbours die, as if caused by under population. -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. You now need to write a program that reads in an initial 'board' configuration, and then performs a specified number of simulations. As always, try and keep your program clean, modular, and only allocate as much memory as you need (using malloc).

Input

The first line will specify 3 numbers: -the number of rows -the number of columns -the number of steps you need to simulate The remainder of the input file comprises the initial configuration, which will have exactly the specified number of rows and columns. Dead cells are indicated with '.' while live cells are marked with 'X'.

Output

As output, you will need to write the board configuration that results from the specified number of simulations, in exactly the same format as you read the input.

Sample Input

6 6 20

.X...X

X.X.X.

X...X.

X..XX.

..XX.X

...X.X

Sample Output

...X..

..X.X.

..X.X.

...X..

......

......

Thanx in advance, please the code needed to be in same input/output given.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>

#define ROWS 20
#define COLS 20

#define GETCOL(c) (c%COLS)
#define GETROW(c) (c/COLS)

#define D_LEFT(c) ((GETCOL(c) == 0) ? (COLS-1) : -1)
#define D_RIGHT(c) ((GETCOL(c) == COLS-1) ? (-COLS+1) : 1)
#define D_TOP(c) ((GETROW(c) == 0) ? ((ROWS-1) * COLS) : -COLS)
#define D_BOTTOM(c) ((GETROW(c) == ROWS-1) ? (-(ROWS-1) * COLS) : COLS)

typedef struct _cell
{
struct _cell* neighbour[8];
char curr_state;
char next_state;
} cell;

typedef struct
{
int rows;
int cols;
cell* cells;
} world;

void evolve_cell(cell* c)
{
int count=0, i;
for (i=0; i<8; i++)
{
if (c->neighbour[i]->curr_state) count++;
}
if (count == 3 || (c->curr_state && count == 2)) c->next_state = 1;
else c->next_state = 0;
}

void update_world(world* w)
{
int nrcells = w->rows * w->cols, i;
for (i=0; i<nrcells; i++)
{
evolve_cell(w->cells+i);
}
for (i=0; i<nrcells; i++)
{
w->cells[i].curr_state = w->cells[i].next_state;
if (!(i%COLS)) printf("\n");
printf("%c",w->cells[i].curr_state ? '*' : ' ');
}
}

world* init_world()
{
world* result = (world*)malloc(sizeof(world));
result->rows = ROWS;
result->cols = COLS;
result->cells = (cell*)malloc(sizeof(cell) * COLS * ROWS);

int nrcells = result->rows * result->cols, i;

for (i = 0; i < nrcells; i++)
{
cell* c = result->cells + i;

c->neighbour[0] = c+D_LEFT(i);
c->neighbour[1] = c+D_RIGHT(i);
c->neighbour[2] = c+D_TOP(i);
c->neighbour[3] = c+D_BOTTOM(i);
c->neighbour[4] = c+D_LEFT(i) + D_TOP(i);
c->neighbour[5] = c+D_LEFT(i) + D_BOTTOM(i);
c->neighbour[6] = c+D_RIGHT(i) + D_TOP(i);
c->neighbour[7] = c+D_RIGHT(i) + D_BOTTOM(i);

c->curr_state = rand() % 2;
}
return result;
}

int main()
{
srand(3);
world* w = init_world();

while (1)
{
system("CLS");
update_world(w);
getchar();
}
}

OUTPUT :

XXX...XX...X.......X

X..X...X...X.X.X...X

X.........XX.X.X.XXX

XX....XXX...XX.X....

..........XXX.......

X.XXXXXXX.XX.XX.....

XXX.......X..XX.....

XXXXX.XXX.........X.

..............X..X..

.X....X.X.........X.

.X.......X.X.X....XX

X...X..X....XX....XX

..XX..XX............

..XXX.X..X..XX......

......X....XX.X.....

...X...X....X......X

..........XX......XX

X....XX..XXX.X...XXX

X.....XXXX.........X

.....X.....X...X....

Add a comment
Know the answer?
Add Answer to:
Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

  • I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....

    I am really struggling with how to approach this program. Conway's Game of life, whereby, 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Submit...

  • using c++ program the following The Assignment For this assignment, we are asking you to implement...

    using c++ program the following The Assignment For this assignment, we are asking you to implement Conway's Game of Life using a two-dimensional array as a grid in which to store the cells. Live cells are denoted as * characters, dead cells are denoted by the character. Input As input, you will be given a line containing the number of rows, number of columns, and number of generations to simulate in the form mng followed by m lines containing the...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: 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 overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT