我正在编写Conway的生命游戏。我的任务是从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;
}
}
答案 0 :(得分:1)
/*
* 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 :(得分:0)
aftert主要缺少{ 在第一个和第二个循环中,你有两次循环
为什么需要复制?
您可以使用此函数fscanf()来读取您的文件
e.g。
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);
....
}
}