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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void print(ofstream &output, bool board[60][82][82])
{
for (int r = 1; r < 81; r++)
{
for (int c = 1; c < 81; c++)
{
if (board[r][c] == false)
output << 'x';
else
output << 'o';
}
output << endl;
}
}
int neighbors(int row, int col, int level)
{
bool board[60][82][82];
int r, c, count = 0;
for (r = row - 1; r <= row + 1; ++r)
for (c = col - 1; c <= col + 1; ++c)
if (r == row && c == col)
continue;
if (board[r][c][level])
++count;
return count;
}
void life(ofstream &output, bool board[60][82][82])
{
int c, r, level, count = 0;
for (level = 0; level < 59; ++level)
{
for (r = 1; r < 81; ++r)
for (c = 1; c < 81; ++c)
{
count = neighbors(r, c, level);
if (board[r][c][level])
{
if (count == 2 || count == 3)
board[r][c][level + 1] = true;
}
else if (count == 3)
{
board[r][c][level + 1] = true;
}
}
}
}
int main()
{
bool board[60][82][82]{ false };
int c, r, level, cellnum, x, y;
string gen;
ifstream input("start.dat");
ofstream output("boards.txt");
input >> cellnum;
while (!input.eof())
{
for (r = 0; r < 82; ++r)
for (c = 0; c < 82; ++c)
for (level = 0; level < 60; ++level)
board[r][c][level] = false; // ISSUE SPOT
for (int i = 0; i < cellnum; i++) {
input >> x >> y;
board[0][y][x] = true;
}
if (level < 10)
gen = "#############";
else if (level >= 10 && level < 100)
gen = "##############";
output << gen << endl << "Generation " << level;
output << ":" << endl << gen << endl << endl;
if (level == 0)
print(output, board);
life(output, board);
level++;
input >> cellnum;
}
system("pause");
return 0;
}
|