In: Computer Science
Assignment
Implement Conway’s Game of Life.
The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows:
If a cell is alive:
If it has less than two living neighbors, it dies due to loneliness.
If it has two or three living neighbors, it lives to the next generation
If it has more than three living neighbors, it dies due to overpopulation.
If a cell is dead:
and, if it has exactly three live neighbors, it becomes alive due to reproduction.
After each simulation round, your program must print the updated game board to screen.
Functional Requirements
Nonfunctional Requirements
Hint
When doing a simulation run, define a second two-dimensional array. For each cell in the existing array, count the number of neighbors it has, and decide what its fate will be on the new board. After having done this for all cells, copy the value of your new board over the old one
life.h
/* * life.h * * Created on: Sep 13, 2016 * Author: leune */ #ifndef LIFE_H_ #define LIFE_H_ #define XSIZE 15 #define YSIZE 15 #define DEFAULTROUNDS 15 #define ALIVE 1 #define DEAD 0 // initialize the board to all dead cells void initBoard(int vBoard[][YSIZE]); // play a round; updates the cells on the board void playRound(int vBoard[][YSIZE]); // print the board void printBoard(int vBoard[][YSIZE]); // determine the number of neighbors int neighbors(int vBoard[][YSIZE], int x, int y); /* determine if the given coordinates are within bounds * returns 0 if the cell is out of bounds; returns 1 if * the cell is in bounds */ int onBoard(int x, int y); #endif /* LIFE_H_ */
life.c
/* * life.c * * Created on: Sep 13, 2016 * Author: leune */ #include <stdio.h> #include <unistd.h> #include "life.h" int main(int argc, char *argv[]) { int board[XSIZE][YSIZE]; int rounds = DEFAULTROUNDS; initBoard(board); board[5][5] = ALIVE; board[5][6] = ALIVE; board[5][7] = ALIVE; board[6][6] = ALIVE; printf("Playing %d rounds.\n\n", rounds); for (int i=0; i<rounds; i++) { printf("Round: %d\n", i+1); printBoard(board); playRound(board); sleep(1); } return 0; } void initBoard(int vBoard[][YSIZE]) { /* write this function */ } void playRound(int vBoard[][YSIZE]) { int tmpBoard[XSIZE][YSIZE]; initBoard(tmpBoard); // perform the algorithm on vBoard, but update tmpBoard // with the new state /* write this fragment */ // copy tmpBoard over vBoard for (int y=0; y < YSIZE; y++) { for (int x=0; x < XSIZE; x++) { vBoard[x][y] = tmpBoard[x][y]; } } } int onBoard(int x, int y) { if (x < 0 || x >= XSIZE) return 0; else if (y < 0 || y >= YSIZE) return 0; else return 1; } int neighbors(int vBoard[][YSIZE], int x, int y) { int n=0; int xp1 = x + 1; int xm1 = x - 1; int yp1 = y + 1; int ym1 = y - 1; if (onBoard(xm1, y) && vBoard[xm1][y] == ALIVE) n++; if (onBoard(xm1, yp1) && vBoard[xm1][yp1] == ALIVE) n++; if (onBoard(x, yp1) && vBoard[x][yp1] == ALIVE) n++; if (onBoard(xp1, yp1) && vBoard[xp1][yp1] == ALIVE) n++; if (onBoard(xp1, y) && vBoard[xp1][y] == ALIVE) n++; if (onBoard(xp1, ym1) && vBoard[xp1][ym1] == ALIVE) n++; if (onBoard(x, ym1) && vBoard[x][ym1] == ALIVE) n++; if (onBoard(xm1, ym1) && vBoard[xm1][ym1] == ALIVE) n++; return n; } void printBoard(int vBoard[XSIZE][YSIZE]) { /* write this fragment */ }
Dear student, below is the answer as asked by you.
The files that you said are also attatched within it. I hope it helps you.
/* * life.h * * Created on: Sep 13, 2016 * Author: leune */ #ifndef LIFE_H_ #define LIFE_H_ #define XSIZE 15 #define YSIZE 15 #define DEFAULTROUNDS 15 #define ALIVE 1 #define DEAD 0 // initialize the board to all dead cells void initBoard(int vBoard[][YSIZE]); // play a round; updates the cells on the board void playRound(int vBoard[][YSIZE]); // print the board void printBoard(int vBoard[][YSIZE]); // determine the number of neighbors int neighbors(int vBoard[][YSIZE], int x, int y); /* determine if the given coordinates are within bounds * returns 0 if the cell is out of bounds; returns 1 if * the cell is in bounds */ int onBoard(int x, int y); #endif /* LIFE_H_ */
/* * life.c * * Created on: Sep 13, 2016 * Author: leune */ #include <stdio.h> #include <unistd.h> #include "life.h" int main(int argc, char *argv[]) { int board[XSIZE][YSIZE]; int rounds = DEFAULTROUNDS; initBoard(board); board[5][5] = ALIVE; board[5][6] = ALIVE; board[5][7] = ALIVE; board[6][6] = ALIVE; printf("Playing %d rounds.\n\n", rounds); for (int i=0; i<rounds; i++) { printf("Round: %d\n", i+1); printBoard(board); playRound(board); sleep(1); } return 0; } void initBoard(int vBoard[][YSIZE]) { /* write this function */ } void playRound(int vBoard[][YSIZE]) { int tmpBoard[XSIZE][YSIZE]; initBoard(tmpBoard); // perform the algorithm on vBoard, but update tmpBoard // with the new state /* write this fragment */ // copy tmpBoard over vBoard for (int y=0; y < YSIZE; y++) { for (int x=0; x < XSIZE; x++) { vBoard[x][y] = tmpBoard[x][y]; } } } int onBoard(int x, int y) { if (x < 0 || x >= XSIZE) return 0; else if (y < 0 || y >= YSIZE) return 0; else return 1; } int neighbors(int vBoard[][YSIZE], int x, int y) { int n=0; int xp1 = x + 1; int xm1 = x - 1; int yp1 = y + 1; int ym1 = y - 1; if (onBoard(xm1, y) && vBoard[xm1][y] == ALIVE) n++; if (onBoard(xm1, yp1) && vBoard[xm1][yp1] == ALIVE) n++; if (onBoard(x, yp1) && vBoard[x][yp1] == ALIVE) n++; if (onBoard(xp1, yp1) && vBoard[xp1][yp1] == ALIVE) n++; if (onBoard(xp1, y) && vBoard[xp1][y] == ALIVE) n++; if (onBoard(xp1, ym1) && vBoard[xp1][ym1] == ALIVE) n++; if (onBoard(x, ym1) && vBoard[x][ym1] == ALIVE) n++; if (onBoard(xm1, ym1) && vBoard[xm1][ym1] == ALIVE) n++; return n; } void printBoard(int vBoard[XSIZE][YSIZE]) { /* write this fragment */ }
// A simple Java program to implement Game of Life
public class GameOfLife
{
public static void main(String[] args)
{
int M = 15, N = 15;
// Intiliazing the grid.
int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
// Displaying the grid
System.out.println("Original Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (grid[i][j] == 0)
System.out.print(".");
else
System.out.print("*");
}
System.out.println();
}
System.out.println();
nextGeneration(grid, M, N);
}
// Function to print next generation
static void nextGeneration(int grid[][], int M, int N)
{
int[][] future = new int[M][N];
// Loop through every cell
for (int l = 1; l < M - 1; l++)
{
for (int m = 1; m < N - 1; m++)
{
// finding no Of Neighbours that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += grid[l + i][m + j];
// The cell needs to be subtracted from
// its neighbours as it was counted before
aliveNeighbours -= grid[l][m];
// Implementing the Rules of Life
// Cell is lonely and dies
if ((grid[l][m] == 1) && (aliveNeighbours < 2))
future[l][m] = 0;
// Cell dies due to over population
else if ((grid[l][m] == 1) && (aliveNeighbours > 3))
future[l][m] = 0;
// A new cell is born
else if ((grid[l][m] == 0) && (aliveNeighbours == 3))
future[l][m] = 1;
// Remains the same
else
future[l][m] = grid[l][m];
}
}
System.out.println("Next Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (future[i][j] == 0)
System.out.print(".");
else
System.out.print("*");
}
System.out.println();
}
}
}