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