So I am having trouble getting the game of life to run. Our instructor gave us the first code, the second code is what we were supposed to right which is an implementation run by the third code which was also given by the instructor.
Right now when ever I try to run the 3rd code in visual studio, my command window is just blank and doesn't not output anything. You can pretty much ignore the first code it just tells sets the rows and columns to 25. The 3rd code cannot be changed so I guess it is just trying to get the 2nd code (my code) to be used correctly to implement the 3rd code.
#ifndef LIFE_H
#define LIFE_H
constint ROWS = 25;
constint COLS = 25;
// Simulate one generation of Conways Game of Life
//
// Given:
// a constant 2D bool array called "current" where each true element
// indicates a live cell and each false element indicates a dead cell.
//
// an empty 2D bool array called "next"
//
// Desired:
// the next generation of the game of life written into the "next" array
//
// "current" should remain unchanged
//
// The rules of the Game Of Life are as follows:
//
// 1. Any live cell with fewer than two live neighbours dies, as if
// caused by under-population.
//
// 2. Any live cell with two or three live neighbours lives on to the
// next generation.
//
// 3. Any live cell with more than three live neighbours dies, as if by
// overcrowding.
//
// 4. Any dead cell with exactly three live neighbours becomes a live cell,
// as if by reproduction.
void life(constbool current[ROWS][COLS], bool next[ROWS][COLS]);
#endif
#include <iostream>
#include "life.h"
constint GENERATIONS = 100;
usingnamespace std;
//make a random array of initial living cells
void gen(bool a[ROWS][COLS]) {
for (int i = 0; i<ROWS; ++i) {
for (int j = 0; j<COLS; ++j) {
if (rand() % 100<10)a[i][j] = true;
else a[i][j] = false;
}
}
a[5][5] = true;
a[5][6] = true;
a[5][7] = true;
return;
}
// check to see if two arrays are equal
bool equal(constbool a[ROWS][COLS], constbool b[ROWS][COLS]) {
int i, j;
for (i = 0; i<ROWS; ++i)for (j = 0; j<COLS; ++j)if (a[i][j] != b[i][j])returnfalse;
returntrue;
}
//copy the b array into the a array
void copy(bool a[ROWS][COLS], constbool b[ROWS][COLS]) {
for (int i = 0; i<ROWS; ++i) {
for (int j = 0; j<COLS; ++j) {
a[i][j] = b[i][j];
}
}
return;
}
//print out the array
void print(constbool a[ROWS][COLS]) {
for (int i = 0; i<ROWS; ++i) {
for (int j = 0; j<COLS; ++j) {
if (a[i][j])cout << 'X';
else cout << ' ';
}
cout << endl;
}
return;
}
int main() {
bool current[ROWS][COLS];
bool next[ROWS][COLS];
int i = 0;
//initialze the cell array and print it out
gen(current);
print(current);
while (i<GENERATIONS) {
//get a carriage return before the next generation
cin.get();
//give the current generation to life()
//it puts the next generation into next
life(current, next);
//copy the next generation into the current
copy(current, next);
//print it out
print(current);
i++;
}
return 0;
}