C++ 从file.txt读取字符串数组并使用它播放Conway';人生的游戏

C++ 从file.txt读取字符串数组并使用它播放Conway';人生的游戏,c++,conways-game-of-life,C++,Conways Game Of Life,我正在编写一个康威的生活游戏。我的任务是从file.txt读取字符串数组。然后使用这个数组作为游戏的输入数组。我已经写了一段代码。但它充满了错误。我不知道做这件事的正确方法是什么 #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <fstream> using

我正在编写一个康威的生活游戏。我的任务是从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;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
命名空间游戏阵列{
int main()
ifstream文件(“file.txt”);
if(file.is_open())
{
字符串myArray[10];
对于(int i=0;i<10;++i)
{
对于(int i=0;i<10;i++)
文件>>阵列1[j][i];
}
}
返回0;
}
无效副本(int array1[10][10],int array2[10][10])
{
对于(int j=0;j<10;j++)
{
对于(int i=0;i<10;i++)
阵列2[j][i]=阵列1[j][i];
}
}
无效寿命(整数数组[10][10],字符选择)
{
内部温度[10][10];
拷贝(数组,临时);
对于(int j=0;j<10;j++)
{
对于(int i=0;i<10;i++)
{
如果(选项='m')
{
整数计数=0;
计数=数组[j-1][i]+
阵列[j-1][i-1]+
数组[j][i-1]+
阵列[j+1][i-1]+
数组[j+1][i]+
数组[j+1][i+1]+
数组[j][i+1]+
阵列[j-1][i+1];
如果(计数<2 | |计数>3)
温度[j][i]=0;
如果(计数=2)
temp[j][i]=数组[j][i];
如果(计数=3)
温度[j][i]=1;
}
}
}
副本(临时、阵列);
}
布尔比较(int-array1[10][10],int-array2[10][10])
{
整数计数=0;
对于(int j=0;j<10;j++)
{
对于(int i=0;i<10;i++)
{
if(array1[j][i]==array2[j][i])
计数++;
}
}
如果(计数=10*10)
返回true;
其他的
返回false;
}
无效打印(整数数组[10][10])
{
对于(int j=0;j<10;j++)
{
对于(int i=0;i<10;i++)
{
if(数组[j][i]==1)

你错过的主要原因是什么{ 在第一个循环和第二个循环中,我循环了两次

你为什么要复印

可以使用此函数fscanf()读取文件

e、 g

/*
*斯皮尔姆
*
*创建日期:2016年9月22日
*作者:菲斯兰
*/
#包括
#包括
#包括
#包括
使用名称空间std;
void GetFile();
bool-MakeArray();
char ChgArray();
//char GameBoard();
常数int ROW1=10;
常数int COL1=10;
连续线路板(10排);
康斯特国际董事会(10);
ifstreammyfile;
字符串文件名;
char live='X';
char dead=';
炭板[板排][板列];
int main()
{
int-q;
//GetFile();
if(MakeArray()){

对于(int i(0);我可以请您提供错误消息吗?TDD(TestDrivenDevelopment)应该是您在这里的朋友。将问题/代码分解为函数并独立测试它们。您将发现错误并修复代码。这可能比附加debbuger或在代码处饥饿要快得多:)为什么要将
main
放在名称空间gamearray中?为什么要使用名称空间std
使用
来处理全局名称空间?为什么在
main()
之后没有大括号
{
?(这甚至可以编译吗?)如果你想学习< <代码>,如果< <代码> > <代码>其他>代码> >代码> >“/CODE”,它总是返回,你可以在你的格式(推荐)上工作,不要使用<代码>系统< /> >,请指出更多的——我猜最好的建议是:读一本好的C++书(或两本)。。这是交给我的任务。我的代码必须将text.file读取为数组,并且此数组应用作代码的输入。您声明了字符串mArray[10],但使用了数组[][]为什么?14在“ifstream”之前应使用初始值设定项15在之前应使用非限定id如果这些是错误
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);
      ....
   }
}
/*
 * 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;

    }