best way to stock a map

Discussion in 'Java' started by charly, Sep 10, 2003.

  1. charly

    charly Guest

    Greetings,

    I am trying to make an app simulating ants running on the map.

    I am wondering about the best way to solve the "map" problem.

    so far, I' ve decided to create a vector which would hold each cell as a
    Cell object (x,y,type of ground, content)

    Is this ok ?

    When calling the draw method of the map, i would use an iterator to draw
    each cell, according to its content.

    When a ant is in a square, a copy of herself is put in the content of
    the square (so 2 ants can detect that they are in neighbouring cells
    because each time a ant moves in a square, she looks around her ) and
    removed from the previous square.

    All ants are stocked in another vector.

    I iterate all ants each time the global thread is awoken and run the act
    method of each ant (changing its coordinates....).

    Has someone have any ideas how I could improve or change the way I did
    things so far ?

    Many thx !
     
    charly, Sep 10, 2003
    #1
    1. Advertising

  2. charly

    Roedy Green Guest

    On Wed, 10 Sep 2003 13:57:49 +0200, charly <> wrote
    or quoted :

    >so far, I' ve decided to create a vector which would hold each cell as a
    >Cell object (x,y,type of ground, content)
    >
    >Is this ok ?


    Just how do you use this "map"?

    Is it for noticing when two ants are close? You only have to examine
    the cell and surrounding cells, not all the ants in all the cells.

    It is just to render a variety of simulated landscapes, then have a
    strange grid-like regularity?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 10, 2003
    #2
    1. Advertising

  3. charly wrote:
    > Greetings,
    >
    > I am trying to make an app simulating ants running on the map.
    >
    > I am wondering about the best way to solve the "map" problem.
    >
    > so far, I' ve decided to create a vector which would hold each cell as a
    > Cell object (x,y,type of ground, content)
    >
    > Is this ok ?


    Depends on exactly what you want to do. If you only have ants or terrain
    in a cell, I think you could get away with making Cell this simple:

    class Cell
    {
    boolean occupied; // Whether the cell has an ant in it or not
    int terrain; // terrain type
    }

    See more below.

    > When calling the draw method of the map, i would use an iterator to draw
    > each cell, according to its content.
    >
    > When a ant is in a square, a copy of herself is put in the content of
    > the square (so 2 ants can detect that they are in neighbouring cells
    > because each time a ant moves in a square, she looks around her ) and
    > removed from the previous square.


    Well, assuming the map is rectangular, you need to check more than two
    neighbors. A cell can have 8 adjacent cells. My suggestion would be to
    make map something like this:

    class Map
    {
    // Two dimensional "map"
    Cell[][] cells;

    public Map(int y, int x)
    {
    cells = new Cell[y][x];
    fill(); // Some method to fill the map with cells
    // Probably a random terrain type and unoccupied
    }

    public void occupy(int cell_y, cell_x)
    {
    // Marks a cell occupied
    }

    public void unoccupy(int cell_y, cell_x)
    {
    // Marks cell unoccupied
    }
    // More methods as you see fit
    }

    Now, the ant move method becomes pretty simple. The ant needs to keep
    track of its last position on the map. When the ant moves it first
    inspects all adjacent cells in the array. If there are any free cells
    unoccupy old position and occupy new, else the ant stays where it is.
    (As a side note, you don't even need an ant object with this map, since
    you can deduct the presence of an ant by the fact that a cell is
    occupied. Hence, you could walk the map, and inspect adjacent cells of
    any cell that is occupied.)

    When drawing the map, you simply draw the cells array in the map,
    converting indexes to graphical coordinates. If the cell is occupied
    draw an ant, else draw the terrain type.

    > All ants are stocked in another vector.
    >
    > I iterate all ants each time the global thread is awoken and run the act
    > method of each ant (changing its coordinates....).
    >
    > Has someone have any ideas how I could improve or change the way I did
    > things so far ?
    >
    > Many thx !
    >



    --
    Daniel Sjöblom
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 10, 2003
    #3
  4. charly

    charly Guest


    >
    > Just how do you use this "map"?
    >
    > Is it for noticing when two ants are close? You only have to examine
    > the cell and surrounding cells, not all the ants in all the cells.
    >
    > It is just to render a variety of simulated landscapes, then have a
    > strange grid-like regularity?
    >
    > --

    It is strange but I'm more interested in the intelligence of the ants
    (exploring the map, collecting food, exchanging maps, fighting :)) than
    the accuracy of the terrain (well for now) but thx for the input anyway !
     
    charly, Sep 11, 2003
    #4
  5. charly

    charly Guest

    Daniel Sjöblom wrote:

    > charly wrote:
    >
    >> Greetings,
    >>
    >> I am trying to make an app simulating ants running on the map.
    >>
    >> I am wondering about the best way to solve the "map" problem.
    >>
    >> so far, I' ve decided to create a vector which would hold each cell as
    >> a Cell object (x,y,type of ground, content)
    >>
    >> Is this ok ?

    >
    >
    > Depends on exactly what you want to do. If you only have ants or terrain
    > in a cell, I think you could get away with making Cell this simple:
    >
    > class Cell
    > {
    > boolean occupied; // Whether the cell has an ant in it or not
    > int terrain; // terrain type
    > }
    >
    > See more below.
    >
    >> When calling the draw method of the map, i would use an iterator to
    >> draw each cell, according to its content.
    >>
    >> When a ant is in a square, a copy of herself is put in the content of
    >> the square (so 2 ants can detect that they are in neighbouring cells
    >> because each time a ant moves in a square, she looks around her ) and
    >> removed from the previous square.

    >
    >
    > Well, assuming the map is rectangular, you need to check more than two
    > neighbors. A cell can have 8 adjacent cells. My suggestion would be to
    > make map something like this:
    >
    > class Map
    > {
    > // Two dimensional "map"
    > Cell[][] cells;
    >
    > public Map(int y, int x)
    > {
    > cells = new Cell[y][x];
    > fill(); // Some method to fill the map with cells
    > // Probably a random terrain type and unoccupied
    > }
    >
    > public void occupy(int cell_y, cell_x)
    > {
    > // Marks a cell occupied
    > }
    >
    > public void unoccupy(int cell_y, cell_x)
    > {
    > // Marks cell unoccupied
    > }
    > // More methods as you see fit
    > }
    >
    > Now, the ant move method becomes pretty simple. The ant needs to keep
    > track of its last position on the map. When the ant moves it first
    > inspects all adjacent cells in the array. If there are any free cells
    > unoccupy old position and occupy new, else the ant stays where it is.
    > (As a side note, you don't even need an ant object with this map, since
    > you can deduct the presence of an ant by the fact that a cell is
    > occupied. Hence, you could walk the map, and inspect adjacent cells of
    > any cell that is occupied.)

    There are other objects so I need to know what the grid content's is : a
    wall, food, water, another ant of the same house, dirt, leaves or little
    pieces of wood to build the ant's house but I'll use the map you gave me

    > When drawing the map, you simply draw the cells array in the map,
    > converting indexes to graphical coordinates. If the cell is occupied
    > draw an ant, else draw the terrain type.
    >

    -> I'll do that excepted that an ant does not cover all the cell si Draw
    first the terrain then I draw the ant over it

    Thank you for the tips !
     
    charly, Sep 11, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    stock prices off the net.

    Rob Love, Aug 18, 2003, in forum: Java
    Replies:
    3
    Views:
    356
    William Brogden
    Aug 19, 2003
  2. Marc Twain

    Best way to map Javabean to RDMS?

    Marc Twain, Nov 21, 2003, in forum: Java
    Replies:
    6
    Views:
    372
    Michael Borgwardt
    Nov 24, 2003
  3. savvy
    Replies:
    1
    Views:
    514
    Cowboy \(Gregory A. Beamer\)
    Jun 28, 2006
  4. parvathi
    Replies:
    0
    Views:
    453
    parvathi
    Feb 3, 2009
  5. Rui Maciel
    Replies:
    11
    Views:
    1,485
    Joshua Maurice
    Feb 12, 2012
Loading...

Share This Page