Распечатка для хранения каждой игры поколений жизни

Эй, ребята, мне нужна помощь с распечаткой в ​​формате fstream для каждой Игры поколений жизни. В настоящее время это только распечатка начальной сетки со всеми мертвыми ячейками.

Вот моя функция печати, и что я пытался.

void print(bool mat[][Row]) //Prints matrix to screen
{
ofstream myfile;
myfile.open("GameOfLife.txt");
cout << setw(3) << " ";
myfile<< setw(3) << " ";
for (int p = 0; 5*p < Row; p++) cout << setw(5) << 5*p+1;
for (int p = 0; 5*p < Row; p++) myfile << setw(5) << 5*p+1;
cout << endl;
myfile<< endl;
for (int m = 0; m < Col; m++)
{
cout << setw(3) << m+1;
myfile<< setw(3) << m+1;
for (int n = 0; n < Row; n++)
{
if (mat[m][n]) cout << "\xDB";
else cout << /*"\xB1"*/"-";
if (mat[m][n]) myfile << "\xDB";
else myfile << /*"\xB1"*/"-";
}
cout << endl;
myfile<<endl;
}
}

Вот где я называю это в основном

 do //Keep updating new generations
{
cout<<"Generation: "<<genX<<endl;
//myfile<<"Generation: "<<genX<<endl;
genX++;
clear(next);
calculate(now, next);
swap(now, next);
print(now);
cin>>cont;
}while(cont == -1);

Было интересно, как я могу заставить его печатать каждое поколение в файл.

редактировать: добавлен весь код

#include <iostream>
#include <fstream>
#include <iomanip>
#define Col 15 //Col size
#define Row 13 //Row size
using namespace std;

void clear(bool mat[][Row]) //resets with all dead cells
{
for (int m = 0; m < Col; m++)
{
for (int n = 0; n < Row; n++)
mat[m][n] = 0;
}
}

void print(bool mat[][Row]) //Prints matrix to screen
{
ofstream myfile;
myfile.open("GameOfLife.txt");
cout << setw(3) << " ";
myfile<< setw(3) << " ";
for (int p = 0; 5*p < Row; p++) cout << setw(5) << 5*p+1;
for (int p = 0; 5*p < Row; p++) myfile << setw(5) << 5*p+1;
cout << endl;
myfile<< endl;
for (int m = 0; m < Col; m++)
{
cout << setw(3) << m+1;
myfile<< setw(3) << m+1;
for (int n = 0; n < Row; n++)
{
if (mat[m][n]) cout << "\xDB";
else cout << /*"\xB1"*/"-";
if (mat[m][n]) myfile << "\xDB";
else myfile << /*"\xB1"*/"-";
}
cout << endl;
myfile<<endl;
}
}

/*void print2(unsigned int mat[][Row]) //Prints matrix to screen
{
for (int m = 0; m < Col; m++)
{
for (int n = 0; n < Row; n++)
cout << mat[m][n] << " ";
cout << endl;
}
}*/

void calculate(bool mata[][Row], bool matb[][Row])
{
unsigned int neighbors;
for (int m = 0; m < Col; m++)
{
for (int n = 0; n < Row; n++)
{
neighbors = 0;
//Begin counting number of neighbors:
if (mata[m%Col-1][n%Row-1] == 1) neighbors += 1;
if (mata[m%Col-1][n%Row] == 1) neighbors += 1;
if (mata[m%Col-1][n%Row+1] == 1) neighbors += 1;
if (mata[m%Col][n%Row-1] == 1) neighbors += 1;
if (mata[m%Col][n%Row+1] == 1) neighbors += 1;
if (mata[m%Col+1][n%Row-1] == 1) neighbors += 1;
if (mata[m%Col+1][n%Row] == 1) neighbors += 1;
if (mata[m%Col+1][n%Row+1] == 1) neighbors += 1;

//Apply rules to the cell:
if (mata[m%Col][n%Row] == 1 && neighbors < 2)
matb[m%Col][n%Row] = 0;
else if (mata[m%Col][n%Row] == 1 && neighbors > 3)
matb[m%Col][n%Row] = 0;
else if (mata[m%Col][n%Row] == 1 && (neighbors == 2 || neighbors == 3))
matb[m%Col][n%Row] = 1;
else if (mata[m%Col][n%Row] == 0 && neighbors == 3)
matb[m%Col][n%Row] = 1;
}
}
}

void swap(bool mata[][Row], bool matb[][Row]) //Replaces first matrix with second
{
for (int m = 0; m < Col; m++)
{
for (int n = 0; n < Row; n++)
mata[m%Col][n%Row] = matb[m%Col][n%Row];
}
}int main()
{
bool now[Col][Row], next[Col][Row]; //Creates now and then matrixes
int x, y, cont; //Used for user input

cout << left << "Welcome to Conway's Game of Life." << endl << endl;
cout << "The Rules of Life:" << endl;
cout << "1.cell with less than 2 neighbors die" << endl;
cout << "2. cell with more than three neighbors dies" << endl;
cout << "3. cell with two or three neighbors lives stays alive." << endl;
cout << "4.dead cell with exactly three live neighbors comes to life." << endl << endl;
cout << "To play: Press any key to begin. Enter the column and row of a cell to make \nalive, separated by a space. ";
cout << "enter 1 1 to fill grid with dead cells, enter \"-1\" to begin the \nsimulation. Then enter any number to continue or \"-1\" to move to next generation." << endl;
cin.get();

clear(now);
print(now);

do //Get initial state
{
cin >> x;
if (x == -1) break; //User is done inputting
cin >> y;
now[y-1][x-1] = 1; //Sets cell to alive
print(now); //Updates screen
}while(x != -1);
int genX=1;
do //Keep updating new generations
{
cout<<"Generation: "<<genX<<endl;
//myfile<<"Generation: "<<genX<<endl;
genX++;
clear(next);
calculate(now, next);
swap(now, next);
print(now);
cin>>cont;
}while(cont == -1);

return 0;
}

0

Решение

Каждый раз, когда вы open файл, он усекается, если вы не открыли для добавления.

Поскольку вы хотите создавать новый файл при каждом запуске, но продолжаете добавлять к нему, самый простой способ — передать открытый файл методу печати. Плюс, это не должен быть файл, вы можете распечатать на любой std::ostream,
И, как дополнительный бонус, все ваши копии для печати на cout также исчезает, и вы можете избавиться от одного выхода или другого, изменив только одну строку в main,

void print(ostream& stream, bool mat[][Row]) //Prints matrix to stream
{
stream << setw(3) << " ";
for (int p = 0; 5*p < Row; p++)
stream << setw(5) << 5*p+1;
stream << endl;
for (int m = 0; m < Col; m++)
{
stream << setw(3) << m+1;
for (int n = 0; n < Row; n++)
{
if (mat[m][n])
stream << "\xDB";
else
stream << "-";
}
myfile << endl;
}
}

Вот как это использовать в main

ofstream myfile("GameOfLife.txt");
do
{
// ... as before ...
print(cout, now);     // Print to stdout
print(myfile, now);   // print to the file
cin >> cont;
} while (cont == -1);

Чтобы любой мог понять, почему ваше последнее поколение все мертво, вам нужно будет предоставить образец первого поколения.

2

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector