2010-11-20 13 views
1

Ich mache einen Roboter, der einen Flasher in einem Labyrinth fischt und ihn in seine ursprüngliche Position zurückbringt. Wenn ich meinen Code teste, wird der Roboter den Blinker finden, aber dann stecken bleiben und nicht in seine Ausgangsposition zurückkehren. Ich habe andere Methoden geschrieben, um das Programm zu vereinfachen. Sie enthalten Methoden, die prüfen, ob die nächste Kreuzung vom Roboter besucht wurde, eine andere Methode, die die nächste Straße berechnet, und eine, die den Roboter dreht. Hier ist der vollständige Code:Roboter wird nicht zu vorherigen Spots zurückkehren

import becker.robots.Robot; 
import becker.robots.City; 
import becker.robots.Direction; 
import becker.robots.Intersection; 
import java.util.ArrayList; 

/* This program has a robot search a maze for a flasher, picks the flasher up, and 
returns back to it's starting position. If no flasher is found, it will return back 
to the starting position as well. 
*/ 

public class RobotUtils{ 
    Robot meow; 
    /* This method says what avenue the robot will be on if it decides to move 
    one intersection in the direction it's facing 
    */ 
    public static int calculateNextAvenue(Robot meow){ 
     int avenue = meow.getAvenue(); 
     Direction direction = meow.getDirection(); 
     //Changes the value of the avenue the robot is on when facing west 
     if (direction == Direction.WEST){ 
      return avenue - 1; 
     } 
     //Changes the value of the avenue the robot is on when facing east 
     if (direction == Direction.EAST){ 
      return avenue + 1; 
     } 
     //If facing north or south, doesn't change the value 
     else{ 
      return avenue; 
     } 
    } 
    /* This method says what street the robot will be on if it decides to move 
    one intersection in the direction it's facing 
    */ 
    public static int calculateNextStreet(Robot meow){ 
     int street = meow.getStreet(); 
     Direction direction = meow.getDirection(); 
     //Changes the value of the avenue the robot is on when facing south 
     if (direction == Direction.SOUTH){ 
      street = street + 1; 
     } 
     //Changes the value of the avenue the robot is on when facing north 
     if (direction == Direction.NORTH){ 
      street = street - 1; 
     } 
     //If facing east or west, doesn't change the value 
     return street; 
    } 
    //This method turns the robot to face a certain specified direction 
    public static void turnRobot(Robot meow, Direction face){ 
     Direction direction = meow.getDirection(); 
     while (direction != face){ 
      meow.turnLeft(); 
      direction = meow.getDirection(); 
     } 
    } 
    //This method checks a list to see if a certain intersection is in the list 
    public static boolean isVisited(ArrayList<Intersection> search , int 
     street, int avenue){ 
     Intersection inter; 
     boolean found = false; 
     int i = 0; 
     while (found == false && i < search.size()){ 
      inter = search.get(i); 
      if (inter.getAvenue()== avenue && 
       inter.getStreet() == street){ 
       found = true; 
      } 
      i++; 
     } 
     return found; 
    } 
    //This method makes the robot search a maze, find a flasher if there is 
    //one, pick the flasher up, and return back to its starting position. 
    public static void retrieveLight(Robot meow){ 
     ArrayList<Direction> path; 
     path = new ArrayList<Direction>(); 
     ArrayList<Intersection> intersections; 
     intersections = new ArrayList<Intersection>(); 
     boolean searched = false; 
     boolean flasher = false; 
     int street = meow.getStreet(); 
     int avenue = meow.getAvenue(); 
     Intersection current = meow.getIntersection(); 

     //Adds the current intersection the robot is on to a list that 
     //keeps track of where the robot has been 
     while (searched == false && flasher == false){ 
      Direction d = meow.getDirection(); 
      if (isVisited(intersections, street, avenue) == false){ 
       intersections.add(current); 
      } 
      //Robot picks flasher up 
      if (meow.canPickThing()){ 
       flasher = true; 
       meow.pickThing(); 
      } 
      //Makes the robot move towards intersections it hasn't visited yet 
      //to search the maze to find the flasher 
      else { 
    //Robot moves if an adjacent intersection hasn't been visited yet 
    if (!isVisited(intersections, calculateNextStreet(meow), 
    calculateNextAvenue(meow)) && meow.frontIsClear()){ 
      path.add(d); 
      turnRobot(meow, d); 
      meow.move();    
    }   
    //If path is blocked or intersection has been visited, 
    //robot turns to look for new direction to move in 
     if (isVisited(intersections, calculateNextStreet(meow), 
     calculateNextAvenue(meow)) || !meow.frontIsClear()){ 
         meow.turnLeft(); 
        } 
    //If the robot has searched the entire maze without 
    //finding flasher, exits the loop 
     else if (path.isEmpty()){ 
     searched = true; 
    } 
    //If all intersections around robot have been visited/are blocked, 
    //robot back tracks to find a new intersection to visit 
     else { 
     int last = path.lastIndexOf(d); 
     path.remove(last); 
     turnRobot(meow, d.opposite()); 
     meow.move(); 
      }   
      } 
     } 
     //Have robot go back to start by backtracking all intersections it 
     //has visited 
      int i = path.size(); 
      while (i >= 0 && isVisited(intersections, 
      calculateNextStreet(meow),calculateNextAvenue(meow))){ 
       Direction d = meow.getDirection(); 
       int last = path.lastIndexOf(d); 
       path.remove(last); 
       turnRobot(meow, d.opposite()); 
       meow.move(); 
       i--; 
       } 
    } 
} 

Ich denke, das Hauptproblem mit dem Code in diesem Abschnitt ist:

//Makes the robot move towards intersections it hasn't visited 
//to search the maze to find the flasher 
else { 
//Robot moves if an adjacent intersection hasn't been visited yet 
if (!isVisited(intersections, calculateNextStreet(meow), 
    calculateNextAvenue(meow)) && meow.frontIsClear()){ 
    path.add(d); 
    turnRobot(meow, d); 
    meow.move();    
}   
//If path is blocked or intersection has been visited, 
//robot turns to look for new direction to move in 
    if (isVisited(intersections, calculateNextStreet(meow), 
    calculateNextAvenue(meow)) || !meow.frontIsClear()){ 
     meow.turnLeft(); 
    } 
//If the robot has searched the entire maze without 
//finding flasher, exits the loop 
    else if (path.isEmpty()){ 
     searched = true 
     } 
//If all intersections around robot have been visited/are blocked, 
//robot back tracks to find a new intersection to visit 
    else { 
    int last = path.lastIndexOf(d); 
    path.remove(last); 
    turnRobot(meow, d.opposite()); 
    meow.move(); 
    }   
       } 
      } 
//Have robot go back to start by backtracking all intersections it 
//has visited 
       int i = path.size(); 
       while (i >= 0 && isVisited(intersections, 
       calculateNextStreet(meow),calculateNextAvenue(meow))){ 
        Direction d = meow.getDirection(); 
        int last = path.lastIndexOf(d); 
        path.remove(last); 
        turnRobot(meow, d.opposite()); 
        meow.move(); 
        i--; 
        } 
     } 
    } 

Wenn Sie mir helfen könnte ich es sehr zu schätzen würde.

BEARBEITEN Mein Roboter stürzt jetzt in die Wand, sobald es den Blinker gefunden hat, irgendwelche Gedanken? Wie soll ich die letzte While-Schleife beenden?

+0

deedex, Ihr Code erwähnt, dass der Roboter zum Start zurückkehren sollte, wenn der Flasher nicht gefunden wird. Funktioniert das? –

+0

Was passiert ist, dass der Roboter vorwärts geht und dann zwischen zwei Kreuzungen hin und her geht (nicht an der Startposition) – deedex11

Antwort

1

Sie sollten einen Stapel aller Punkte erstellen, die Sie im Labyrinth durchquert haben. Sobald Sie Ihr Ziel erreicht haben, "knallen" Sie jeden Punkt aus dem Stapel. Dadurch können Sie Ihre Schritte effektiv nachvollziehen und können ganz einfach in Ihren aktuellen Code einprogrammieren. Es ist viel einfacher als mit einer ArrayList ...

+0

Ich bin neu in der Java-Programmierung, also bin ich mir nicht sicher, wie Stacks funktionieren. Es wurde auch angegeben, dass ich ArrayLists verwenden muss, um dieses Programm abzuschließen. – deedex11

+0

Die Reihenfolge einer ArrayList ist vorhersehbar, wenn auf sie mit nur einem Thread zugegriffen wird. Sie könnten entweder ein Array aus der ArrayList exportieren und tun, wie Trevor sagt, oder einen Iterator von der ArrayList holen und rückwärts darüber iterieren. –

+0

Ja, iteriere rückwärts durch die ArrayList mit einer Schleife. Ich empfehle, die Startposition zu markieren, bevor der Roboter sich bewegt. Wenn Sie zurückfahren möchten, verwenden Sie den Startpunkt als Stoppkriterium für Ihre Schleife. –

Verwandte Themen