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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#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;
//}
|