【问题标题】:Game of Life Problems in C [closed]C中的生活问题游戏[关闭]
【发布时间】:2013-09-14 00:05:11
【问题描述】:

我正在创建我创建了一个工作副本的生命游戏程序,但我在制作它时遇到问题,以便用户可以在我尝试更改我的代码时输入网格的 x 和 y(行和列)它得到混乱并导致很多错误。我也尝试使用malloc()free() 开始使用堆,但我没有运气。下面的代码只是一个工作硬编码的解决方案。 (我还注释掉了测试数据和用户输入部分)。提前感谢您提供的任何帮助。

#include <stdio.h>
#define HEIGHT 12
#define WIDTH 12
#define LIFE_YES 'X'
#define LIFE_NO 'O'

typedef int TableType[HEIGHT][WIDTH];

void printTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    if (table[height][width] == LIFE_YES) {
                        printf("X");
                    } 
            else {
                        printf("-");
                    }
            }
            printf("\n");
        }
        printf("\n");
}

void clearTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    table[height][width] = LIFE_NO;
            }
        }
}

void askUser(TableType tableA) {
    int i;
        int n;
        int height, width;

        printf("Enter the amount of initial organisms: ");
        scanf("%d", &n);

        for (i = 0; i < n; i++) {
            printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
            scanf("%d %d", &height, &width);

            tableA[height][width] = LIFE_YES;
        }

        printTable(tableA);
        printf("Generation 0");
}

int getNeighborValue(TableType table, int row, int col) {
        if (row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH || table[row][col] != LIFE_YES ) {
            return 0;
        } 
    else {
            return 1;
        }
}

int getNeighborCount(TableType table, int row, int col) {
        int neighbor = 0;

        neighbor += getNeighborValue(table, row - 1, col - 1);
        neighbor += getNeighborValue(table, row - 1, col);
        neighbor += getNeighborValue(table, row - 1, col + 1);
        neighbor += getNeighborValue(table, row, col - 1);
        neighbor += getNeighborValue(table, row, col + 1);
        neighbor += getNeighborValue(table, row + 1, col - 1);
        neighbor += getNeighborValue(table, row + 1, col);
        neighbor += getNeighborValue(table, row + 1, col + 1);

        return neighbor;
}

void calculate(TableType tableA) {
        TableType tableB;
        int neighbor, height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    neighbor = getNeighborCount(tableA, height, width);
                    if (neighbor==3) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else {
                        tableB[height][width] = LIFE_NO;
                    }
            }
        }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    tableA[height][width] = tableB[height][width];
            }
    }
}

/* test data
void loadTestData(TableType table) {
        table[3][4] = LIFE_YES;
        table[3][5] = LIFE_YES;
        table[3][6] = LIFE_YES;

        table[10][4] = LIFE_YES;
        table[10][5] = LIFE_YES;
        table[10][6] = LIFE_YES;
        table[11][6] = LIFE_YES;
        table[12][5] = LIFE_YES;
}
*/

int main(void) {
        TableType table;
        char end;
        int generation = 0;

        clearTable(table);
        /*askUser(table);*/
        /*loadTestData(table);*/
        printTable(table);

        while (end != 'q') {
            calculate(table);
            printTable(table);
            printf("Generation %d\n", ++generation);
            printf("Press q to quit or 1 to continue: ");
            scanf(" %c", &end);
        }
        return 0;
}

第一个测试数据实例的输出:[3][4]、[3][5]、[3][6] 上的 X。

------------
------------
------------
----XXX-----
------------
------------
------------
------------
------------
------------
------------
------------

更新:

#include <stdio.h>
#define LIFE_YES 'X'
#define LIFE_NO 'O'

extern int HEIGHT, WIDTH;
typedef int **TableType;

void printTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    if (table[height][width] == LIFE_YES) {
                        printf("X");
                    } 
            else {
                        printf("-");
                    }
            }
            printf("\n");
        }
        printf("\n");
}

void clearTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    table[height][width] = LIFE_NO;
            }
        }
}

void askUser(TableType tableA) {
    int i;
        int n;
        int height, width;

        printf("Enter the amount of initial organisms: ");
        scanf("%d", &n);

        for (i = 0; i < n; i++) {
            printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
            scanf("%d %d", &height, &width);

            tableA[height][width] = LIFE_YES;
        }

        printTable(tableA);
        printf("Generation 0");
}

