In: Computer Science
Assignment
Extend the Game of Life assignment to load its configuration from a file.
Functional Requirements
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");
}
}
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.