private Action moveTowardsDirection(Robot robot, Direction direction) {

    Action next = null;

    Orientation currentOrientation = robot.getCurrentOrientation();
    Orientation orientationOnDirection = orientationOnDirection(currentOrientation, direction);
    int statusOnOrientation = robotSurroundingStatus(robot, orientationOnDirection);

    if (statusOnOrientation <= 0) {
      next = directionToAction(direction);
      //			if(next.equals(Action.TURN_LEFT) ||
      //					next.equals(Action.TURN_RIGHT)){
      //
      //				preTentativeTurn = next;
      //			}else{
      //				preTentativeTurn = null;
      //			}

      if (statusOnOrientation == -1) {
        assert (next.equals(Action.TURN_LEFT) || next.equals(Action.TURN_RIGHT));
        preTentativeTurn = next;
      } else {
        preTentativeTurn = null;
      }
    }
    return next;
  }
示例#2
0
 @Override
 public String toString() {
   if (getNumChoices() == 0) {
     return "No choices.";
   }
   StringBuilder sb = new StringBuilder();
   int i = 1;
   for (Action action : actions) {
     if (getChoiceSelected() != null && action.equals(getChoiceSelected())) {
       sb.append("Selected Action: ");
     }
     sb.append(String.format("(#%d) ", i++));
     sb.append(action);
   }
   return sb.toString();
 }
  public void testRandomized() {
    List<Action> actions =
        Arrays.asList(
            new NextAction(),
            new HasNextAction(),
            new PreviousAction(),
            new HasPreviousAction(),
            new NextIndexAction(),
            new PreviousIndexAction(),
            new RemoveAction(),
            new AddAction(),
            new SetAction());

    List<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
    Filter<Integer> filter =
        new Filter<Integer>() {
          public boolean accept(Integer element) {
            return element % 2 == 0;
          }
        };
    List<Integer> filteredList = copyFiltered(list, filter);
    ListIterator<Integer> filteringIterator =
        new FilteringListIterator<Integer>(list.listIterator(), filter);
    ListIterator<Integer> referenceIterator = filteredList.listIterator();

    Random random = new Random();
    for (int i = 0; i < 20000; i++) {
      Action next = actions.get(random.nextInt(actions.size()));

      Object o1 = next.execute(filteringIterator);
      Object o2 = next.execute(referenceIterator);

      if (!Action.equals(o1, o2) || !copyFiltered(list, filter).equals(filteredList)) {
        fail(
            copyFiltered(list, filter)
                + "\n"
                + filteredList
                + "\n"
                + next.getClass().getSimpleName()
                + "\n"
                + o1
                + "\n"
                + o2
                + "\n");
      }
    }
  }
 private static Action reverseTurn(Action turn) {
   if (turn.equals(Action.TURN_LEFT)) return Action.TURN_RIGHT;
   if (turn.equals(Action.TURN_RIGHT)) return Action.TURN_LEFT;
   assert (false) : "Should not reach here,only turns are allowed";
   return null;
 }