Results 1 to 9 of 9
Thread: A little help with arrays..
- 01-05-2009, 01:46 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
A little help with arrays..
Hello,
I am writing a program that an ace is moving randomly in a 5x5 zero-array and filling aces on its way.
So the ace moves randomly and can move only to a neighbor tile which is not 1 (an ace can not move to the same tile twice)
My problem is how can I detect when an ace is surrounded with aces and cannot move further?
I tried the following but didnt work because was out of bounds for the cases that the ace was at the boundaries of the array
Java Code:if ( box[x+1][x+0]!=0 && box[x+0][x-1]!=0 && box[x-1][x+0]!=0 && box[x+0][x+1]!=0 && box[x+1][x-1]!=0 && box[x-1][x-1]!=0 && box[x+1][x+1]!=0 && box[x-1][x+1]!=0){ }
Tnx a lot!!!
- 01-05-2009, 04:32 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You must have a well design logic to do this. What are the surrounding of an element. What happen on the borders and middle elements.
- 01-05-2009, 10:04 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
Eranga thank you very much fro your response!
Consider that there is an all zero 5x5 array.
Then we put an ace in a random position lets say at (1,0).
The ace moves randomly and
1) can go only at a zero tile
2) can go only on a neighbor tile
2) can move only inside the array's boundaries
3) if instructed to go out of boundaries it just does not go and wait for the next random direction
An example:
The ace starts at (1,0)
Then goes at (0,1),
Then is instructed to go at (1,0) but it doesnt go because (1,0) is already 1
Then goes at (1,1)
And finally at (0,0)
Now it is surrounded by aces and the matrix boundaries <- How can I detect that??
Tnx a lot !!
- 01-05-2009, 11:04 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean by this.
1) can go only at a zero tile
- 01-05-2009, 11:12 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
- 01-05-2009, 11:29 AM #6
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
The matrix boundaries can be easy detected. Let's pretend your ace has two variables named x and y. Then your if condition is:
Java Code:(ace.x > 0 && ace.x < 5) && ( ... same with y ...)
- 01-05-2009, 11:32 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
boolean arrayField(){
int x=0;
outer:
for(x=0;x<5;x++){
for(int y=0;y<5;y++){
if(box[x][y]==0)
// if we find 0 in any element of the array is we need not check //further
break outer;
}
}
if(x==5)
return true;
else
return false;
}
- 01-05-2009, 11:58 AM #8
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
You also need to check if the position is adjacent to you. So:
To check if a Position is near to you:
Originally Posted by dswastik
Coding it is up to you...| yourX - positionX | <= 1
Same with y...
The class Position defines an x and y integer fields:
Then you can choose at random among the elements returned by getAdjacentEmptyPlaces()Java Code:class Position { int x, y }Last edited by raffaele181188; 01-05-2009 at 12:04 PM. Reason: I forgot to add my code...
- 01-05-2009, 12:33 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07-21-2008, 02:34 AM -
Arrays
By bunbun in forum New To JavaReplies: 1Last Post: 04-09-2008, 02:24 AM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 02:45 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks