gpt4 book ai didi

c - 如何检查数组位置是否为某个数字

转载 作者:行者123 更新时间:2023-11-30 16:18:35 26 4
gpt4 key购买 nike

我正在进行一个项目(用 C 语言),正在编写宾果游戏,最后还有一个函数需要制作,即检查是否有宾果游戏。我有一个 5 x 5 生成的随机数数组,我通过用户输入生成一个随机数。如何让数组将该数字(如果实际上在数组中)更改为 0,然后通过用户输入检查是否有宾果游戏?

这是生成数组的代码

for (int row = 0; row < 5; row++) {
for(int column = 0; column < 5; column++) {
if(row == 2 && column == 2) {
board[row][column] = 0;
} else {
int num = rand() %15 + 1 +(column * 15);
for(int i = 0; i < 75; i++) {
if(num == used[i]) {
num = rand() %15 + 1 +(column * 15);
}
}
board[row][column] = num;
used[used_counter] = num;
used_counter++;
}
}
}
int generate_number(int boneyard[75], int *boneyard_counter) {
int num = rand() %75 + 1;
for (int i = 0; i <75; i++){
if(num == boneyard[i]);
num = rand() %75 + 1;
}
boneyard[*boneyard_counter] = num;
boneyard_counter++;
return num;
}

这是生成随机数的代码。

最佳答案

你会做这样的事情:

    int found_match = 0;
for(int row = 0; row < 5; ++row){
for(int column = 0; column < 5; ++column){
if(board[row][column] == num){
board[row][column] = 0;
found_match = 1;
goto loop_end;
}
}
}
loop_end:
if(found_match){
/*
Check all diagonals, rows, and columns to see if any one of them contains only zeroes,
stopping if you find that a diagonal/row/column that does in fact contain only zeroes
and informing the player that they've won.
*/
}

关于c - 如何检查数组位置是否为某个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com