
// RandomWalk.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

// https://introcs.cs.princeton.edu/java/15inout/RandomWalk.java.html

/* Simulates a 2D random walk and plots the trajectory.
   Works best if distance is a divisor of 600.

   Usage:
     > compile RandomWalk.java
     > run RandomWalk <distance>
   e.g.
     > run RandomWalk 20
     > run RandomWalk 50
*/


public class RandomWalk
{

  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: run RandomWalk <distance>");
      return;
    }
    int n = Integer.parseInt(args[0]);

    StdDraw.setScale(-n - 0.5, n + 0.5);
    StdDraw.clear(StdDraw.GRAY);
    StdDraw.enableDoubleBuffering();

    int x = 0, y = 0;
    int steps = 0;
    while (Math.abs(x) < n && Math.abs(y) < n) {
      StdDraw.setPenColor(StdDraw.WHITE);
      StdDraw.filledSquare(x, y, 0.45);
      double r = Math.random();
      if (r < 0.25) 
        x--;
      else if (r < 0.50) 
        x++;
      else if (r < 0.75) 
        y--;
      else if (r < 1.00)
        y++;

      steps++;
      StdDraw.setPenColor(StdDraw.BLUE);
      StdDraw.filledSquare(x, y, 0.45);

      StdDraw.show();
      StdDraw.pause(10);
    }
    System.out.println("Total steps = " + steps);
  } /* main */


}  // end of RandomWalk class

