c++ - 'C' 程序 : multidimensional array issue

标签 c++ c arrays multidimensional-array

请帮我解决这个问题,在过去的 11 个小时里,我已经测试并测试了它并重新阅读和重新编写,然后我放弃了。我找到了别人写的有效代码,但它仍然没有向我解释为什么他的作品和我的作品不一样,因为我遇到的问题适用于他而不适用于我的

知道了,为遇到类似问题的任何人编辑代码... 我的原始代码在这里 http://pastebin.com/h7fXHKzf 我遇到的问题是它一直卡在 if(board[x][y - 1] == '.') 检查上。

说得太早了....该程序有时会崩溃...这种情况很少见,但之前已经连续崩溃 3 次...大多数情况下,当我运行它时一切正常。

    // Chapter 8 Programming Project #9

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

    #define SIZE 10
    #define PATH_SIZE ((int) (sizeof(brd_path)))
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE] = {};
        char brd_path[25] = {'B', 'C', 'D', 'E', 'F', 'G', 'H',
                             'I', 'J', 'K', 'L', 'M', 'N', 'O',
                             'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                             'W', 'X', 'Y', 'Z'};
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        bool path_dir_chk[4] = {false};
        bool blocked = false;
        unsigned short i, j, x = 0, y = 0;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';

        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        while (blocked != true && i != PATH_SIZE) {
            for (i = 0; i < PATH_SIZE;) {
                // Reset path_dir_chk values if empty
                for (j = 0; j < 4; j++) {
                    if (board[x][y - 1] == '.')
                        path_dir_chk[0] = false;
                    if (board[x][y + 1] == '.')
                        path_dir_chk[1] = false;
                    if (board[x - 1][y] == '.')
                        path_dir_chk[2] = false;
                    if (board[x + 1][y] == '.') 
                        path_dir_chk[3] = false;
                }
                // Check the direction and replace that char
                switch (dir) {
                    case 0: if ((y - 1) >= 0 && board[x][y - 1] == '.') {
                        board[x][--y] = brd_path[i];
                        path_dir_chk[0] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 1: if ((y + 1) >= 0 && board[x][y + 1] == '.') {
                        board[x][++y] = brd_path[i];
                        path_dir_chk[1] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 2: if ((x - 1) >= 0 && board[x - 1][y] == '.') {
                        board[--x][y] = brd_path[i];
                        path_dir_chk[2] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 3: if ((x + 1) >= 0 && board[x + 1][y] == '.') {
                        board[++x][y] = brd_path[i];
                        path_dir_chk[3] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                }
            // if all path's are true exit
            if (path_dir_chk[0] == true &&
                path_dir_chk[1] == true &&
                path_dir_chk[2] == true &&
                path_dir_chk[3] == true)
                blocked = true;
            // Reset the random direction
            dir = rand() % 4;
            }
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }

好的,我已经进行了更改以反射(reflect)我目前所做的,不,它正在打印“我现在:”数字 1 - 25,然后它重新开始,但它第二次停在 12 并卡住成某种形式循环

下面是我在网上找到的工作代码,你可以比较两者,看看相似之处,但是他的代码行和我的一模一样,我的不工作......

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

    #define ROWS 10
    #define COLS 10
    int main (void)
    {
        int i, j, k, direction;
        char board[ROWS][COLS];
        const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F',
                                'G', 'H', 'I', 'J', 'K', 'L',
                                'M', 'N', 'O', 'P', 'Q', 'R',
                                'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z'};

        srand ((unsigned) time(NULL));

        for (i = 0; i < ROWS; i++)
          for (j = 0; j < COLS; j++)
            board[i][j] = '.';

        i = 0;
        j = 0;
        k = 1;
        //set array[0][0] to first element
        board[i][j] = letters[0];
        while (k < 26) {
            direction = rand() % 4;

            if (board[i][j] == '.')
                board[i][j] = letters[k++];
            if ((board[i][j + 1] != '.' || j == ROWS - 1 )&&
                (board[i + 1][j] != '.' || i == COLS -1) &&
                (board[i - 1][j] != '.' || i == 0) &&
                (board[i][j - 1] != '.' || j == 0))
                break;
            switch (direction) {
              case 0: if (j < ROWS - 1 && board[i][j + 1] == '.'){  //move right
                      j++;
                      break;     }
              case 1: if (i < COLS -1 && board[i + 1][j] == '.') {  //move down
                      i++;
                      break; }
              case 2: if (i > 0 && board[i - 1][j] == '.'){  //move up
                      i--;
                      break;  }
              case 3: if (j > 0 && board[i][j - 1] == '.') { //move left
                      j--;
                      break; }
            }
        }
        for (i = 0; i < ROWS; i++) {
          for (j = 0; j < COLS; j++)
            printf ("%4c", board[i][j]);
          printf ("\n");
        }

        return 0;
    }

最佳答案

你没有在这段代码之后将 x 和 y 设置回 0

for (x = 0; x < ROW_SIZE; x++) {
        for (y = 0; y < ROW_SIZE; y++)
            board[x][y] = '.';
    }

因此 x 和 y 将从 10 开始。而且您没有检查 x 和 y 的范围,这意味着 x 和 y 可能会偏离棋盘。

这段代码

    if ((board[x][y - 1] != '.' || y - 1 < 0) &&
        (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
        (board[x - 1][y] != '.' || x - 1 < 0) &&
        (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))

应该是这样

    if ((y - 1 <  0        || board[x][y - 1] != '.') &&
        (y + 1 >= ROW_SIZE || board[x][y + 1] != '.') &&
        (x - 1 <  0        || board[x - 1][y] != '.') &&
        (x + 1 >= ROW_SIZE || board[x + 1][y] != '.'))

有两个细微差别。

首先 y+1 和 x+1 不允许等于 ROW_SIZE,因为 ROW_SIZE 是 10,但有效的数组索引是 0 到 9。

其次,顺序很重要。计算逻辑 OR 时,首先计算左侧,如果为真,则计算右侧。这很重要,因为在某些机器上,即使在数组边界之外读取也会导致崩溃。

关于c++ - 'C' 程序 : multidimensional array issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317910/

相关文章:

c++ - Node js 服务器和 c++ 客户端套接字 io 连接,无法发出或读取数据

c++ - 带phong阴影和光线追踪的伪像

c++ - boost program_options 生成一个 Klocwork MLK.MUST

c - 可以以编程方式更新的系统全局计数器(在各种 Linux 版本上)?

javascript - 循环遍历 Javascript 中的嵌套对象

c - 为什么变量的地址在传递函数后会改变

c++ - 错误 : expected unqualified-id (C++)

c - 有一个测试卡在无限循环中,你能找到它吗?

python - 将 ByteArray 从 Python 传递给 C 函数

java - 如何在 Java 中将 String[] 数组转换为 InputStream