Chromatic 0 Newbie Poster

Hi, I've been trying to get Conway's Game of Life code to work now for the past two days with no avail!
The rules of the game are:
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any live cell with two or three live neighbours lives on to the next generation.
Any dead cell with exactly three live neighbours becomes a live cell.

What I want my program to do is to edit a .txt with an initial configuration of cells in a 22X80 block of * and place it into an array. It then takes that array and puts it another .txt. It then applies the rules of life and displays.

However, my program fails to apply the rules properly. Any help is much appreciated! Thank you!

#include <graphics.h>
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;


class Life{
char data, life_array [22][81], final_array[22][81];
int neighbors;
ifstream fin;
ofstream fout;

public:
void Clear();
void CurrentGen();
void NextGen();
void Display();
};

void Life::Clear()
{
fin.open("clear.txt");
if (fin.fail())
{
 cout << "clear.txt(Clear) failed to open." << endl;
 exit(1);
}

fout.open("omega.txt");
if (fout.fail())
{
cout << "omega.txt(Clear) failed to open." << endl;
exit(1);
}

for (int i = 0; i < 22; i ++)
{
    for (int k = 0; k < 80; k++)
    {
        fin.get(data);
        life_array [i][k] = data;
        //cout << life_array [i][k];
        fout << life_array [i][k];
    }
}
fin.close();
fout.close();
}

void Life::CurrentGen()
{
fin.open("omega.txt");
if (fin.fail())
{
 cout << "omega.txt(CurrentGen) failed to open." << endl;
 exit(1);
}

fout.open("output.txt");
if (fout.fail())
{
cout << "output.txt(CurrentGen) failed to open." << endl;
exit(1);
}

for (int i = 0; i < 22; i ++)
{
    for (int k = 0; k < 80; k++)
    {
        fin.get(data);
        life_array [i][k] = data;
        fout << life_array[i][k];
        //cout << life_array[i][k];
    }
}
fin.close();
fout.close();
}

void Life::NextGen()
{
fin.open("output.txt");
if (fin.fail())
{
 cout << "output.txt(NextGen) failed to open." << endl;
 exit(1);
}
fout.open("omega.txt");

if (fout.fail())
{
cout << "omega.text(NextGen) failed to open." << endl;
exit(1);
}

for (int i = 0; i < 22; i ++)
{
    for (int k = 0; k < 80; k++)
    {
        fin.get(data);
        life_array [i][k] = data;
        final_array [i][k] = data;

        int neighbors = 0;
        if (life_array[i-1][k-1] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i-1][k] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i-1][k+1] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i][k-1] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i][k+1] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i+1][k-1] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i+1][k] == '^')
        {
            neighbors += 1;
        }
        if (life_array[i+1][k+1] == '^')
        {
            neighbors += 1;
        }

        if (life_array[i][k] == '^' && neighbors < 2)
        {
            final_array[i][k] = '*';
        }
        else if (life_array[i][k] == '^' && neighbors > 3)
        {
            final_array[i][k] = '*';
        }
        else if (life_array[i][k] == '^' && (neighbors == 2 || neighbors == 3))
        {
            final_array[i][k] = '^';
        }
        else if (life_array[i][k] == '^' && neighbors == 3)
        {
            final_array[i][k] = '^';
        }

        fout << final_array [i][k];
        //cout << final_array [i][k];
    }
}
fin.close();
fout.close();
}

void Life::Display()
{
fin.open("omega.txt");
if (fin.fail())
{
cout << "omega.txt(Display) failed to open." << endl;
exit(1);
}

for (int i = 0; i < 22; i ++)
{
    for (int k = 0; k <= 80; k++)
    {
        fin.get(data);
        final_array [i][k] = data;
        cout << final_array [i][k];
    }
}
}

int main()
{
int start = 0;
char choice;
Life run1;

cout << "WELCOME TO ZOMBIE APOCALYPSE" << endl << endl << "WILL HUMANITY SURVIVE?" << endl << endl;
cout << "The Rules of THE GAME:" << endl;
cout << "This is a zombie: * " << endl;
cout << "This is a person: ^ " << endl;
cout << "1. Any person with fewer then two neighbors commits suicide." << endl;
cout << "2. Any person with three or more neighbors gets " << "fed up " << endl << "and ditches the group, but later becomes a zombie." << endl;
cout << "3. Any person with two or more neighbors lives." << endl;
cout << "4. Any zombie with three people near will stand up to attack." << endl;
run1.CurrentGen();
run1.NextGen();
run1.Display();
return 0;
}
//cout << "Hit [1] to begin." << endl;
//cin >> start;

//do{
//run1.CurrentGen();
//run1.NextGen();
//run1.Display();
//}while(start == 1);

//cout << "Go again? Hit [g] to continue, [c] to clear, or any other key to quit.";
//cin >> choice;

//switch (choice)
//{
//case 'g':
//{
//run1.CurrentGen();
//run1.NextGen();
//run1.Display();
//break;
//}

//case 'c':
//{
//run1.Clear();
//break;
//}

//default:
//{
//break;
//}
//}
//return 0;
//}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.