Random walk. Simulate the wandering of an intoxicated person in a square street grid. Draw a grid of 20 streets horizontally and 20 streets vertically. Represent the simulated drunkard by a dot, placed in the middle of the grid to start. For 100 times, have the simulated drunkard randomly pick a direction (east, west, north, south), move one block in the chosen direction, and draw the dot. (One might expect that on average the person 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 with probability 1 that the person eventually moves outside any finite region. Use classes for the grid and the drunkard.

Here is a sample program output:

.

Complete the following files:

Drunkard.java

public class Drunkard { /** Creates a Drunkard object representing an intoxicated person. @param initialRow the initial grid row @param initialColumn the initial grid column */ public Drunkard(int initialRow, int initialColumn) { . . . } /** Makes the drunkard move randomly into one of four directions. */ public void move() { . . . } /** Gets the current row of the drunkard. */ public int getRow() { . . . } /** Gets the current row of the drunkard. */ public int getColumn() { . . . } }

DrunkardComponent.java

Grid.java

Use the following files:

DrunkardTester.java

public class DrunkardTester
{
   public static void main(String[] args)
   {      
      Drunkard d = new Drunkard(10, 10);
      d.move();
      System.out.println(d.getRow() != 10 && d.getColumn() == 10
         || d.getColumn() != 10 && d.getRow() == 10);
      System.out.println("Expected: true");
   }
}

DrunkardViewer.java

import javax.swing.*;

/**
   This class tests the Drunkard and Grid classes.
*/
public class DrunkardViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final int FRAME_WIDTH = 270;
      final int FRAME_HEIGHT = 300;

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("DrunkardViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      DrunkardComponent component = new DrunkardComponent();
      frame.add(component);

      frame.setVisible(true);
   }
}