int getNeighborValue(TableType table, int row, int col) {
        if (row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH || table[row][col] != LIFE_YES ) {
            return 0;
        } 
    else {
            return 1;
        }
}

int getNeighborCount(TableType table, int row, int col) {
        int neighbor = 0;

        neighbor += getNeighborValue(table, row - 1, col - 1);
        neighbor += getNeighborValue(table, row - 1, col);
        neighbor += getNeighborValue(table, row - 1, col + 1);
        neighbor += getNeighborValue(table, row, col - 1);
        neighbor += getNeighborValue(table, row, col + 1);
        neighbor += getNeighborValue(table, row + 1, col - 1);
        neighbor += getNeighborValue(table, row + 1, col);
        neighbor += getNeighborValue(table, row + 1, col + 1);

        return neighbor;
}

void calculate(TableType tableA) {
        TableType tableB;
        int neighbor, height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    neighbor = getNeighborCount(tableA, height, width);
                    if (neighbor==3) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else {
                        tableB[height][width] = LIFE_NO;
                    }
            }
        }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    tableA[height][width] = tableB[height][width];
            }
    }
}

/* test data
void loadTestData(TableType table) {
        table[3][4] = LIFE_YES;
        table[3][5] = LIFE_YES;
        table[3][6] = LIFE_YES;

        table[10][4] = LIFE_YES;
        table[10][5] = LIFE_YES;
        table[10][6] = LIFE_YES;
        table[11][6] = LIFE_YES;
        table[12][5] = LIFE_YES;
}
*/

int main(void) {
        char end;
        int generation = 0;

        printf("Enter the amount of rows and columns you want in the grid: ");
        scanf("%i%i\n" &HEIGHT, &WIDTH);

        TableType table = malloc(HEIGHT * sizeof(int*));
        for (int i = 0; i < HEIGHT; i++) {
            table[i] = malloc(WIDTH * sizeof(int));
    }

        clearTable(table);
        /*askUser(table);*/
        /*loadTestData(table);*/
        printTable(table);

        while (end != 'q') {
            calculate(table);
            printTable(table);
            printf("Generation %d\n", ++generation);
            printf("Press q to quit or 1 to continue: ");
            scanf(" %c", &end);
        }
        return 0;
}

计算()

void calculate(TableType tableA) {
        TableType tableB;
        int neighbor, height, width, i;
    **tableB= malloc(HEIGHT * sizeof(int*));

    for (i = 0; i < HEIGHT; i++) {
            tableB[i] = malloc(WIDTH * sizeof(int));
    }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    neighbor = getNeighborCount(tableA, height, width);
                    if (neighbor==3) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else {
                        tableB[height][width] = LIFE_NO;
                    }
            }
        }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    tableA[height][width] = tableB[height][width];
            }
    }
    free(tableB);
}

【问题讨论】:

  • "它变得混乱并导致很多错误。"
  • 如果可以解释预期输出是什么以及您的输出有多接近,将会很有帮助。顺便说一句,感谢您提供 hte 输入数据 (loadTestData)!
  • 我在尝试编辑以获取用户对网格大小和他们希望看到多少代的规范时完美地工作。只是因为我对所有内容都进行了硬编码,我在尝试对其进行非硬编码时遇到了问题。

标签: c pointers multidimensional-array heap-memory conways-game-of-life


【解决方案1】:

将您的类型定义更改为:

typedef int **TableType;

并将 HEIGHTWIDTH 从宏更改为全局变量(或传递给接收表作为参数的每个函数的局部变量)。

从用户那里得到HEIGHTWIDTH后,你可以用:

table = malloc(HEIGHT * sizeof(int*));
for (int i = 0; i < HEIGHT; i++) {
    table[i] = malloc(WIDTH * sizeof(int));
}

【讨论】:

  • 这是否是我需要做的唯一更改才能使用用户输入获取网格? table = 的部分也将进入我的主要方法。
  • 是的,表分配进入main()。我认为这应该是您需要更改的全部内容。
  • 我更新了我的代码,现在看起来还好吗?
  • 您不应该覆盖问题中的原始代码,如果他们没有看到您写的内容,他们应该如何理解我的答案?请放回原始代码,并将您的重写添加为底部的更新。无论如何,您的新版本是否按预期工作?
  • 我在您创建的 for 循环中收到一个在 c99 模式之外使用的 for 循环初始声明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
相关资源
最近更新 更多