# A little help with arrays..

• 01-05-2009, 02:46 AM
zeppelin
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
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, 05:32 AM
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.
• 01-05-2009, 11:04 AM
zeppelin
Quote:

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 !!
• 01-05-2009, 12:04 PM
Eranga
What you mean by this.

Quote:

1) can go only at a zero tile
• 01-05-2009, 12:12 PM
zeppelin
Quote:

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...
• 01-05-2009, 12:29 PM
raffaele181188
The matrix boundaries can be easy detected. Let's pretend your ace has two variables named x and y. Then your if condition is:
Code:

`(ace.x > 0 && ace.x < 5) && ( ... same with y ...)`
• 01-05-2009, 12:32 PM
dswastik
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, 12:58 PM
raffaele181188
You also need to check if the position is adjacent to you. So:
Quote:

Originally Posted by dswastik
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 ) {             rooms.add( new Position(row, col) );         }       }   }   return rooms; }```

To check if a Position is near to you:
Quote:

| yourX - positionX | <= 1
Same with y...
Coding it is up to you...
The class Position defines an x and y integer fields:
Code:

```class Position {   int x, y }```
Then you can choose at random among the elements returned by getAdjacentEmptyPlaces()
• 01-05-2009, 01:33 PM
zeppelin
Thank you very much all of you...

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