2016-09-21 3 views
-2

Ich kodiere ein Conway Spiel des Lebens. Meine Aufgabe liest von file.txt zu Array von Zeichenfolgen. Und dann dieses Array als die Eingabefeld des Spiels. Ich habe einen Code darüber geschrieben. Aber es ist voller Fehler. Ich weiß nicht, wie man das richtig macht.Lesen von file.txt zu Array von Zeichenfolgen und damit Conways Spiel des Lebens zu spielen

#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

Können Sie bitte die Fehlermeldungen zur Verfügung stellen? – Rakete1111

+1

TDD (TestDrivenDevelopment) sollte hier dein Freund sein. Zerlege Probleme/Codes in Funktionen und teste sie unabhängig voneinander. Sie finden Fehler und beheben Ihren Code. Es könnte viel schneller Weg sein, als debbuger oder verhungern bei Code :) –

+0

Warum setzen Sie 'main' in Namespace gamearray? Warum polen Sie den globalen Namespace mit 'using namespace std'? Warum gibt es keine öffnende Klammer '{' nach 'main()'? (Ist das überhaupt kompilierbar?), Möchten Sie vielleicht etwas über 'else if', 'else' nach einem 'if' erfahren, das immer wiederkehrt, ist ziemlich überflüssig, Sie könnten an Ihrer Formatierung arbeiten (empfehlen [clang-format] (http: //clang.llvm.org/docs/ClangFormat.html)), benutze 'system' nicht; bitte, es gibt noch viel mehr zu sagen - ich denke, der beste Rat ist: Lesen Sie ein * gutes * C++ Buch (oder zwei). –

Antwort

0

aftert Haupt Sie vermissen { in ersten und zweiten Schleife haben Sie i-Schleife zweimal

warum brauchen Sie zu kopieren?

können Sie diese Funktion fscanf() benutzen, um Ihre Datei

zum Beispiel zu lesen

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

Dies ist die Aufgabe, die mir gegeben wurde. Mein Code muss eine text.file als Array lesen und dieses Array sollte als Eingabe für den Code verwendet werden. –

+0

Sie deklarieren String mArray [10], aber Sie verwenden Array [] [] warum? – Angen

+0

14 erwartete Initialisierer vor 'ifstream' 15 erwartete unqualifizierte ID vorher, wenn dies die Fehler sind –

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; 

    } 

Statt der vorherigen I schrieb neu. und es hat funktioniert

Verwandte Themen