Question

In: Computer Science

Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...

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

  • MUST correctly play the Game of Life
  • You MUST use this life.h header file to find definitions
  • You MUST use this life.c file as a starting point for your implementation
  • You MUST assume a grid of 15x15
  • The grid size MUST be square
  • Your program MUST start with an initial grid in which cells (5,5) (5,6) (5,7) and (6,6) are alive and all others one are not
  • Your program MUST let the game run for 15 rounds
  • Your program MUST print the game board after each round

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

source code:

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 */
}

Solutions

Expert Solution

Code:

/*
 * life.c
 *
 *  Created on: Sep 13, 2016
 *      Author: leune
 */
#include <stdio.h>
#include <stdlib.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 < 15; i++)
        {
                printf("Round: %d\n", i + 1);
                printBoard(board);
                playRound(board);

                sleep(1);
        }

        return 0;
}

void initBoard(int vBoard[][YSIZE])
{
        for (int i = 0; i < XSIZE; i++)
                for (int j = 0; j < YSIZE; j++)
                        vBoard[i][j] = DEAD;
}

void playRound(int vBoard[][YSIZE])
{
        int tmpBoard[XSIZE][YSIZE];
        initBoard(tmpBoard);

        // perform the algorithm on vBoard, but update tmpBoard
        // with the new state
        for (int x = 0; x < XSIZE; x++)
        {
                for (int y = 0; y < YSIZE; y++)
                {
                        int noOfNeighbors = neighbors(vBoard, x, y);

                        if (vBoard[x][y] == ALIVE)
                        {
                                // If a cell is alive

                                if (noOfNeighbors < 2)
                                        // If it has less than two living neighbors, it dies due to loneliness.
                                        tmpBoard[x][y] = DEAD;
                                else if (noOfNeighbors <= 3)
                                        // If it has two or three living neighbors, it lives to the next generation
                                        tmpBoard[x][y] = ALIVE;
                                else
                                        // If it has more than three living neighbors, it dies due to overpopulation.
                                        tmpBoard[x][y] = DEAD;
                        }
                        else
                        {
                                // If a cell is dead
                                if (noOfNeighbors == 3)
                                        // if it has exactly three live neighbors, it becomes alive due to reproduction.
                                        tmpBoard[x][y] = ALIVE;
                        }
                }
        }
        // copy tmpBoard over vBoard
        for (int x = 0; x < XSIZE; x++)
        {
                for (int y = 0; y < YSIZE; y++)
                {
                        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])
{
        for (int i = 0; i < XSIZE; i++)
        {
                for (int j = 0; j < YSIZE; j++)
                {
                        //print character '-' for DEAD and for alive 'O'
                        printf("%c ", vBoard[i][j] ? 'O' : '-');
                }
                printf("\n");
        }
}

output:

Playing 15 rounds.

Round: 1
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 2
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 3
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O - O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 4
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - O - O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 5
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - O - O - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 6
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - O - O - - - - - - - 
- - - - O - - - O - - - - - - 
- - - - - O - O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 7
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - O O - O O - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 8
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - O - - - O - - - - - - 
- - - - O - - - O - - - - - - 
- - - - O - - - O - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 9
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - O - O - O - - - - - - 
- - - O O O - O O O - - - - - 
- - - - O - O - O - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 10
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 11
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - O O O - - - O O O - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 12
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 13
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - O O O - - - O O O - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 14
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - O - - - - - O - - - - - 
- - - - - - - - - - - - - - - 
- - - - - O O O - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
Round: 15
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - O O O - - - O O O - - - - 
- - - - - - - - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - O - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 

Related Solutions

Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
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...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C 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...
1.- In this assignment you will implement a simulation of the interaction of user programs with...
1.- In this assignment you will implement a simulation of the interaction of user programs with the OS to execute an I/O operation on two different devices. User programs: User programs will communicate with DOIO (OS) to request an I/O operation. (This will simulate a system call) User programs will give to DOIO three parameters: User id, device number (dev is a random number in the range 1 and 2 that represents device one or device two) and an address...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Project #1 Designing and implementing the Game of Life The main task is to implement (in...
Project #1 Designing and implementing the Game of Life The main task is to implement (in java) a 2-D (or 3-D) Graphic version of the Game of Life
In C language Assignment Extend the Game of Life assignment to load its configuration from a...
In C language Assignment Extend the Game of Life assignment to load its configuration from a file. Functional Requirements MUST load the initial state from text file MUST load the number of rounds to play from text file COULD load the grid size from the file Nonfunctional Requirements MUST compile without warnings and error Source code: life.h /* * life.h * *  Created on: Sep 20, 2020 *      Author: Joker Zhong */ #ifndef LIFE_H_ #define LIFE_H_ #define XSIZE   15 #define YSIZE   15 #define DEFAULTROUNDS...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
Matlab Implement Fading Channel Simulation: Jakes Model with using Matlab
Matlab Implement Fading Channel Simulation: Jakes Model with using Matlab
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT