Results 1 to 9 of 9
  1. #1
    zeppelin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #3
    zeppelin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    zeppelin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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...

  6. #6
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

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

  7. #7
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    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;
    }

  8. #8
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    You also need to check if the position is adjacent to you. So:
    Quote 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 ) {
                rooms.add( new Position(row, col) );
             }
          }
       }
       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 12:04 PM. Reason: I forgot to add my code...

  9. #9
    zeppelin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you very much all of you...

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

Similar Threads

  1. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 08:16 PM
  2. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 02:34 AM
  3. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 02:24 AM
  4. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM

Posting Permissions

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