best way to stock a map

C

charly

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

Roedy Green

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

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

charly said:
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.
 
C

charly

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

charly

Daniel said:
charly said:
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 !
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top