Use JAVA The Drunkards Walk A drunkard in a grid of streets

Use JAVA. The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn’t move very far because the choices cancel each other out, but that is not the case. Represent locations as integer pairs (x, y). Implement the drunkard’s walk over 100 intersections, starting at (0, 0), and print the ending location.

Solution

import java.util.*;
java.utill.random;;
class Drunkard
{
int x, y;
Drunkard(int x, int y)
{
this.x = x;
this.y = y;
}
void moveNorth()
{
this.y -= 1;
}
void moveEast()
{
this.x += 1;
}
void report()
{
System.out.println(\"Hiccup: \" + x + \", \" + y);
}
}
class Four
{
public static void main(String[] args)
{
Random generator = new Random();
Drunkard drunkard = new Drunkard(100, 100);
int direction;
for (int i = 0; i < 100; i++)
{
direction = Math.abs(generator.nextInt()) % 4;
if (direction == 0)
{ // North
drunkard.moveNorth();
}
else if (direction == 1)
{ // East
drunkard.moveEast();
}
else if (direction == 2)
{ // South
System.out.println(\"Should move South.\");
}
else if (direction == 3)
{ // West
System.out.println(\"Should move West.\");
}
else
{
System.out.println(\"hufff Impossible\");
}
System.out.drunkard.report();
}
}
}

Use JAVA. The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly
Use JAVA. The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site