C:尽管忽略了初始化和增量表达式,For循环仍在执行,因此始终为真

时间:2018-10-06 23:32:17

标签: c for-loop multidimensional-array

上下文:

您好,我正在尝试打印一个7x6的四连板,其中每个部分为|___|,并带有三个下划线。我想在每个中心下划线添加一个2D数组的元素,以便以后进行更新。

冲突:

我没有收到任何错误或警告,但我的输出仅为|________ ...,并带有无限数量的下划线。我已经成功地重写了打印三个下划线而不将中心分配给数组的代码(但是显然,由于我无法更新中心下划线,因此该代码对于制作实际游戏毫无用处)。我当前代码中的所有循环声明均已用于该成功的变体中,因此,我很确定那些不是我的问题。如果您认为有帮助,也可以提供该代码。我所知道的是colCnt(列数)永远增加,而undCnt(下划线数)则停留在2。因此,我怀疑这个for循环是我代码中的主要问题,但是我不知道在哪里:

                // 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("_");

                        // If printing left section, increment column count
                        if(undCnt = 1){colCnt++;}
                    }

                    // Assign middle section to board array and prints it as `_`
                    else if(undCnt == 1)
                    {
                        arr[rowCnt][colCnt] = '_';
                        printf("%c", arr[rowCnt][colCnt]);

                    }

                }

代码:

#include <stdio.h>

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

int main()
{

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

    PrintBoard(board);

    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("_");

                        // If printing left section, increment column count
                        if(undCnt = 1){colCnt++;}
                    }

                    // Assign middle section to board array and prints it as `_`
                    else if(undCnt == 1)
                    {
                        arr[rowCnt][colCnt] = '_';
                        printf("%c", arr[rowCnt][colCnt]);

                    }

                }
            }
        }

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


    /* HOW THE BOARD SHOULD LOOK:

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

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

    */

}

如果您有任何疑问,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:0)

您是否考虑过尝试打印“ ___ |”一次全部?然后,您要做的就是检查列数,如果列数为0,则打印'|'如果它是最大列值,请添加换行符。

for(int row_count = 0; row_count<max_rows; rows++)
{
    for ( int column_count = 0; column_count<max_columns; columns++)
    {
         if( column_count==0)
         {
             printf('|');
         }
         printf("___|");        
    }
    printf('\n');
}

也许是这样吗?