'C'程序:多维数组问题



请帮我弄清楚这一点,我已经测试了它,在过去的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: %dn", ++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: %dn", ++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: %dn", ++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: %dn", ++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;
    }

好的,我已经做了一些改变来反映我到目前为止的情况,不,它正在打印"我现在是:"数字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时,首先计算左侧,如果为true,则不计算右侧。这一点很重要,因为在某些机器上,即使在数组边界之外进行读取也会导致崩溃。

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

初始化后,您没有重置x和y的值。当x的值=ROW_SIZE时,您正在尝试访问板[x+1][y]和板[x][y+1]。

x = 0;
y = 0;

感谢用户3386109,您的输入和其他人的帮助一样宝贵。我很感激,工作代码如下:(

    // Chapter 8 Programming Project #9
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 10
    #define PATH_SIZE 25
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
    int main(void)
    {
        char board[SIZE][SIZE];
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        unsigned short i = 0, x, y;
        // 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
        for (i = 0; i < PATH_SIZE;) {
                // Check that the last character has not been cornered
            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))
                break;
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0
                            && board[x][y - 1] == '.') {
                            board[x][--y] = i + 'B';
                            ++i;
                        } break;
                case 1: if ((y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                            board[x][++y] = i + 'B';
                            ++i;
                        } break;
                case 2: if ((x - 1) >= 0
                            && board[x - 1][y] == '.') {
                            board[--x][y] = i + 'B';
                            ++i;
                        } break;
                case 3: if ((x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                            board[++x][y] = i + 'B';
                            ++i;
                        } break;
                default: if (board[x][y] == '.')
                             board[x][y] = i + 'B';
                             break;
            }
        // Reset the random directions
        dir = rand() % 4;
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%4c ", board[x][y]);
            printf("n");
        }
        return 0;
    }

相关内容

  • 没有找到相关文章

最新更新