1

My program uses StdDraw to create an N-by-N grid. I'm supposed to accept N and T in the command line (N is the number of lines in the grid, T is the number of times I can try to escape the grid in a random walk). I keep getting an error that says:

Exception in thread "main" java.lang.NegativeArraySizeException
at RandomWalk.main(RandomWalk.java:28)

My program looks like this:

import java.util.Random;
    public class RandomWalk {
        public static void main(String[] args) {
            int N = Integer.parseInt(args[0]); 
            int T = Integer.parseInt(args[1]); 
            int tempN = N;
            int DEcount = 0; //Dead End Count
            int x0 = N/2;
            int y0 = N/2;
            int x1 = x0;
            int y1 = y0;
            StdDraw.setXscale(0.0, N);
            StdDraw.setYscale(0.0, N);
            StdDraw.setPenColor(StdDraw.GRAY);
            StdDraw.setPenRadius(0.002);
            while (N >= 0) {
                StdDraw.line(tempN, N, 0, N); 
                N--;
            }
           StdDraw.setPenColor(StdDraw.GRAY);
           StdDraw.setPenRadius(0.002);
           N = tempN;
           while (N >= 0) {
               StdDraw.line(N, tempN, N, 0); 
               N--;
           }
           for (int i = 0; i < T; i++) {
               boolean[][] check = new boolean[N][N];
               while (x1 > 0 && x1 < N-1 && y1 > 0 && y1 < N-1) {
                   //check for dead ends and make a random move
                   check[x1][y1] = true;
                   if (check[x1-1][y1] && check[x1+1][y1] && check[x1][y1-1] && check[x1][y1+1]) {
                DEcount++;
                break;
            }
            double rand = Math.random();
            if      (rand < 0.25) { if (!check[x1+1][y1]) x1++;}
            else if (rand < 0.50) { if (!check[x1-1][y1]) x1--;}
            else if (rand < 0.75) { if (!check[x1][y1+1]) y1++;}
            else if (rand < 1.00) { if (!check[x1][y1-1]) y1--;}

            StdDraw.setPenColor(StdDraw.RED);
            StdDraw.setPenRadius(0.01);
            StdDraw.line(x0, y0, x1, y1);
            x0 = x1;
            y0 = y1;
            }
         }
    }
}

Additionally, what I'm supposed to be printing on the grid (the red lines that represent the random walk) aren't printing. The grid itself does print, though.

Can anyone help me figure out what I'm doing wrong?

Help is appreciated.

Bianca Fuller
  • 39
  • 1
  • 9

2 Answers2

1

Consider this code fragment:

       while (N >= 0) {
           StdDraw.line(N, tempN, N, 0); 
           N--;
       }
       for (int i = 0; i < T; i++) {
           boolean[][] check = new boolean[N][N];

At the end of the while loop, N is -1, and then you use it as an array size.

You may have meant to use tempN which seems to preserve N. I suggest more descriptive names to avoid this sort of problem.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0
  • issue 1: Inside your while loop you decrement variable N until its -1. its next use is as an array size specifier when allocating check. I guess you forgot another assignment N = tempN immediately before the for loop.
collapsar
  • 17,010
  • 4
  • 35
  • 61