2016-09-21 16 views
-2

我正在編寫一個康威的生命遊戲。我的任務是從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

你能提供錯誤信息嗎? – Rakete1111

+1

TDD(TestDrivenDevelopment)應該是你的朋友。將問題/代碼分解爲函數並獨立測試它們。你會發現錯誤並修復你的代碼。這可能比附加debbuger或在代碼上捱餓更快:) –

+0

爲什麼你把'main'放在命名空間gamearray裏面?爲什麼要用'using namespace std'來監控全局名稱空間?爲什麼在'main()'之後沒有開頭括號'{''? (這是否甚至可以編譯?),你可能想了解'else if','else'後總是返回一個'if'是相當多餘的,你可以使用你的格式(推薦[clang-format] //clang.llvm.org/docs/ClangFormat.html)),不要使用'system';請有更多的指出 - 我想最好的建議是:閱讀一本*好的* C++書(或兩本)。 –

回答

0

aftert主要在第一和第二環缺少{ 你有我兩次循環

爲什麼你需要複製?

您可以使用此功能的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

這是給我的任務。我的代碼必須讀取一個text.file作爲一個數組,這個數組應該被用作代碼的輸入。 –

+0

你聲明字符串mArray [10]但你使用array [] []爲什麼? – Angen

+0

14'ifstream'之前的期望初始值設定項15如果這些是錯誤值,那麼15期望unqualified-id –

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; 

    } 

我寫了新的。它的工作