Connect-N 棋盘游戏,当宽度为 >> 高度时崩溃

标签 c cygwin

我正在编写 Connect-N 棋盘游戏,即将完成并已完成故障排除。我现在的问题是,在更改了一些内容后,如果宽度比高度大太多,当计算机进行移动时,我的游戏就会崩溃。这里涉及到两个函数,所以我把它们都贴出来。

Board
*AllocateBoard(int columns, int rows)
        {
        int **array= malloc(sizeof(int *) *columns);
        int r = 0;
        for ( r = 0; r < columns; ++r)
                {
                array[r] = malloc(sizeof(int) * rows);
                }
        int j = columns - 1;
        int k = rows - 1;
        int m = 0;
        int n = 0;
        for ( m = 0; m < j; ++m)
                {
                for ( n = 0; n < k; ++n)
                        {
                        array[m][n] = 0;
                        }
                }
        Board *board = malloc(sizeof(Board));
        board->columns = columns;
        board->rows = rows;
        board->spaces = array;
        return board;
        }

第一个函数将板分配为用户通过命令行传入的宽度 * 高度矩阵。然后,它将板上的每个空间初始化为零,然后将列、行和空间存储到我创建的 Board 结构中。然后它返回板。

int
computerMakeMove(Board *board)
{       int RandIndex = 0;
        int **spaces = board->spaces;
        int columns = board->columns;
        int *arrayoflegalmoves = malloc(sizeof(int) * (columns));
        int columncheck = 0;
        int legalmoveindex = 0;
        while (columncheck <= columns - 1)
        {
                if (spaces[columncheck][0] == 0)
                        {
                        arrayoflegalmoves[legalmoveindex] = columncheck;
                        ++legalmoveindex;
                        ++columncheck;
                        }
                else
                        {
                        ++columncheck;
                        }
                arrayoflegalmoves = realloc(arrayoflegalmoves, (legalmoveindex) * sizeof(int));
        }
        if (legalmoveindex == 1)
        {
                return arrayoflegalmoves[0];
        }
        else
        {
                RandIndex = rand() % (legalmoveindex);
                return arrayoflegalmoves[RandIndex];
        }
}

第二个函数的目的是让计算机随机选择棋盘上的一列。它通过检查每列顶行的值来实现这一点。如果那里有零,它将将该值存储在合法移动数组中,然后递增合法移动索引。如果不存在,它会跳过该列并检查下一列。当它完成检查最后一列时结束。如果只有一个合法的 Action ,它就会执行它。如果有更多,它将从合法移动数组中选择一个随机索引(我在主程序中运行 srand),然后返回该值。它只会尝试在合法的棋盘上玩,所以这不是问题。但是,我非常有信心该函数中会出现问题,因为我按如下方式调用函数

  printf("Taking the computers move.\n");
        {printf("Taking computer's move.");
         computermove = computerMakeMove(playerboard);
         printf("Computer's move successfully taken.\n");
         playerboard = MakeMove(playerboard, computermove, player);
         printf("Computer's board piece successfully played.\n");
         system("clear");
         displayBoard(playerboard);
         ...;
        }

并打印

Aborted (core dumped)

打印后立即

"Taking computer's move."

我的问题又是:为什么电脑播放时宽度大于高度,我的程序就会崩溃?

谢谢。

编辑:我找到了解决方案,但我很愚蠢。

我在 while 循环期间重新分配。 realloc 应该是 while 循环之外的第一件事。

最佳答案

对于 future 可能遇到此问题的程序员的答案:

注意

while (columncheck <= columns - 1)
        {
                if (spaces[columncheck][0] == 0)
                        {
                        arrayoflegalmoves[legalmoveindex] = columncheck;
                        ++legalmoveindex;
                        ++columncheck;
                        }
                else
                        {
                        ++columncheck;
                        }
                arrayoflegalmoves = realloc(arrayoflegalmoves, (legalmoveindex) * sizeof(int));
        }

里面有一个realloc。 realloc 应该立即移动到它的外部,就像这样

while (columncheck <= columns - 1)
        {
                if (spaces[columncheck][0] == 0)
                        {
                        arrayoflegalmoves[legalmoveindex] = columncheck;
                        ++legalmoveindex;
                        ++columncheck;
                        }
                else
                        {
                        ++columncheck;
                        }
        }
        arrayoflegalmoves = realloc(arrayoflegalmoves, (legalmoveindex) * sizeof(int));

关于Connect-N 棋盘游戏,当宽度为 >> 高度时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282277/

相关文章:

c - 是否可以在链接期间触发#warning?

Apache Nutch 错误 : Injector: java. io.IOException:命令字符串中的(空)条目:空 chmod 0644

C++:如何在头文件中包含 Paillier.h

c - 从函数内部获取函数的地址

c - 我在使用函数将字母分配给结构的 char 变量时遇到问题

java - 设置csipsimple项目

java - 如何通过java shell调用cygwin?

android - CYGWIN 中的 repo 出现类似 "Python can not open file ' user/bin/repo' 的错误

c++ - eclipse编译错误

c - Integrity 上是否有适用于 OpenVMS 的 Oracle C 客户端