Чтение из файла file.txt в массив строк и использование его для игры Конвея в «Жизнь»

Я кодирую игру Конвея «Жизнь из жизни». Моя задача — прочитать файл file.txt в массив строк. А затем использовать этот массив в качестве входного массива игры. Я написал код об этом. Но это полно ошибок. Я не знаю, как правильно это сделать.

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;
namespace gamearray {
int main()ifstream file_("file.txt");
if(file.is_open())
{
string myArray[10];

for(int i = 0; i < 10; ++i)
{
for(int i = 0; i < 10; i++)

file >> array1[j][i];
}
}
return 0;
}

void copy(int array1[10][10], int array2[10][10])
{
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
array2[j][i] = array1[j][i];
}
}void life(int array[10][10], char choice)
{

int temp[10][10];
copy(array, temp);
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(choice == 'm')
{

int count = 0;
count = array[j-1][i] +
array[j-1][i-1] +
array[j][i-1] +
array[j+1][i-1] +
array[j+1][i] +
array[j+1][i+1] +
array[j][i+1] +
array[j-1][i+1];

if(count < 2 || count > 3)
temp[j][i] = 0;

if(count == 2)
temp[j][i] = array[j][i];

if(count == 3)
temp[j][i] = 1;
}}
}

copy(temp, array);
}bool compare(int array1[10][10], int array2[10][10])
{
int count = 0;
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(array1[j][i]==array2[j][i])
count++;
}
}

if(count == 10*10)
return true;
else
return false;
}void print(int array[10][10])
{
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(array[j][i] == 1)
cout << '*';
else
cout << ' ';
}
cout << endl;
}
}int main()
{gamearray::main();

int gen0[10][10];
int todo[10][10];
int backup[10][10];
char neighborhood;
char again;
char cont;
bool comparison;{do
{

do
{
cout << "Which neighborhood would you like to use (m): ";
cin >> neighborhood;
}while(neighborhood != 'm');

system("CLS");
int i = 0;

do
{

srand(time(NULL));

for(int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
gen0[j][i] = rand() % 2;
}if(i == 0)
copy(gen0, todo);
copy(todo, backup);
print(todo);
life(todo, neighborhood);
i++;system("sleep .5");

if(i % 10 == 1 && i != 1)
{
cout << endl;

do
{
cout << "Would you like to continue this simulation? (y/n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}

comparison = compare(todo, backup);
if(comparison == false)
system("CLS");
if(comparison == true)
cout << endl;
}while(comparison == false);
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> again;
}while(again != 'y' && again != 'n');
}while(again == 'y');

return 0;
}
}

-2

Решение

после пропущенного основного
в первом и втором цикле у меня есть цикл дважды

зачем вам копировать?

Вы можете использовать эту функцию fscanf () для чтения вашего файла

например

File= fopen(FileName, "r");
if( File == NULL ){
printf("Impossible to open  file %s ! \n",FileName);
}

if( File != NULL)
{
char text[255];
while(!feof(File))
{
fscanf(File,"%s\n",&text);
....
}
}
0

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

/*
* spielm.cpp
*
*  Created on: 22.09.2016
*      Author: fislam
*/#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;void GetFile();
bool MakeArray();
char ChgArray();
//char GameBoard();

const int ROW1 =10;
const int COL1 =10;
const int BOARD_ROWS(10);
const int BOARD_COLS(10);
ifstream myfile;
string filename;
char live = 'X';
char dead = '.';

char board [BOARD_ROWS][BOARD_COLS];

int main()
{
int q;

//GetFile();
if ( MakeArray() ){

for ( int i(0); i <10; i++)
ChgArray();
}
else {
cout << "Error parsing input file" << endl;
}

cin >> q;
return 0;
}void GetFile()
{
cout<<"Enter the filename: \n";
cin>>filename;
return;
}

bool MakeArray()
{
bool ret(false);
char val;
int  totCnt = BOARD_ROWS*BOARD_COLS;
myfile.open (/*filename.c_str()*/"c_str.txt");
if ( myfile.is_open() ) {
for (int r=0; r<ROW1; r++)
{
for (int c=0; c<COL1; c++)
{
myfile>>val;
if ( val == dead || val == live ) {
board[r-1][c-1] = val;
totCnt--;
}
}
}
if ( !totCnt ) {
ret = true;
}
myfile.close();
}
return ret;
}
char getNextState(char b[BOARD_ROWS][BOARD_COLS], int r, int c)
{
char ret;

return ret;
}
char ChgArray()
{
char boardTmp[BOARD_ROWS][BOARD_COLS];
for (int r=0; r<BOARD_ROWS; r++)
{
for (int c=0; c<BOARD_COLS; c++)
{

boardTmp[r][c] = getNextState(board,r,c);

cout << boardTmp[r][c];
}
cout<<endl;
}

memcpy(board,boardTmp,sizeof(board));
cout << endl;

}

Вместо предыдущего я написал новый. и это сработало

1

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