在 C 中检查数组中的答案时如何防止空行?

How do you prevent blank lines when checking answers in an array in C?

我正在为一个项目用 C 语言制作宾果游戏,并且已经完成了最初的棋盘创建: B = 1-20, I = 21-40, N = 41-60, G = 61-80, O = 81-99。 我还能够随机生成一对不重复的 letter/number 对,用于玩以下游戏:

int main()
{
    srand(time(0));

    int rollcount = 0, rollc, rollr, nohund, basec=5, base=20;
    const char BINGO[5] = {'B','I','N','G','O'};
    bool check[101];

    //fills check array with falses that will change to true as numbers are picked.        
    for(int i = 0; I < 100; i++) 
        check[i]=false;
    
    //marks free space as true, free = 100 on board
    check[100]=true; 

    do
    {
        //test var to prevent duplicate "rolls" until a game is won
        bool reroll=true; 

        rollcount++;

        //here's where the problem starts
        //This is meant to loop if the number rolled has been rolled before.
        do  
        {
            //getchar();
            if(getchar() == 'q')
            {
               //future code to display stats and escape game
               return 0;
            }

            rollc = rand() %basec;  //pick random column

            //pick random number in range of selected column
            rollr = rand() %base + 1;
            rollr += (rollc * 20);  

            //limits last col to 99 instead of 100
            nohund = (rollr == 100 ? rollr -= 1 : rollr);  

            if (check[rollr] == false)  //checks if number has been used
            {
                //prints the number of the roll, column letter, and random number
                printf("%d: %c-%d", rollcount, BINGO[rollc], rollr);
                    
                check[rollr]=true;  //prevents future use of that number
                reroll=false;  //escapes loop to roll another number
            }
        }
        //roll current roll again when duplicate is generated
        while(reroll==true);  
    }
    //intended to roll with each press of 'ENTER'
    while(getchar()=='\n');  
}

我已经尝试了一些东西。如上所述,主要问题是如果发现重复项,它会在 if() 语句中显示来自 getchar() 输入的空行。随着越来越多的数字被发现,线条之间的差距越来越大(由于需要更多的 ENTER 按键才能找到一个唯一的数字),留下一个几乎空白的屏幕。按一次 ENTER,我需要它循环直到找到合适的数字。如果我摆脱所有 getchar() 表达式,程序将执行此操作,我可以按 ENTER 直到我得到 99 个唯一数字,但我需要允许按 q 退出程序。

示例输出

1: B-15  
2: G-78  
3: I-37  
4: G-62  

总而言之,我需要它像没有 getchar() 时那样显示数字,通过所有 99 种可能性,让我退出 q

请帮忙,提前谢谢你。

让我们首先对代码的其他部分应用一些修复,以确保一切按预期运行。这是不带参数的主函数的典型签名:

int main(void)

这里中间的I需要小写,否则代码编译不通过或者会使用复数中的虚数:

// Note: index 0 is not used
for(int i = 1; i < 100; i++) 

使程序运行的最直接方法是将 getchar() == 'q' 检查移到 循环 之前。 do-loop 的功能现在是循环,直到它找到一个以前没有选择过的数字。现在的问题是,如果所有数字都被占用,这将永远循环。因此,我们将添加一个额外的检查:只有在仍有可用号码时才循环。

if(getchar() == 'q')
{
    return 0;
}

bool aNumberIsAvailable;
do
{
    // ... same code as before ...

    aNumberIsAvailable = false;
    for(int i = 0; i < 100; i++) 
    {
        if (!check[i])
        {
            aNumberIsAvailable = true;
            break;
        }
    }
}
while(reroll==true && aNumberIsAvailable);

也就是说,有更好的方法来设计这个程序。一个简单的步骤就是将两个 getchar 组合成一个,就好像它是一个菜单:'\n' 结果表示“掷出另一个数字”,'q' 结果表示“退出”。正如 user3386109 所建议的,有更好的方法来解决“样本无替换”问题。
最后,请注意 getchar 不会 检测按键。它只是从(终端)输入缓冲区中读取一个字符。如果您想看到实际的键 up/down 移动,您将需要一个库,它可以让您更直接地访问键盘,例如 SDL2。