Search code examples
cminesweeper

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

Solution

  • Your basic problem is that you are counting before you read the input, i.e. before you know what x and y is. 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.

    Further it seems to me that your big if statement is hard to read. 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);
    }