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;
    }
}

Recommended Answers

All 9 Replies

Code tags and formatting/indentation please.

[code=JAVA] // paste code here

[/code]

Your code is below, but it's still hard to read since there is no indentation. Second, what is the question?

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;
}
}

VernonDozier well i have post description first and after _________dis line i have code of program
......plz help me to complete this program if any of you know how to do it output should look like dis when i compile
walk completed
A............
BCD.........
.FG..........
HG..........
I.............
J.........Z..
K..RSTUVY
LMPQ...WX..
.NO.............
..................

walk blocked
...................
...................
.............A...
..........BC...
............DKJ
...........ELI
..........FGH
...............
................
...............

You have a project specification, but not a question. Did you write the code or was it part of the specification? You need to repost please using indentation and code tags, and explain where exactly you are stuck (i.e. a certain line doesn't compile, it has a run-time error on a certain line, you're confused about certain aspects of the program, etc.). Otherwise we have no idea what the problem is.

plz help me to complete this program if any of you know how to do it

We would not complete the program for you irrelevant of the fact whether we know it or not and as VD has said tell us what point in the program your stuck at, ask specific questions to which we might be willing to answer.

i need to know how to start not the coding program description is all there explain Array

What you just said makes no sense. It looks like 3 sentences jumbled into one. In English, please?

> explain Array

Which array you want us to explain ?

sfdstrg

hhytrujytjttrtry

> explain Array

Which array you want us to explain ?

how to explain this Array

commented: Eh? Did you absolutely have to puke in between ? -1
commented: What is this garbage you are posting? -4
String [] array = new String[5]; // array is not null but array[0], ... are null
//array variable is an array and should be treated like one.

// instatiating the elements of the array:
array[0] = "aa";
array[1] = "bb";

System.out.println("Array: "+array);
for (int i=0;i<array.length;i++) {
   System.out.println("Elements: "+array[i]);
}

//array[0] is a String and should be treated like a String

When you are creating any array this is one of the ways:
SomeObject [] array = new SomeObject[N];

With this
array[0] = new SomeObject(); you are creating a SomeObject instance. Remember array[0] IS a SomeObject.
array is an array.

Also if you still don't understand run my example.

And these things that you wrote will get you in trouble:

sfdstrg
hhytrujytjttrtry

If you don't understand something, repost with better explanation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.