简体   繁体   English

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

[英]C: For-loop is executing despite ignoring initialization and increment expressions, therefore always true

CONTEXT:语境:

Hello, I'm trying to print a 7x6 Connect Four board where each section is |___|您好,我正在尝试打印一个 7x6 Connect Four 板,其中每个部分都是|___| with three underscores.三个下划线。 I want to create each center underscore an element of a 2D array so I can later update it.我想创建每个中心下划线的 2D 数组元素,以便稍后更新它。

CONFLICT:冲突:

I am getting no errors or warnings, but my output is just |________ ... with an infinite amount of underscores.我没有收到任何错误或警告,但我的输出只是|________ ...带有无限数量的下划线。 I have successfully rewritten the code where three underscores are printed without assigning the center into an array (however obviously this code is useless for the sake of making an actual game since I can't update the center underscores).我已经成功地重写了代码,其中打印了三个下划线,而没有将中心分配到数组中(但显然,由于我无法更新中心下划线,因此该代码对于制作实际游戏毫无用处)。 All the loop declarations in my current code were used in that successful variation, so I'm pretty sure those are not my issue.我当前代码中的所有循环声明都用于那个成功的变体,所以我很确定那些不是我的问题。 I can provide that code as well if you think it can help you.如果您认为它可以帮助您,我也可以提供该代码。 What I do know is that colCnt (column count) is incrementing forever and undCnt (underscore count) is stuck at 2. Because of this, I suspect that this for-loop is the primary issue in my code, however I do not know where:我所知道的是 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]);

                    }

                }

CODE:代码:

#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

    */

}

If you have any questions just let me know.如果您有任何问题,请告诉我。 Thanks!谢谢!

Have you considered attempting to print "___|"您是否考虑过尝试打印“___|” all at once?一次全部? Then all you have to do is check column count, if it is 0 print a '|'然后你所要做的就是检查列数,如果是 0 打印一个 '|' if it is the maximum column value add a newline character.如果是最大列值,则添加换行符。

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');
}

maybe something like this?也许是这样的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM