在 Java 中的网格上查找位置和移动方向

Finding location and moving directions on a grid in Java

我有一个测试仪 class 我已经准备好了,我只是不确定如何使用我的蓝图来完成这项工作。这是我正在尝试做的事情:

"You exist on an infinite grid of avenues and streets where positions are represented as a coupling of integers (avenue,street). You can use negatives. Now consider a drunkard that randomly picks one of four directions at an intersection and then stumbles to the next intersection doing the same and so on. Write a class Drunkard to simulate this way of being given the drunkard's starting point. Your Drunkard class should have as instance variables the drunkard's current avenue (x location) and current street (y location). Your class should have a method called step( ) that moves the drunkard to the next randomly chosen adjacent intersection. Your class should have another method called fastForward(int steps) that takes an integer as input (call it steps) and moves the drunkard steps intersections from his current location. Your class should have a method getLocation( ) that returns a String indicating the drunkard's current location. Finally your class should have a method called howFar( ) that reports the drunkards distance in blocks from where he started calculated using the Manhattan distance metric."

你有什么推荐?任何帮助都可以,谢谢。

这是我到目前为止的蓝图。

java.util.Random;

class Drunkard 
{
    private int avenue;
    private int street;

    Drunkard(int avenue, int street) {
        this.avenue = avenue;
        this.street = street;
    }

    Random rand = new Random();

    void moveUp(){
        this.street -= 1;
    }
    void moveDown(){
        this.street += 1;
    }
    void moveRight(){
        this.avenue += 1;
    }
    void moveLeft(){
        this.avenue -= 1;
    }
    void getLocation(){
        int avenue = parseInt("avenue");
        int street = parseInt("street");
        System.out.println("Location: " + avenue + ", " + street);
    }
    void fastForward(int steps)
    {
        for (int i=0; i < steps.length; i++}{
        }
    }
    void step(){
    }
    void howFar(){
        int distance = Math.abs(x1-x0) + Math.abs(y1-y0);
    }
}

而且我需要它对应这个测试仪class。

import java.util.Scanner;

public class DrunkardTester {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the starting avenue integer: ");
    int avenue = input.nextInt();
    System.out.println("Please enter the starting street integer: ");
    int street = input.nextInt();

    // instantiate
    Drunkard jeff = new Drunkard(avenue,street);

     //move 100 intersections
    jeff.fastForward(100);

    //find current location
    String location = jeff.getLocation();

    // get distance from start
    int distance = jeff.howFar();

    System.out.println("Current location: " + location);
    System.out.println("This is " + distance + " blocks from your origin.");

    }
}

所以,要跟踪酒鬼的起始路口,

你必须保持实例变量如下

private int startAvenue;
private int startStreet;

并修改构造函数以存储 startAvenue 和 StartStreet 值。

Drunkard(int avenue, int street) {
    this.startAvenue = avenue;
    this.startStreet = street;
}

现在酒鬼实例总是有它的起始位置。

现在我们可以专注于步法,

void step(){
    int nextMove = rand.nextInt(3);
    if(nextMove == 0)
      moveUp();
    else if(nextMove == 1)
      moveDown();
    else if(nextMove == 2)
      moveLeft();
    else
      moveRight();
}

在 fastForward() 中,只需在 for 循环中调用 step() 方法

 void fastForward(int steps)
{
    for (int i=0; i < steps.length; i++}{
      step();
    }
}

在 howFar() 方法中,

int howFar(){
    int distance = Math.abs(startAvenue-avenue) + Math.abs(startStreet-street);
}