# Thread: A little help with arrays..

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!!!

2. 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.

3. Member
Join Date
Jan 2009
Posts
4
Rep Power
0
Originally Posted by Eranga
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.
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 !!

4. Member
Join Date
Jan 2009
Posts
4
Rep Power
0
Originally Posted by Eranga
What you mean by this.

All array's elements are initially zero. When the ace visits a square in the matrix then this square becomes 1.

The ace can visit a square of the array only once...

5. 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 ...)`

6. Senior Member
Join Date
Dec 2008
Location
Kolkata
Posts
283
Rep Power
9
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;
}

7. 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:
Originally Posted by dswastik
Java Code:
```public List<Positon> getAdjacentEmptyPlaces() {
List<Position> rooms = ...
for (... each row near to you...) {
for (...each col near to you...) {
//If the specified element has not been visited yet
if ( matrix[row][col] == 0 ) {
}
}
}
return rooms;
}```
To check if a Position is near to you:
| yourX - positionX | <= 1
Same with y...
Coding it is up to you...
The class Position defines an x and y integer fields:
Java Code:
```class Position {
int x, y
}```
Then you can choose at random among the elements returned by getAdjacentEmptyPlaces()
Last edited by raffaele181188; 01-05-2009 at 01:04 PM. Reason: I forgot to add my code...

8. Member
Join Date
Jan 2009
Posts
4
Rep Power
0
Thank you very much all of you...

I will work on your ideas an let you know :)

#### Posting Permissions

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