gpt4 book ai didi

c - 我找不到代码的错误。扫雷C

转载 作者:行者123 更新时间:2023-11-30 16:46:29 27 4
gpt4 key购买 nike

我试图在扫描中输入一些数字,如果有我的(*),则打印繁荣,如果没有我的,则打印它附近的地雷数量。我找不到代码的问题,但是有一个错误。如果发现问题请验证。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10

int main (void)
{
char minefield [N][N];
int i, j;
int k;
int x, y;
int count;
int mine_number;

count = 0;
mine_number = N*N/10;

srand((long)time(NULL));


for (k=1; 0< k < mine_number; k=k+1) {
i = rand() % N;
j = rand() % N;
minefield [i][j] = '*';
}

for (i=0; i < N; i=i+1) {
for (j=0; j < N; j=j+1) {
count = 0;
if (minefield[i][j] != '*') {
if (i == 0) {
if (j == 0) {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
}
else if (j == N-1) {
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}

else {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
}

else if (i == N-1) {
if (j == 0) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
}

else if (j == N-1) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}

else {
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
}
}

else {
if (j == 0) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
}

else if (j == N-1) {
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;

}
}

else {
if (minefield [i-1][j-1] == '*') {
count = count + 1;
}
if (minefield [i-1][j] == '*') {
count = count + 1;
}
if (minefield [i-1][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j+1] == '*') {
count = count + 1;
}
if (minefield [i][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j-1] == '*') {
count = count + 1;
}
if (minefield [i+1][j] == '*') {
count = count + 1;
}
if (minefield [i+1][j+1] == '*') {
count = count + 1;
}
}
}
}
}
}

scanf("%d %d", &x, &y);

if (minefield[x][y] = '*') {
printf("boom");
}

else {
printf("%d", count);
}

return 0;
}

最佳答案

您的基本问题是您在读取输入之前进行计数,即在知道xy是什么之前。换句话说,目前您在整个雷区上计算“某物”,这不是您想要的。

所以首先要做的就是在计数之前读取 xy

此外,在我看来,你的大 if 语句很难阅读。您可以像这样重新组织它:

if (scanf("%d %d", &x, &y) != 2 || x < 0 || x >= N || y < 0 || y >= N)
{
// Illegal input
exit(1);
}

if (minefield[x][y] == '*') {
printf("boom");
}
else {
count = 0;

if (x-1 >= 0 && y-1 >= 0) count += (minefield[x-1][y-1] == '*');
if (x-1 >= 0) count += (minefield[x-1][y] == '*');
....
.... Add code to cover all 8 combinations (i.e. add the 5 missing combinations)
....
if (x+1 < N && y+1 < N) count += (minefield[x+1][y+1] == '*');

printf("%d", count);
}

关于c - 我找不到代码的错误。扫雷C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43716013/

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