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

Drunk Walker Write a Java program that generates a ‘random’ (drunk) walk across

ID: 3852926 • Letter: D

Question

Drunk Walker

Write a Java program that generates a ‘random’ (drunk) walk across a 10 X 10 array. Initially each cell of the array should contain a period (‘.’)

The program must randomly ‘walk’ from element to element – always going up, down, right, or left (no diagonal) by one element.

The elements ‘visited’ will be labeled by the letters ‘A’ through ‘Z’ in the order visited.

To determine your direction to move, use the random() method to generate a random integer

                           double rand = random();

Convert the random integer to a number 0~3 (hint: use the mod   %)

If the number generated is a:

                                    0 – move up (north)

                           1 – move right (east)

                           2 – move down (south)

                           3 – move left (west)

If the spot you are trying to move to is already occupied by a letter (has been visited), try the next spot. In other words

         Random number is a 0 – check N, E, S, W

                                             1 – check E, S, W, N

                                             2 – check S, W, N, E

                                             3 – check W, N, E, S

Keep within the array (check array bounds before moving)

If you hit a point where you cannot move, stop and print the array

If you get to the letter ‘Z’, stop and print the array

Ask for the user to enter the initial row and column on which to start.

Validate that row and column entered are valid (within the array)

Output – if you stopped because you are blocked, use a heading of “Arrested – in jail”. If you make it to the letter ‘Z’, use a heading of “Made it home”. Then print the array

Explanation / Answer

walker.java

import java.util.Random;
import java.util.Scanner;
public class walker
{
   public static void main(String[] args)
   {
       char[][] board = new char[10][10];
       for (int i = 0;i<10 ;i++ )
       {
           for (int j = 0;j<10 ;j++)
           {
                   board[i][j] = '.';  
               }  
       }
       int i = 0;
       int x,y = 0;

Scanner scanner = new Scanner(System.in);
while(true)
{
   System.out.println("Enter Starting x coordinate: ");
   x = scanner.nextInt();
   if(x >=0 || x <=9)
   {
       break;
   }
   else
   {
   System.out.println("Invalid position, Enter 0 <= x <= 9");        
   }
   }

while(true)
{
   System.out.println("Enter Starting y coordinate: ");
   y = scanner.nextInt();
   if(y >=0 || y <=9)
   {
       break;
   }
   else
   {
   System.out.println("Invalid position, Enter 0 <= y <= 9");        
   }
   }
   int ascii = 65;
       while(true)
       {
           int rand = (int )(Math.random() * 4 );
           if(rand == 0)
           {
               y = y-1;
               if(y<0 || ascii == 91)
               {
                   break;
               }
               else
               {
                   if(board[x][y] != '.')
                   {
                       continue;
                   }
                   else
                   {
                       board[x][y] = (char)ascii;
                       ascii++;
                   }
                }
           }
           if(rand == 1)
           {
               x = x+1;
               if(x > 9 || ascii == 91)
               {
                   break;                  
               }
               else
               {
                   if(board[x][y] != '.')
                   {
                       continue;
                   }
                   else
                   {
                       board[x][y] = (char)ascii;
                       ascii++;
                   }
                }
           }
           if(rand == 2)
           {
               y = y+1;
               if(y > 9 || ascii == 91)
               {
                   break;                  
               }
               else
               {
                   if(board[x][y] != '.')
                   {
                       continue;
                   }
                   else
                   {
                       board[x][y] = (char)ascii;
                       ascii++;
                   }
                }
           }
           if(rand == 3)
           {
               x = x-1;
               if(x < 0 || ascii == 91)
               {
                   break;                  
               }
               else
               {
                   if(board[x][y] != '.')
                   {
                       continue;
                   }
                   else
                   {
                       board[x][y] = (char)ascii;
                       ascii++;
                   }
                }
           }
          i++;
   }
   if(ascii == 91)
   {
System.out.println("Reached home");        
   }
   else
   {
System.out.println("Arrested – in jail");                
   }
       for (i = 0;i<10 ;i++ )
       {
           for (int j = 0;j<10 ;j++)
           {
       System.out.print(board[i][j] + " ");        
               }  
       System.out.println("");        
       }

   }
}

Sample Output:

Enter Starting x coordinate:
4
Enter Starting y coordinate:
5
Arrested – in jail
. . . . . F . . . .
. . . . . E D . . .
. . . . . I C G . .
. . . . . A B H . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Enter Starting x coordinate:
4
Enter Starting y coordinate:
5
Reached home
. . . . . . . . . .
. . . . . . . . . .
. . . . . . F E . .
. . . . . . G D . .
. . . O . . A C . .
Y Z P N M L B H . .
X V Q . . K J I . .
W U R . . . . . . .
. T S . . . . . . .
. . . . . . . . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote