# Thread: Minesweeper game in C, segmentation fault error

1. ## Minesweeper game in C, segmentation fault error

I have a college assignment and I am having segmentation fault with one of the functions. The assignment is about minesweeper game. Detailed description of the functions:
random board ():
randomly positions num mines in the matrix, taking care not to place any mines in position i, j neither around position i, j. At the end of the call:

• each position of the matrix must be either UNKN FREE or UNKN MINE,
• there must be exactly num mines positions with UNKN MINE value,
• no position around i, j (included) must be UNKN MINE.

flag board ():
add a flag (flag) in position i, j:

• if position i, j has already been shown, it does nothing and returns 0,
• if position i, j is already "flagged", remove the flag (FLAG FREE becomes UNKN FREE and FLAG MINE becomes UNKN MINE) and returns -1,
• if position i, j is not "flagged" add a flag (UNKN FREE becomes FLAG FREE and UNKN MINE becomes FREE MINE) and returns 1.

display board ():
shows the content in position i, j

• if position i, j has already been shown or is "flagged" it does nothing and returns 0,
• if position i, j has not already been shown and contains a mine, place the constant MINE in i, j and returns -1,
• if position i, j has not already been shown and does not contain a mine, place in i, j the enumerative constant indicating the number of mines surrounding position i, j (ie C0, .. ., C8). If position i, j has no mines around it (i.e. it becomes C0) then all undiscovered positions around i, j are revealed. The same algorithm is repeated as long as positions marked with C0 are discovered. The function returns the total number of detected positions.

expand board ():
show the contents of the positions not yet unfolded around i, j. The behavior is exactly the same as for the display board (), return value included, but applied to all the cells not revealed around i, j. Cases where the function does nothing and returns 0:

• if the position i, j has not already been shown,
• if position i, j does not contain around a number of flag-gated cells equal to the number shown in i, j

display_ board() is having the segmentation error.
I am leaving below the code. Here is codeboard to check out the errors Codeboard * the IDE for the classroom
code.txt

2. We can't tell anything without a main() function to show how you call those functions.

3. Originally Posted by everythingpro
I have a college assignment and I am having segmentation fault with one of the functions.
If you're advanced enough to write a minesweeper game then you should be past the stage where people have to point out this type of bug in your code on the forum. Every C programmer gets segmentation faults, everyone has to debug them.

One powerful tool is valgrind. If you have valgrind, it will tell you where the program first made an illegal memory access, which might not be where it crashed.

If you don't have valgrind, then use the following technique. Reduce your program to "Hello world" and verify that it runs and exits normally. Then comment in the program agan, and verify that it still segfaults.
Then slowly comment in to the hello world program and comment out of the full program. Note when the hello world program crashes or the commented out program falis to crash. Eventually you should be able to toggle between crashes / doesn't crash with a single line, and that will usually tell you where the bug is.

Note that this technique is effectively a binary search.It doesn't matter how large your program is. You can isolate the crash in logarithmic time.