Thread: Minesweeper game in C, segmentation fault error

  1. #1
    Registered User
    Join Date
    Nov 2020

    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
    Last edited by everythingpro; 11-11-2020 at 03:14 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    We can't tell anything without a main() function to show how you call those functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Quote Originally Posted by everythingpro View Post
    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.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 06-25-2013, 11:07 PM
  2. snake game segmentation fault
    By dingdong in forum Game Programming
    Replies: 3
    Last Post: 04-30-2011, 11:44 AM
  3. Segmentation fault Error
    By unknown_ in forum C Programming
    Replies: 7
    Last Post: 03-21-2010, 01:32 PM
  4. Segmentation fault with card game
    By bwisdom in forum C Programming
    Replies: 6
    Last Post: 11-05-2009, 06:59 PM
  5. Hangman Game - Rewrite (Segmentation Fault?)
    By tigrfire in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 07:56 PM

Tags for this Thread