-1

My own minesweeper I am trying to make

#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>


int randNum(int minimum, int maximum);
int checkSurroundings(int x, int y);


//settings:
int diffi = 3;                        //how frequently mines show up, 1/n
const int width = 10, height = 10;    //height and width of the board
int state[10][10];                    //couldn't make it dynamic with W and H,
                                      //it gives me an error that it is at file scope
int mineshape = 'X';

//the main
void main(){

    srand(time(NULL));      //seeds the randomizer once at the start

    state[height][height];

    for(int y = 0; y < height; y++){

        printf("/ ");

        for(int x = 0; x < width; x++){

            state[x][y] = randNum(0, diffi);

            //assigns the board values accordingly
            if (state[x][y] == diffi){

                state[x][y] = mineshape;
            }
            else{

                state[x][y] = checkSurroundings(x, y);
            }

            printf("%c ", state[x][y]);
        }

        printf("/  %d\n",y + 1);
    }
}

the RNG I used to generate mine cells: (I don't think this has anything to do with the problem), I got it from google

int randNum(int minimum, int maximum){

    int result = 0, low_num = 0, hi_num = 0;

    if (minimum < maximum){

        low_num = minimum;
        hi_num = maximum + 1;
    }
    else{

        low_num = maximum + 1;
        hi_num = minimum;
    }

    result = (rand() % (hi_num - low_num)) + low_num;

    return result;
}

this was my implementation to loop over the surrounding cells and check them:

/*int checkSurroundings(int x, int y){

    int mines = 0, i, j;

    for(j = y-1; j <= y+1; j++){

        for(i = x-1; i <= x+1; i++){

            if(state[i][j] == mineshape){

                mines++;
            }
        }
    }

    return mines + 48;
}
*/

here I used a manual code to check surroundings, marked the broken instructions with x in the comment: both implementations have the same problem with checking all the cells around.

int checkSurroundings(int x, int y){

    int mines = 0, i, j;

    if(state[x-1][y-1] == mineshape){   //top left
        mines++;
    }
    if(state[x][y-1] == mineshape){     //top
        mines++;
    }
    if(state[x+1][y-1] == mineshape){   //top right
        mines++;
    }

    if(state[x-1][y] == mineshape){     //left
        mines++;
    }
    if(state[x+1][y] == mineshape){     //x right
        mines++;
    }

    if(state[x-1][y+1] == mineshape){   //x bottom left
        mines++;
    }
    if(state[x][y+1] == mineshape){     //x bottom
        mines++;
    }
    if(state[x+1][y+1] == mineshape){   //x bottom right
        mines++;
    }

    return mines + 48;
}

it for some reason doesn't check the cells at the right, bottom-left, bottom, bottom-right cells around the current cell, and I can't figure it out. I am new to coding, started learning C around 2 months ago.

halfer
  • 19,471
  • 17
  • 87
  • 173
Punchler
  • 1
  • 2
  • 1
    Unrelated to your problem, but `state[height][height];` will give you *undefined behavior*. You index out of bounds of your arrays, to get a value that you then throw away. You don't need that statement, and considering the UB you should definitely not have it. – Some programmer dude May 09 '22 at 12:36
  • 1
    As for your problem, you can call `checkSurroundings` with `x` or `y` being equal to `0`. What do you think `x - 1` or `y - 1` will do? Same in the other direction where `x + 1` and `y + 1` can go out of bounds. Don't do these checks if you're on the border already. – Some programmer dude May 09 '22 at 12:37
  • Edit the question to provide a [mre]. If you think the problem is in `checkSurroundings`, then create a `main` routine that defines a small array, fills it with data that reproduces the problem, calls `checkSurroundings` once, and prints the incorrect result. Show the observed output and the output desired instead. Use fixed data for debugging; do not initialize with `srand(time(NULL));`. – Eric Postpischil May 09 '22 at 12:38
  • I think you are confusing x and y relative to height and width. You are comparing x against width, but using it as the first index into the two dimensional array, which would be its row (height). Same for y against height. Whilst the board as defined is square, this might not matter. You are certainly exceeding the bounds (under and over) of the array, which will cause column references to bleed into other rows and throw things off. – Dave Meehan May 09 '22 at 12:41
  • update: I did more analyzing to try and fix the problem, found out that when I assign values to the board manually, everything worked fine but the problem rises when the RNG gives the board values, anything to do with time? is the program skipping something? – Punchler May 09 '22 at 14:14
  • If you are unable to reliably reproduce the problem due to the random number generator, then I suggest that you change the line `srand(time(NULL));` to `srand(1);`, so that the random number generator will always generate the exact same sequence of random numbers whenever you run the program. You can then see if that reproduces the problem. If not, then try `srand(2);` and `srand(3);` etc., until you are able to reliably reproduce the problem. This will make debugging much easier. Once you have fixed your bug and your program works, you can change it back to `srand(time(NULL));`. – Andreas Wenzel May 09 '22 at 15:31
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 09 '22 at 15:35

0 Answers0