简体   繁体   English

我找不到代码错误。 扫雷车C

[英]I can't find an error of the code. minesweeper C

I'm trying to make when I put some numbers in scan, and if there's mine(*), print boom and if there's no mine, print the number of mines near to it. 我尝试扫描一些数字时进行打印,如果有地雷(*),则打印繁荣,如果没有地雷,则打印附近的地雷数量。 I can't find a problem with the code, but there's an error. 我找不到代码有问题,但是有一个错误。 Please verify it if you find the problem. 如果发现问题,请进行验证。

#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;
}

Your basic problem is that you are counting before you read the input, ie before you know what x and y is. 您的基本问题是阅读输入之前(即在您知道xy是什么之前)进行计数。 In other words, currently you are counting "something" on the whole minefield which isn't what you want. 换句话说,当前您正在整个雷区上计算“某物”,这不是您想要的。

So the first thing to do is to read x and y before counting. 因此,要做的第一件事是在计数之前先读取xy

Further it seems to me that your big if statement is hard to read. 此外,在我看来,您的大if陈述很难阅读。 You could reorganize it like: 您可以像这样重组它:

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

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

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