生命游戏.exe怪输出

时间:2018-02-03 22:36:35

标签: c conways-game-of-life

我为生命游戏模拟编写了一个C代码。我的程序完全适用于案例1,这是我的随机数组。但是当我尝试从.txt文件中加载数组时,它不起作用。我的意思是它给了我一个输出但它没有输出带'*'或''的数组它给了我这个:

example of output

有人知道我的错误是什么吗?

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>

#define ED 25   
#define ZD 25   

// - FUNCTIONS
void print_matr(char matr[ED][ZD], int n, int m);
void create_matr(char matr[ED][ZD], int n, int m, int *alz, int prozent);
void check_neighbours(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz);
void ausfuehren(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz);
void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz);
int Menu(char *text, int anz, char *prompt);
int check_rules(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int lx, int ly, int alz);
int Einlesen(char *prompt, int min, int max, int versuche);

// -MAIN-
int main() {
    const int n = ED; const int m = ZD;  //1.Dim/2.Dim
    char matr[ED][ZD]; 
    char lager[ED][ZD];
    int alz; //Anzahl lebender Zellen

    int prozent;
    switch (Menu("\n1 - random\n2 - load array 1\n3 - load array 2\n4 - load array 3\n\n0 - exit", 5, "\n >>>> ")) {
        case 0: exit(0); break;
        case 1:
            prozent = Einlesen("Percentage? ", 0, 100, 5);
            create_matr(matr, n, m, &alz, prozent); 
            print_matr(matr, n, m);
            ausfuehren(matr, n, m, lager, &alz);
            break;
        case 2: 
            load_matr(matr, n, m, 1,&alz);
            print_matr(matr, n, m);
            ausfuehren(matr, n, m, lager, &alz); break;
        case 3: 
            load_matr(matr, n, m, 2,&alz);
            print_matr(matr, n, m);
            ausfuehren(matr, n, m, lager, &alz); break;
        case 4: 
            load_matr(matr, n, m, 3, &alz);
            print_matr(matr, n, m);
            ausfuehren(matr, n, m, lager, &alz);
            break;
        }
     return 0;
}
// -PRINT
void print_matr(char matr[ED][ZD], int n, int m) {
    for (int lx = 0; lx < n; lx++) { 
        for (int ly = 0; ly < m; ly++) {
            printf(" %c", matr[lx][ly]);
        }
        printf("\n");
    }
    printf("\n\n");
}

int Einlesen(char *prompt, int min, int max, int versuche) {
    int zahl;
    fflush(stdin);
    if (!versuche) {
        exit(0);
    }
    printf(prompt);
    if (!scanf("%d", &zahl)) {
        printf(" *** Bitte Zahl eingeben! ***\n");
        return Einlesen(prompt, min, max, versuche - 1);
    }
    if ((zahl<min) || (zahl>max)) {
        printf(" *** [%d,%d]! ***\n", min, max);
        return Einlesen(prompt, min, max, versuche - 1);
    }
    return zahl;
}

int Menu(char *text, int anz, char *prompt) {
    printf(text);
    return Einlesen(prompt, 0, anz, 5);
}

void create_matr(char matr[ED][ZD], int n, int m, int *alz, int prozent) {  
    for (int ln = 0; ln < n; ln++) {    
        for (int lm = 0; lm < m; lm++) {   
            if (rand()%100 <= prozent) {
                matr[ln][lm] = '*'; 
                (*alz)++;
            }
            else { matr[ln][lm] = ' ';}
        }
    }
}

int check_rules(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int lx, int ly, int l_anzlz) {
    //Rule 1
    if (matr[lx][ly] == '*') {
        if ((l_anzlz == 0) || (l_anzlz == 1) || (l_anzlz > 3)) {
            lager[lx][ly] = ' ';
        }
        //Rule 2
        else if ((l_anzlz == 2) || (l_anzlz == 3)) {
                lager[lx][ly] = '*';
        }
    }
    //Rule 3
    else if (matr[lx][ly] == ' ') {
        if (l_anzlz == 3) {
            lager[lx][ly] = '*';
        }
        else lager[lx][ly] = ' ';
    }
    return 1;
}

