Results 1 to 3 of 3

Thread: 2D Array: Finding Adjacent Elements

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Question 2D Array: Finding Adjacent Elements

    Ok, I have been looking all over the place for an answer to this, but nothing seems to be helping with quite what I need need.

    My program is a simple human vs alien sim where the life forms (humans and aliens) are stored in a 2D array. The 2D array is used to keep track of the locations the life forms are currently located in. In order for the life forms to attack each other, they must be adjacent to each other. This is where I find my problem, I am not quite sure how to find out of they are adjacent to each other.

    Currently I have nested loops that I am using as well as a couple 'if' statements that are suppose to check the top, bottom, left, and right. Diagonals are not allowed in the program.

    I have searched for 'Adjacency Matrix' and am not quite sure if that is what this is considered. Plus, anything I found all seemed to use integers and not objects, which is what mine is, an array of objects.

    I hope someone can help me with this. Thanks in advance for the help and I will try to give extra info if needed.

    vb Code:
    1. for(int i = 0; i < room.length; i++)
    2.         {
    3.             for(int j = 0; j < room[i].length; j++)
    4.             {
    5.                 if(j > 0 && room[i][j] == room[i][j - 1])
    6.                 {
    7.                     System.out.println("test");
    8.                 }else if(j < 0 && room[i][j] == room[i][j + 1])
    9.                 {
    10.                     System.out.println("test");
    11.                 }else if(i > 0 && room[i][j] == room[i - 1][j])
    12.                 {
    13.                     System.out.println("test");
    14.                 }else if(i < 0 && room[i][j] == room[i + 1][j])
    15.                 {
    16.                     System.out.println("test");
    17.                 }
    18.             }
    19.         }

  2. #2
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    Re: 2D Array: Finding Adjacent Elements

    If you can't check the diagonals then there is really know way to know if the other is there with absolute certainty.
    Two ideas come to mind:
    You could eliminate the spots where the other character is not at by marking the spots in the array you check with a letter. If that letter is there then you have checked it. however if the other character is moving this will not work.

    Another thought is if you can still check horizontally and vertically then find the other character that way then move to a position you can attack from. Try and use walls to corner the other.

    What is the board size?
    On the off chance that I helped you (Rate This Post){
    Also If your problem was resolved;
    }

  3. #3

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: 2D Array: Finding Adjacent Elements

    I actually managed to solve this problem, probably not the cleanest way...but it works. This was actually for a school project that was due yesterday (Oct 31), I just left the thread open to see other ideas people may have.

    It's not that I can't check for diagonals, its just that we didn't need to for the project.

    To answer your question, the board size varies per game, it doesn't have a fixed size.

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
  •  



Click Here to Expand Forum to Full Width