Question

In: Computer Science

Assignment Extend the Game of Life assignment to load its configuration from a file. Functional Requirements...

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

CODE I HAVE:

#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]) {

/* write this functions */

for (int i = 0; i < XSIZE; i++)

for (int j = 0; j < YSIZE; j++)

//Sets it to dead values on the board

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 numNeighbors = neighbors(vBoard, x, y);

// If a cell is alive

if (vBoard[x][y] == ALIVE) {

//If it has less than two living neighbors, it dies.

if (numNeighbors < 2)

//SETS TO DEAD

tmpBoard[x][y] = DEAD;

//Else if it has two or three living neighbors, it lives and continues to the next generation

else if (numNeighbors <= 3)

//SETS TO ALIVE

tmpBoard[x][y] = ALIVE;

//Else if it has more than three living neighbors, it dies becuase of overpopulation

else

//SETS TO DEAD

tmpBoard[x][y] = DEAD;

}

//Else if a cell is dead

else {

// if it has exactly three living neighbors, it becomes alive due to reproduction.

if (numNeighbors == 3)

//Sets to ALIVE

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++) {

//prints dead characters as - and alive chracters as O

printf("%c ", vBoard[i][j] ? 'O' : '-');

}

//prints space to keep rounds seperate

printf("\n");

}

}

Solutions

Expert Solution

task is to write codes to read data from a file for initial state, number of rounds and grid size

So to make it more maintainable i will use three separate file

1. for loading initial state

2. for number of rounds

3. for grid size

first, lets write a function to read data from text file

string getDataFromFile(string filePath, int size){
/* open file for reading and writing */
char result[size];
/* open file for reading and writing */
fp = fopen(filePath, "w+");
   /* go to the beginning of the file */
fseek(fp, 0, SEEK_SET);
/*reads the data and stores it to result array*/
fread(result, size, 1, fp);
return result;

}

now as you get the data from text and you need to put it in integer variables
in stdlib.h there is a function called atoi which converts valid string characters to integers

so for DEFAULTROUNDS XISZE YSIZE and number of rounds

what you should do I give you one examle in the below
int xSize = atoi(getDataFromFile("xSize.txt",3));

int ySize=atoi(getDataFromFile("ySize.txt",3));

int defaultRounds = atoi(getDataFromFile("ySize.txt",5));

int numberOfRounds = atoi(getDataFromFile("ySize.txt",5));

I am not doing the entire refactoring of the code as this would be not good for the student. so leaving him /her with the task to call the method in appropriate position to run the entire code block.


Related Solutions

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...
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...
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++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design and implement what is known as the “Game of Life”, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.   The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cell’s life cycle...
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 Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT