javagridawtrobot

How to direct a robot


I am writing a Java program that allows a user to send messages to a robot to tell it where to go on a 5x5 grid. E.g, if I send it 'FRFFLF', this means go forward, run right 90 degrees, forward twice, turn left 90 degrees and then go forward. The program then returns the robot's new position on the grid.

I'm not seeking a solution, just some guidelines on my approach. Are there already design patterns available for such a program (so I'm not reinventing the wheel) or is it possible to write from scratch?

The bottom of the grid is (0,0 and the top would be (4,4).

I'm thinking that the Robots position could be defined as a Java Point class and the grid would be a 2D array?

Would something like the following be a start?

    public Point forward(Point p) {
    p.x +=1;
    return p;
}

Has anyone every tried to write such a program? Is it possible?

EDIT: I've thought about the following: Firstly I need the direction of the robot and its current position. From there I will be encapsulating the grid creating a few rules around boundaries. Basically I will update the position of the robot interpreting the commands passed to it and updating the position based of the robot. Obviously the robot will need to only move within the bounds of the grid.


Solution

  • Firstly, I would decide what x and y means. In this case, I'm going to say that x gets larger when you move "east", and y gets larger when you move "south". This is usually how it works in computer graphics - the 0,0 point is usually in the top left corner, as is shown here: http://graphics.comsci.us/notes/coord.html

    So, what I would do is have two classes, Robot and Grid.

    The Grid class should have these properties:

    And these methods:

    The Robot class should have these properties:

    It should also have these methods:

    It does not matter how you store the orientation - 0,1,2,3 or "north", "east", "south", "west", or "up", "right", "down", "left", or maybe even 0, 90, 180, 270, 360 (degrees), or radians. What is important is that you're consistent in your use of the orientation.

    Now, inside left() and right(), you change the orientation of your robot. So you could do something like this:

    public void left() {
      switch (orientation) {
        case "north": orientation = "west"; break;
        case "east": orientation = "north"; break;
        ...
      }
    }
    

    You would change the directions in order, and you would do them in reverse order in the right() function.

    Inside forward() is where you interact with the grid object:

    public void forward() {
      switch (orientation) {
        case "north":
          if (grid.isValid(position.x, position.y+1)) {
            position.y += 1;
          } else {
            System.out.println("Can't go there!");
          }
          break;
        ...
    }
    

    What is interesting to notice, is that the Grid class can store any shape. As long as you check that the place you want to go to is valid first, the robot doesn't care what the grid looks like. You can pluck your robot off this grid, and put it on another grid, and it would still work the same way.

    The next bit would be something that reads commands that you send to it, I imagine. At that point, maybe you'll just read the string one character at a time, and execute the command that it specifies.