Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simulate how many steps its take a random walker starting at the center of an 10

ID: 3889891 • Letter: S

Question

Simulate how many steps its take a random walker starting at the center of an 10x10 grid to visit every cell of the grid. If the walker tries to go outside of the grid then it doesn't move in that step.

Write a java program which simulates th steps of the random walker, and keeps hold about the grid with 2D boolean array and write out the steps when the random walker have walked all the cells.

-----------------------------------------------------------------------------------------------

I have managed to come up with this code:

-----------------------------------------------------------------------------------------------

public class SlembiEind2 {

public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int[] x = new int[n];
int[] y = new int[n];
int steps = 0;
int ctv = n*n;
boolean[][] fylki = new boolean[n][n];
  
StdDraw.setXscale(0,n-1);
StdDraw.setYscale(0,n-1);
StdDraw.clear(StdDraw.GRAY);
StdDraw.enableDoubleBuffering();
  
for(int i = 0; i < n; i++) {
x[i] = n/2;
y[i] = n/2;
StdDraw.filledSquare(x[i], y[i], 0.45);
}
fylki[n/2][n/2] = true;
ctv--;

while (ctv > 0) {

double r = Math.random();
for(int i = 0; i < n; i++) {
  
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledSquare(x[i], y[i], 0.45);
StdDraw.show();
StdDraw.pause(100);
if(r < 0.25){
x[i]--;
} else if(r < 0.50){
x[i]++;
} else if(r < 0.75){
y[i]--;
} else if(r < 1.00){
y[i]++;
}

if (x[i] < n && y[i] < n && x[i] >= 0 && y[i] >= 0 && !fylki[x[i]][y[i]]) {
ctv--;
fylki[x[i]][y[i]] = true;   
  
}
steps++;
StdDraw.setPenColor(StdDraw.WHITE);
System.out.println(ctv + " " + steps);
}   
}
StdOut.println("Total steps = " + steps);
}
}

-------------------------------------------------------------------------------------

But I have two problems.

The walker seems to walk outside of the array/grid.

And it keeps counting the steps when it walks outside of the grid.

Anyway you can help me fix the code so it fix these problems? The code should not count if the walke trys to go out of the grid!

-----------------------------------------------------------------------------

ctv is the cells left to visit before the walker fill the grid!

Explanation / Answer

import java.io.*;
import java.util.*;


public class DemoWalker {
  
    public static boolean check(char data[][]){
        boolean found = true;
        int count = 0;
        for (int i = 0; i<10; i++){
           for(int j= 0; j<10; j++){
              if (data[i][j] != 'V')
                 found = false;
              else
                 count++;
           }
        }
        System.out.println(count);
        return found;
    }

    public static void main(String[] args){

        Random rand = new Random();
        char[][] grid = new char[10][10];
        int x,y;
        int opt;

        for (int i = 0; i<10; i++){
            for(int j = 0; j<10; j++){
               grid[i][j] = '.';
            }
        }
        grid[5][5] = 'V';
        x = 5;
        y = 5;
        int count = 0;
        while (!check(grid)){
           
       
               opt = rand.nextInt(4);
               if (opt == 0){
                  if ((x-1) > 0)
                      x = x-1;
                   
               }
               if (opt == 1){
                  if ((x+1) < 10)
                    x = x+1;
               }
               if (opt == 2){
                 if ((y-1) > 0)
                    y = y-1;
               }
               if (opt == 3){
                  if ((y+1) < 10)
                    y = y+1;
               }
           
            grid[x][y] = 'V';
            System.out.println("Moved to :" + x + " " +y);
        }
        count++;
        System.out.println("The number of steps : " + count);               

    }
}