|
-
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"); } } }
-
Nov 1st, 2011, 03:09 AM
#2
Lively Member
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;
}
-
Nov 1st, 2011, 09:37 AM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|