Thread: Minesweeper adjacent mines with one dimensional array?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Minesweeper adjacent mines with one dimensional array?

    So I've searched around the internet for a way to check for adjacent mines. And a lot of algorithms for it do exist out there. The thing is
    that all of the solutions that I've encountered for the algorithm is using a two dimensional array while I want to use a one dimensional array.

    I will create my array and initialize 10 mines at random positions. Then I want to count the mines around these squares that are initialized to 0.

    So the solutions out there for two dimensional arrays work, but how would I go with a one dimensional array?

    Also consider that I don't take in x and y coordinates in the function that creates the board.

    So I would need some tips or refering for who to do this.
    Thanks in advance!

    Research links:
    Simple Minesweeper [C] -- Kinda eh..

    Implementation of Minesweeper Game - GeeksforGeeks
    Last edited by DecoratorFawn82; 10-05-2018 at 02:09 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,609
    I think you should be able to copy the 2D array algorithm whole cloth if you just convert the i, j coordinates to a one dimensional subscript.

    It's a simple formula i * x + j where x is the total number of columns in the "2D" array.
    Last edited by whiteflags; 10-05-2018 at 02:28 PM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Refering to the second link inmy post.

    How would I use that formula in:

    Code:
    bool isMine (int row, int col, char board[][MAXSIDE]) 
    { 
        if (board[row][col] == '*') 
            return (true); 
        else
            return (false); 
    }
    Since it is no for loop inside of this function?
    Should I just let the function take in the index
    as parameter and calculate the index using
    i * x + j as an argument like this?

    Code:
    bool isMine (int index, char board[][MAXSIDE]) 
    { 
       if (board[index] == '*') 
           return (true); 
       else
           return (false); 
    }
    Calling the function:
    Code:
    if (isMine (i * x + j, board)) {
      // ...
    } 
    Last edited by DecoratorFawn82; 10-05-2018 at 03:47 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,609
    That wouldn't be what I would do, personally. I would pass i and j into the function and calculate i*x+j internally. This way, you can think of board the way you are using it, even though it is just a one dimensional array. Consider that if you call isMine(i*x+j, board); if you mess up the argument, the function will check the wrong cell, and it's better to codify the formula so no human can mess it up (short of simply brainfarting on i and j).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-19-2017, 04:46 AM
  2. Assigning One dimensional array to two dimensional array
    By lightning_star in forum C Programming
    Replies: 1
    Last Post: 03-19-2014, 09:44 PM
  3. Replies: 12
    Last Post: 09-02-2013, 07:50 PM
  4. Replies: 4
    Last Post: 09-02-2013, 11:19 AM
  5. Land Mines
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-19-2003, 12:00 PM

Tags for this Thread