Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Checking surrounding indexes of an index in a 2d array

  1. #1
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Question Checking surrounding indexes of an index in a 2d array

    Hey guys, I have a question on what may be a good way to check the surrounding indexes of a certain index of an array.
    For example, if i want to check the surrounding indexes of the index 6 I want to check 1 2 3 5 7 9 a b

    1 2 3 4
    5 6 7 8
    9 a b c

    when I check the surrounding indexes I want to check if the object == null. If so, I want to check the surrounding indexes of that index.

    so if 7 == null then check 2 3 4 8 c b a 6

    I have thought of recursion but I know its not very efficient and should be avoided and I also got a stack overflow

    this is the crap I wrote which I am not very proud of and definitely want to get rid of :(

    Thanks in advanced,
    Cheers

    Java Code:
        public void revealEmpty(int x ,int y)
        {
          if(x < 0 || x > mf.cols || y < 0 || y > mf.rows)
              return;
          if(mf.field[x][y] != null)
              return;
          buttons[x][y].setEnabled(false);
          revealEmpty(x-1,y);
          revealEmpty(x+1,y);
          revealEmpty(x,y-1);
          revealEmpty(x,y+1);
          revealEmpty(x-1,y-1);
          revealEmpty(x-1,y+1);
          revealEmpty(x+1,y-1);
          revealEmpty(x+1,y+1);
        }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,188
    Blog Entries
    7
    Rep Power
    24

    Default Re: Checking surrounding indexes of an index in a 2d array

    In your example you use a single index value, but in your code you use two index values (x and y); which is it?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Checking surrounding indexes of an index in a 2d array

    I think i did not explain my example right my bad but in my code I am passing in x, y which are the indexes for a 2d array. The array is in the mf object so when i say mf.field i refer to the array.

    in my example i should have said
    for index [1][1] I want to check [0][0], [0][1],[0][2],[1][0],[1][2],[2][0],[2][1],[2][2]

    1 2 3 4
    5 6 7 8
    9 a b c

    Sorry for the confusion

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,188
    Blog Entries
    7
    Rep Power
    24

    Default Re: Checking surrounding indexes of an index in a 2d array

    For two dimensional indexes it's easy:

    Java Code:
    for (int dx= -1; dx <= 1; dx++)
       for (int dy= -1; dy <= 1; dy++)
          if (!(dx == 0 && dy == 0))
             // check cell (x+dx, y+dy) ...
    kind regards,

    Jos
    romero4742 likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Checking surrounding indexes of an index in a 2d array

    Awesome it will take some recursion away :D thanks a lot

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,188
    Blog Entries
    7
    Rep Power
    24

    Default Re: Checking surrounding indexes of an index in a 2d array

    Quote Originally Posted by romero4742 View Post
    Awesome it will take some recursion away :D thanks a lot
    You're welcome and indeed, no recursion is needed here but don't be afraid of recursion; most of the times recursion is your friend and it allows for very elegant solutions of the problem.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 5
    Last Post: 09-17-2012, 06:47 AM
  2. Replies: 1
    Last Post: 11-13-2011, 12:25 PM
  3. Replies: 1
    Last Post: 06-06-2011, 08:57 AM
  4. Removing indexes from an array.
    By Cruncher in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 09:41 PM
  5. array indexes
    By Kaito in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 04:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •