programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Need help making ant wander in all directions

Greenhorn
Posts: 7
i have all it figured out, but my ant wonders only to one side of my grid, the Southwest side. Can someone help me?

Simulate the wandering of an ant in a street square grid. Draw a grid of 10 streets horizontally and 10 streets vertically. Represent the simulated ant by a dot, placed in the middle of the grid to start. For 100 times, have the simulated ant randomly pick a direction (east, west, north, south), move one block in the chosen direction, and redraw a new dot. After the iterations, display the effective distance the ant has covered (i.e., distance between the starting location and the ending location in number of blocks). (One might expect that on average the ant might not get anywhere because the moves to different directions cancel one another out in the long run, but in fact it can be shown mathematically that with probability 1 the ant eventually moves outside any finite region.)
You can draw the street grid starting at (10,10) with each square in the grid 30 pixels wide. Here is the code to draw the grid:

for ( int i = 10; i < 310; i += 30 )
for ( int j = 10; j < 310; j += 30 )
g.drawRect( i, j, 30, 30 );
Use int variables x, y for keeping track of the location of the ant and simulate the ant by drawing an oval 6 pixels wide at that location, as follows:

g.fillOval( x-3, y-3, 6, 6 );
Start with x = 160, y = 160 (i.e., the center) and simulate the random moves by generating a random no. between 0 and 3 for each direction, and redraw the oval 30 pixels away (1 block) in the right direction. So, for e..g, to move the ant north you would change only the y coordinate by subtracting 30, to move east you would add 30 to the x coordinate, etc. Make sure that the location of the ant never goes beyond the grid (i.e., x and y should never go below 10 or above 310).
To calculate the effective distance travelled by the ant, sum the absolute difference between the beginning and ending coordinates and divide by 30 to get the distance in blocks, as shown below:

distance = ( Math.abs( x - 160 ) + Math.abs( y - 160 ) ) / 30;
When you execute the program, the 100 iterations would get over very fast. In order to get a better feel for the movement of the simulated ant, you can put the program to "sleep" inside the loop for a set number of millisec. To put the program to sleep for 100 millisec. insert the following code inside your loop.

try
{
}
catch (InterruptedException ie)
{
}
Note that you will be writing an applet and all your code should be inside the paint method of the applet. A sample copy of the .class file is given below. You will need to create your own html file to execute the RandomWalk.class file using the appletviewer command.

Julie Mendez
Greenhorn
Posts: 7

Ranch Hand
Posts: 1272
I notice that you and Holly Leery are working on the same problem. Are you in the same class?

Just curious.

Julie Mendez
Greenhorn
Posts: 7
I don't know. If she know's who Sam Maverick is probably so.

 Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS