Question

In: Computer Science

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

  1. MUST load the initial state from text file
  2. MUST load the number of rounds to play from text file
  3. COULD load the grid size from the file

Nonfunctional Requirements

  1. 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 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 20, 2020

*      Author: Joker Zhong

*/

#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<rounds; 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 three live neighbors, it becomes alive due to reproduction.

                    tmpBoard[x][y] = ALIVE;

            }

            

        }

    }

    // 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])

{

    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");

    }   

}

Solutions

Expert Solution

I have moved all functions into life.h so thta our code will looks more good and it will be easy to understand for other person. text file contail height and weight of our 2D array then values of array and last we will give no. of rounds text file i am giving below.

life.h file

//#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[XSIZE][YSIZE])

{
for(int i = 0; i < XSIZE; i++)
for(int j = 0; j < YSIZE; j++)
vBoard[i][j] = DEAD;
}
// play a round; updates the cells on the board
void playRound(int vBoard[XSIZE][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 three live neighbors, it becomes alive due to reproduction.
tmpBoard[x][y] = ALIVE;
}
}
}
// copy tmpBoard over vBoard
for (int y=0; y < YSIZE; y++)
{
for (int x=0; x < XSIZE; x++)
{
vBoard[x][y] = tmpBoard[x][y];
}
}
}
// print the board
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");
}
}

// determine the number of neighbors
int neighbors(int vBoard[XSIZE][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;
}

/* 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)
{
if (x < 0 || x >= XSIZE)
return 0;
else
if (y < 0 || y >= YSIZE) return 0;
else
return 1;
}
life.c file

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "life.h"

int main()

{

int board[XSIZE][YSIZE];
initBoard(board);
printBoard(board);
int rounds;
int temp;

int h,w;
FILE* f = fopen("text.txt","r");
fscanf(f,"%1d %1d",&h, &w)

for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(fscanf(f,"%1d",&board[i][j])!=1){printf("not scan");}
  
}
}
fscanf(f,"%1d",&rounds);
fclose(f);
  

printf("initially borad \n");
printBoard(board);

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;

}

text.txt file
15 15
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 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 1 1 1 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 1 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
7


Related Solutions

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...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
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. 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...
#C language array must store at least 50 inputs Game Scores Assignment Outcomes: Demonstrate the ability...
#C language array must store at least 50 inputs Game Scores Assignment Outcomes: Demonstrate the ability to create a menu driven program. Demonstrate the ability to create and use a 2D array on the stack. Demonstrate the use of functions. Demonstrate good programming style. Program Specifications: Your program will read in "game scores" from the user. Each score consists of a score for YOUR team and a score for the OTHER team. You don't need names or dates for the...
Suppose ABC firm is considering an investment that would extend the life of one of its...
Suppose ABC firm is considering an investment that would extend the life of one of its facilities for 4 years. The project would require upfront costs of $7.4M plus $25.57M investment in equipment. The equipment will be obsolete in (N+2) years and will be depreciated via straight-line over that period (Assume that the equipment can't be sold). During the next 4 years, ABC expects annual sales of 71M per year from this facility. Material costs and operating expenses are expected...
Suppose ABC firm is considering an investment that would extend the life of one of its...
Suppose ABC firm is considering an investment that would extend the life of one of its facilities for 5 years. The project would require upfront costs of $11.36M plus $23.89M investment in equipment. The equipment will be obsolete in (N+2) years and will be depreciated via straight-line over that period (Assume that the equipment can't be sold). During the next 5 years, ABC expects annual sales of 69M per year from this facility. Material costs and operating expenses are expected...
suppose ABC firm is considering an investment that would extend the life of one of its...
suppose ABC firm is considering an investment that would extend the life of one of its facilities for 5 years. the project will require upfront cost of 8 m plus (35+2) 37 millions investment in equipment. The equipment will be obsolete in 2 years and will be depreciated via straight line over that period (assume that the equipment can’t be sold). During the next 5 years ABC expect annual sales of 55 M per year from this facility. Material cost...
Suppose ABC firm is considering an investment that would extend the life of one of its...
Suppose ABC firm is considering an investment that would extend the life of one of its facilities for 5 years. The project would require upfront costs of $8M plus $40M investment in equipment. The equipment will be obsolete in 9 years and will be depreciated via straight-line over that period (Assume that the equipment can't be sold). During the next 5 years, ABC expects annual sales of 63 M per year from this facility. Material costs and operating expenses are...
Assignment (C language, not C ++) Start a new project to store grade information for 5...
Assignment (C language, not C ++) Start a new project to store grade information for 5 students using structures. Declare a structure called student_t that contains : First name Last name Student ID Number Percentage grade Letter grade Create the following functions: getStudentInfo(void)- Declares a single student object - Uses printf()/scanf() to get keyboard input for Name, SID, and Percentage (not Letter). - Returns a single student structure calcStudentGrade(student_t *st_ptr)- Takes the pointer to a student (to avoid making a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT