即使紧接在重写元素之后,二维数组也不会更新

时间:2018-10-07 06:19:25

标签: c arrays multidimensional-array

上下文:

您好,我正在尝试打印一个7x6的四连板,其中每个部分为|___|,并带有三个下划线。我试图创建每个中心下划线2D数组的元素,以便稍后可以为播放器移动而更新它。我目前正在尝试测试更新中心下划线或数组元素。

冲突:

成功的测试将把选定的部分输出为|_X_|。我尝试测试使用PrintBoard()更新arr[1][1] = 'X';内的元素,但是该元素仍然为_,该节也仍然为|___|。然后,我在main()中重试了一次,但board[1][1] = 'X';没有用。我也没有错误或警告。

代码:

#include <stdio.h>

void PrintBoard(char arr[6][7]);

int main()
{

    // Declaration of 7x6 2D board array: board[row][col]
    char board[6][7];

    // Sets all board array elements to '_'
    PrintBoard(board);

    /* board[1][1] =  'X'; // NOT WORKING, ELEMENTS REMAIN AS `_` */

    return 0;
}

void PrintBoard(char arr[6][7])
{
    int vertCnt = 0; // Counts vertical lines (8 per row, separates sections)
    int undCnt = 0; // Counts underscores (3 per section)
    int rowCnt = 0; // Counts rows (6 total)
    int colCnt = 0; // Count columns (7 total)

    // Print game title
    printf("      ~~ CONNECT FOUR ~~\n\n");

    for (int rowCnt = 0; rowCnt <= 6; rowCnt++)
        {
            // If current row is not the first, start it on a new line
            if (rowCnt > 0)
            {
                printf("\n");
            }

            // Creation of row: |___|___|___|___|___|___|___|
            for (int vertCnt = 0; vertCnt < 8; vertCnt++)
            {
                printf("|");

                // Only print `_` three times as long as there have been 7 total or less vertical lines printed
                for (int undCnt = 0; undCnt < 3 && vertCnt <= 6; undCnt++)
                {

                    // Print left and right sections as `_`
                    if(undCnt != 1)
                    {
                        printf("_");
                    }

                    // Assign middle section to board array and prints it as `_`
                    else if(undCnt == 1)
                    {
                        // If printing left underscore, increment column count
                        if(colCnt < 7){colCnt++;}

                        // Assign middle section to 2D board array
                        arr[rowCnt][colCnt] = '_';
                        printf("%c", arr[rowCnt][colCnt]);

                        // Test to rewrite random array element
                        arr[1][1] = 'X'; // NOT WORKING. ELEMENTS REMAIN AS `_`

                        // After last (7th) column reached, reset to 0
                        if(colCnt == 7){colCnt = 0;}
                    }

                }
            }
        }

    // Print column numbers
    printf("\n  1   2   3   4   5   6   7\n\n");

    /* HOW A CLEAN BOARD SHOULD LOOK:

         ~~ CONNECT FOUR ~~             <--- GAME TITLE

    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|       <--- BOARD
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
      1   2   3   4   5   6   7         <--- COLUMN NUMBERS

    */

}

1 个答案:

答案 0 :(得分:4)

  

char[7]的有效数组索引范围为0<= 6

您无法访问数组char arr[6][7]

for (int rowCnt = 0; rowCnt <= 6; rowCnt++)

最多计数6,在该情况下,它仅应计入5和构造

if (colCnt < 7) { colCnt++; }

arr[rowCnt][colCnt] = '_';  // eventually arr[6][something] gets
printf("%c", arr[rowCnt][colCnt]);  // written to --> corupted stack.

if (colCnt == 7) { colCnt = 0; }

有效地将colCnt1转换为7,而不是将0转换为6


您完全使事情复杂化了...

#include <stddef.h>
#include <stdio.h>

enum { board_width = 7, board_height = 6 };

void board_print(char arr[board_height][board_width]);

int main(void)
{
    char board[board_height][board_width] = { 0 };

    board_print(board);
    board[1][1] = 'X';
    board_print(board);
}

void board_print(char arr[board_height][board_width])
{
    puts("      ~~ CONNECT FOUR ~~\n");

    for (size_t row = 0; row < board_height; ++row) {
        for (size_t col = 0; col < board_width; ++col) {
            printf("|_%c_", arr[row][col] ? arr[row][col] : '_');
        }
        puts("|");
    }

    puts("  1   2   3   4   5   6   7\n");
}

输出:

      ~~ CONNECT FOUR ~~

|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
  1   2   3   4   5   6   7

      ~~ CONNECT FOUR ~~

|___|___|___|___|___|___|___|
|___|_X_|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
  1   2   3   4   5   6   7