void check_neighbours(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz) {
    int counteq = 0;
    int l_anzlz;
    (*alz)=0;
    for(int lx = 0; lx < n; lx++) { 
        for(int ly = 0; ly < m; ly++) { 
            l_anzlz = 0; 
            for(int la = -1; la <= 1; la++) { 
                for(int lb = -1; lb <= 1; lb++) { 
                    if((la==0)&&(lb==0)) continue; 
                    if((lx==0)&&(la==-1)) continue; 
                    if((lx==n-1)&&(la==1)) continue; 
                    if((ly==0)&&(lb==-1)) continue; 
                    if((ly==m-1)&&(lb==1)) continue; 

                    if (matr[lx + la][ly + lb] == '*') { 
                        l_anzlz++;
                    }
                }
            }
            check_rules(matr, n, m, lager, lx, ly, l_anzlz);
        }
    }
    //printf("lager\n");
    //print_matr(lager, n, m);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++) {
            if (matr[i][j] == lager[i][j]) {
                counteq++;
            }
            matr[i][j] = lager[i][j];
            if (matr[i][j] == '*') (*alz)++;
        }
    }
    if (counteq == (n*m)) (*alz)=0;
}

// -EXECUTE
void ausfuehren(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz) {
    char eing;
    int fors; 
    fors = Einlesen(" [0] or [1]?\n", 0, 1, 2);
    if(fors==1) printf(" 'q' to end \n");

    while ((*alz) > 0) {
        check_neighbours(matr, n, m, lager, alz); /
        print_matr(matr, n, m);
        if (fors == 0) {
            Sleep(300); 
        }
        else if (fors == 1) {
            fflush(stdin);
            scanf("%c", &eing);
            if(eing=='q') break;
        }
    }
}


void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz) {
    FILE *fp;
    char c[4];
    (*alz)=0;

    if (filenr == 1) {
        fp = fopen("matrix1.txt", "rt"); 
    }
    else if (filenr == 2) {
        fp = fopen("matrix2.txt", "rt");
    }
    else if (filenr == 3) {
        fp = fopen("matrix3.txt", "rt");
    }

    if(fp==0){
        printf("Couldnt open File!");
        exit(-1);
    }

    if(fgets(c, 5, fp)==NULL){
        printf("Error\n");
        exit(-1);
    }
    n=(int)(*c);
    if(fgets(c, 5, fp)==NULL){
        printf("Error\n");
        exit(-1);
    }
    m=(int)(*c);

    for (int lx = 0; lx < n; lx++) {
        for (int ly = 0; ly < m; ly++) {
            int star = fgetc(fp);
            if (star == 42) { //42=* | ASCII
                matr[lx][ly] = '*';
                (*alz)++;
            }
            else if (star == 32) { //32=  | ASCII
                matr[lx][ly] = ' ';
            }
            else if (star == 10) { //10=\n | ASCII
                lx--; ly = m - 1;

            }
            else break; //EOF
        }

    }
    fclose(fp);
}

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是进入load_matr函数:

这是一个可能的解决方案,它使输入文件格式正确(它不是解决方案容错),以及许多不好的事情,但它可以正常工作。

void clear_matr(char matr[ED][ZD])
{
    for (int row = 0; row < ED; row++)
        for (int col = 0; col < ZD; col++)
            matr[row][col] = ' ';
}


void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz) {
    FILE *fp;
    char c[16];
    (*alz)=0;
    memset(c, 0, sizeof(c));

    if (filenr == 1) {
        fp = fopen("matrix1.txt", "rt"); 
    }
    else if (filenr == 2) {
        fp = fopen("matrix2.txt", "rt");
    }
    else if (filenr == 3) {
        fp = fopen("matrix3.txt", "rt");
    }

    if (fp == 0) {
        printf("Couldnt open File!");
        exit(-1);
    }

    if (fgets(c, 5, fp) == NULL) {
        printf("Error\n");
        exit(-1);
    }
    n = atoi(c);

    if (fgets(c, 5, fp) == NULL) {
        printf("Error\n");
        exit(-1);
    }
    m = atoi(c);

    clear_matr(matr);

    for (int ly = 0; ly < m; ly++) {
        for (int lx = 0; lx < n; lx++) {
            int star = fgetc(fp);
            if (star == '\n')
                star = fgetc(fp);
            if (star == 42) { //42=* | ASCII
                matr[ly][lx] = '*';
                (*alz)++;
            }
            else if (star == 32) { //32=  | ASCII
                matr[ly][lx] = ' ';
            }
            else break; //EOF
        }

    }
    fclose(fp);
}