Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// GameOfLife // // AFTER I COMPILE, NO OUTPUT SHOW UP // #include <iostream> #i

ID: 3722356 • Letter: #

Question

// GameOfLife

//

// AFTER I COMPILE, NO OUTPUT SHOW UP

//

#include <iostream>

#include <fstream>

using namespace std;

// Print the grid

void print(int cells[12][12], std::ostream &myfile)

{

  

myfile << " 0 1 2 3 4 5 6 7 8 9 10 11" << endl;

  

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

  

{

  

myfile << i << " ";

  

for(int j; j < 12; j++)

  

{

  

myfile << cells[i][j] << "";

  

}

  

myfile << endl;

  

}

  

}

// Check if i and j are valid indices for grid

bool isValid(int i, int j)

{

  

if(i >= 0 && i < 12 && j >= 0 && j < 12)

  

return true;

  

else

  

return false;

  

}

// Find no of live neighbors

int findActiveNeighbors(int cells[12][12], int i, int j)

{

  

int activeNeighbors = 0;

  

if(isValid(i - 1, j - 1) && cells[i - 1][j - 1] == 1)

  

activeNeighbors++;

  

if(isValid(i - 1, j) && cells[i - 1][j] == 1)

  

activeNeighbors++;

  

if(isValid(i - 1, j + 1) && cells[i - 1][j + 1] == 1)

  

activeNeighbors++;

  

if(isValid(i, j - 1) && cells[i][j - 1] == 1)

  

activeNeighbors++;

  

if(isValid(i, j + 1) && cells[i][j + 1] == 1)

  

activeNeighbors++;

  

if(isValid(i + 1, j - 1) && cells[i + 1][j - 1] == 1)

  

activeNeighbors++;

  

if(isValid(i + 1, j) && cells[i + 1][j] == 1)

  

activeNeighbors++;

  

if(isValid(i + 1, j + 1) && cells[i + 1][j + 1] == 1)

  

activeNeighbors++;

  

return activeNeighbors;

  

}

// This method simulates a round; it changes the original array

void simulate(int cells[12][12])

{

  

int newCells[12][12];

  

// Not processing border cells

  

for(int i = 1; i < 11; i++)

  

{

  

for(int j = 1; j < 11; j++)

  

{

  

// Find no of live neighbors

  

int n = findActiveNeighbors(cells, i, j);

  

if(cells[i][j] == 1) // if cell is live

  

{

  

if(n == 0 || n == 1)

  

{

  

newCells[i][j] = 0; // mark for deletion

  

}

  

else if(n >= 4)

  

{

  

newCells[i][j] = 0; // mark for deletion

  

}

  

else

  

{

  

newCells[i][j] = cells[i][j];

  

}

  

}

  

else if (n == 3)

  

{

  

newCells[i][j] = 1; // mark for generation

  

}

  

else

  

{

  

newCells[i][j] = cells[i][j];

  

}

  

}

  

}

  

// Restoring values into original array

  

for(int i = 1; i < 11; i++)

  

{

  

for(int j = 1; j < 11; j++)

  

{

  

cells[i][j] = newCells[i][j];

  

}

  

  

}

  

  

  

}

int main()

{

  

ofstream myfile;

  

myfile.open ("Output.txt");

  

int life[12][12] =

  

{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

  

   {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},

  

   {0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},

  

   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

  

   {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},

  

   {0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},

  

   {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},

  

   {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},

  

   {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},

  

   {0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},

  

   {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},

  

   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

  

myfile << " Game of Life ------------" << endl;

  

int round = 1;

  

myfile << " Original configuration " << endl;

  

print(life, myfile);

  

while(round <= 4)

  

{

  

myfile << " Round " << round << endl;

  

simulate(life);

  

print(life, myfile);

  

round++;

  

}

  

return 0;

}

Explanation / Answer

/*

* The Game of Life

*/

#include <stdio.h>

/* dimensions of the screen */

#define BOARD_WIDTH 79

#define BOARD_HEIGHT 24

/* set everthing to zero */

void initialize_board (int board[][BOARD_HEIGHT]) {

int i, j;

for (i=0; i<BOARD_WIDTH; i++) for (j=0; j<BOARD_HEIGHT; j++)

board[i][j] = 0;

}

/* add to a width index, wrapping around like a cylinder */

int xadd (int i, int a) {

i += a;

while (i < 0) i += BOARD_WIDTH;

while (i >= BOARD_WIDTH) i -= BOARD_WIDTH;

return i;

}

/* add to a height index, wrapping around */

int yadd (int i, int a) {

i += a;

while (i < 0) i += BOARD_HEIGHT;

while (i >= BOARD_HEIGHT) i -= BOARD_HEIGHT;

return i;

}

/* return the number of on cells adjacent to the i,j cell */

int adjacent_to (int board[][BOARD_HEIGHT], int i, int j) {

int k, l, count;

count = 0;

/* go around the cell */

for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)

/* only count if at least one of k,l isn't zero */

if (k || l)

if (board[xadd(i,k)][yadd(j,l)]) count++;

return count;

}

void play (int board[][BOARD_HEIGHT]) {

/*

(copied this from some web page, hence the English spellings...)

1.STASIS : If, for a given cell, the number of on neighbours is

exactly two, the cell maintains its status quo into the next

generation. If the cell is on, it stays on, if it is off, it stays off.

2.GROWTH : If the number of on neighbours is exactly three, the cell

will be on in the next generation. This is regardless of the cell's current state.

3.DEATH : If the number of on neighbours is 0, 1, 4-8, the cell will

be off in the next generation.

*/

int i, j, a, newboard[BOARD_WIDTH][BOARD_HEIGHT];

/* for each cell, apply the rules of Life */

for (i=0; i<BOARD_WIDTH; i++) for (j=0; j<BOARD_HEIGHT; j++) {

a = adjacent_to (board, i, j);

if (a == 2) newboard[i][j] = board[i][j];

if (a == 3) newboard[i][j] = 1;

if (a < 2) newboard[i][j] = 0;

if (a > 3) newboard[i][j] = 0;

}

/* copy the new board back into the old board */

for (i=0; i<BOARD_WIDTH; i++) for (j=0; j<BOARD_HEIGHT; j++) {

board[i][j] = newboard[i][j];

}

}

/* print the life board */

void print (int board[][BOARD_HEIGHT]) {

int i, j;

/* for each row */

for (j=0; j<BOARD_HEIGHT; j++) {

/* print each column position... */

for (i=0; i<BOARD_WIDTH; i++) {

printf ("%c", board[i][j] ? 'x' : ' ');

}

/* followed by a carriage return */

printf (" ");

}

}

/* read a file into the life board */

void read_file (int board[][BOARD_HEIGHT], char *name) {

FILE *f;

int i, j;

char s[100];

f = fopen (name, "r");

for (j=0; j<BOARD_HEIGHT; j++) {

/* get a string */

fgets (s, 100, f);

/* copy the string to the life board */

for (i=0; i<BOARD_WIDTH; i++) {

board[i][j] = s[i] == 'x';

}

}

fclose (f);

}

/* main program */

int main (int argc, char *argv[]) {

int board[BOARD_WIDTH][BOARD_HEIGHT], i, j;

initialize_board (board);

read_file (board, argv[1]);

/* play game of life 100 times */

for (i=0; i<100; i++) {

print (board);

play (board);

/* clear the screen using VT100 escape codes */

puts ("");

}

}