Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Need help making ant wander in all directions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help making ant wander in all directions" Watch "Need help making ant wander in all directions" New topic

Need help making ant wander in all directions

Julie Mendez

Joined: Oct 04, 2004
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.

Thread.sleep( 100 );
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.

<b><i>To err is human but to really mess up the things you need a computer </b></i>
Julie Mendez

Joined: Oct 04, 2004
Posts: 7
Never mind about this topic. I have found out how to make it move.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I notice that you and Holly Leery are working on the same problem. Are you in the same class?

Just curious.

Mike Gershman
SCJP 1.4, SCWCD in process
Julie Mendez

Joined: Oct 04, 2004
Posts: 7
I don't know. If she know's who Sam Maverick is probably so.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Need help making ant wander in all directions
Similar Threads
Switch Statement
please help me
Random Method
Help w/ controlling my dots.