|
-
Oct 29th, 2011, 03:50 PM
#1
Thread Starter
Member
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:
for(int i = 0; i < room.length; i++) { for(int j = 0; j < room[i].length; j++) { if(j > 0 && room[i][j] == room[i][j - 1]) { System.out.println("test"); }else if(j < 0 && room[i][j] == room[i][j + 1]) { System.out.println("test"); }else if(i > 0 && room[i][j] == room[i - 1][j]) { System.out.println("test"); }else if(i < 0 && room[i][j] == room[i + 1][j]) { System.out.println("test"); } } }
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|