Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > best way to stock a map

Reply
Thread Tools

best way to stock a map

 
 
charly
Guest
Posts: n/a
 
      09-10-2003
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 !

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-10-2003
On Wed, 10 Sep 2003 13:57:49 +0200, charly <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      09-10-2003
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

 
Reply With Quote
 
charly
Guest
Posts: n/a
 
      09-11-2003

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

 
Reply With Quote
 
charly
Guest
Posts: n/a
 
      09-11-2003
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 !

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map: best way to get biggest key? Rui Maciel C++ 11 02-12-2012 09:25 PM
Open the stock Broking Franchisee of karvy Stock broking parvathi C++ 0 02-03-2009 06:48 AM
How to add extra field(In stock/Out of stock) to a DropDownList ? savvy ASP .Net 1 06-28-2006 12:28 PM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
Best way to map Javabean to RDMS? Marc Twain Java 6 11-24-2003 04:29 PM



Advertisments