Write a JAVA program that generates a "random" (drunk) walk across a 10 X 10 array. Initially the array will contain all periods(".")
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 you 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)
i have have of the Program
import java.lang.Math.*;
class DrunkWalker
{
/*****************
* data items
******************/
private static char ch[][] = new char[10][10];
private static int randNSEW;
private static int stopCode = 0;
private static int row = 0;
private static int col = 0;
private static int alphaIndex = 0;
public static void main(String args[])
{
row = 4;
col = 6;
loadArray();
printArray();
while(stopCode != 1)
{
getRand();
switch(randNSEW)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default: break;
} // end switch
if(alphaIndex == 26)
{
stopCode = 1;
}
} // end while loop
} // end main
public static void loadArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
for(col=0; col<10; col++)
{
ch[row][col] = '.';
}
}
}// end loadArray
public static void printArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
System.out.println();
for(col=0; col<10; col++)
{
System.out.print(ch[row][col]);
}
}
System.out.println();
}// end printArray
public static void getRand()
{
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100= (int)(randomNum * 100);
randNSEW = x100 % 4;
}
}