So, I have an assignment to do that involves arrays and the Conway's Game of Life. We were advised to use file streams for this assignment. However, something in my program isn't working right...I have a section in my code in one function where it checks the cells surrounding the array and determines whether the cell lives or dies.
The giant underscore in my code is where it seems to have a problem. The couts in the next few if-statements are just there to see which one is causing the problem.
An image to the output can be found below. The first block is just the text from my assignment7.txt file, and the second block is the result after going through. (Asterisks are living cells, dashes are dead cells.)
#include <iostream>
#include <fstream>
#include <graphics.h>
usingnamespace std;
// 22 rows in the program
constint ROW = 22;
// Added one to COLUMN to include '\n'
constint COLUMN = 51;
char world[ROW][COLUMN];
char world2[ROW][COLUMN];
// Function prototypes
void nextGeneration();
void display();
int main()
{
// Initializes the ifstream object (fin for "file input")
ifstream fin;
fin.open("assignment7.txt");
// Initializes 2-D arrays
char reading;
int one, two;
char dummy;
dummy = 'n';
// If the file fails to open
if (fin.fail())
{
cout << "Failed to open assignment7.txt. The program will end." << "\n";
exit (1);
}
cout << "Press 'n' after every generation is displayed for the next generation." << "\n";
cout << "Press any other key to quit." << "\n";
// Loops through .get to read the characters into arrays
for (one = 0; one < ROW; one++)
{
for (two = 0; two < COLUMN; two++)
{
fin.get(reading);
if (reading == '\n')
{
// Makes it so it prints out a new line and reading does not set it as an array element
cout << "\n";
}
else
{
world[one][two] = reading;
cout << world[one][two];
}
}
}
fin.close();
cin >> dummy;
while ((dummy == 'n') || (dummy == 'N'))
{
nextGeneration();
display();
cin >> dummy;
}
return 0;
}
// Displays the next generation
void display()
{
int onedisp, twodisp;
for (onedisp = 0; onedisp < ROW; onedisp++)
{
for (twodisp = 0; twodisp < COLUMN; twodisp++)
{
cout << world[onedisp][twodisp];
// Prints out a new line when twodisp has increased to COLUMN - 1
if (twodisp == (COLUMN - 1))
{
cout << "\n";
}
}
}
}
// Decides which cells live or die
void nextGeneration()
{
int onefunc, twofunc;
int count;
int test;
for (onefunc = 0; onefunc < ROW; onefunc++)
{
for (twofunc = 0; twofunc < COLUMN; twofunc++)
{
count = 0;
// Counts to see if there are any other asterisks around the cell
if ((world[onefunc - 1][twofunc]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc + 1][twofunc]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc - 1][twofunc - 1]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc + 1][twofunc + 1]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc][twofunc - 1]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc][twofunc + 1]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc - 1][twofunc + 1]) == '*')
{
count = count + 1;
}
elseif ((world[onefunc + 1][twofunc - 1]) == '*')
{
count = count + 1;
}
// Dies due to underpopulation
// ______________________________________________________________
if ((world[onefunc][twofunc]) == '*' && (count < 2))
{
world[onefunc][twofunc] = '-';
cout << "h" << "\n";
}
// Dies due to overcrowding
elseif ((world[onefunc][twofunc]) == '*' && (count > 3))
{
world[onefunc][twofunc] = '-';
cout << "i" << "\n";
}
// If the cell is alive and has two or three neighbors, it remains living
elseif ((world[onefunc][twofunc]) == '*' && count == 2 || count == 3)
{
world[onefunc][twofunc] = '*';
cout << "j" << "\n";
}
// If the cell is dead and if there are EXACTLY three cells surrounding it, it is born
elseif ((world[onefunc][twofunc]) == '-' && count == 3)
{
world[onefunc][twofunc] = '*';
cout << "j" << "\n";
}
}
}
}