public static double runTrial(int memoryType, int probability) {
    HashMap<Point, EnergySource> energySources = new HashMap<>();
    int sources = 40, width = 200, height = 200, energy = 125;
    Point location = new Point(0, 0);
    Random generator = new Random();
    Robot<EnergySource> robot;
    EnergySource energySource;
    double amount;

    // Generate all the energy sources and the robot
    while (energySources.size() < sources) {
      location = new Point(generator.nextInt(width), generator.nextInt(height));
      if (isFarEnough(location, energySources)) {
        energySources.put(location, new EnergySource(location, energy));
      }
    }
    while (!isFarEnough(location, energySources)) {
      location = new Point(generator.nextInt(width), generator.nextInt(height));
    }
    robot = new Robot<>(location, memoryType);
    // Program loop
    while (robot.getEnergy() > 0) {
      if (robot.isCurious()) {
        robot.moveRandomly(width, height);
      } else {
        energySource = robot.retrieveEnergySource(probability);
        if (energySource != null) {
          robot.moveToLocation(energySource.getLocation());
        } else {
          robot.moveRandomly(width, height);
        }
        energySource = energySources.get(robot.getLocation());
        if (energySource != null) {
          if (robot.getMaxEnergy() - robot.getEnergy() < energySource.getEnergy()) {
            amount = robot.getMaxEnergy() - robot.getEnergy();
          } else {
            amount = energySource.getEnergy();
          }
          robot.increaseEnergy(amount);
          energySource.decreaseEnergy(amount);
          if (energySource.getEnergy() <= 0) {
            robot.forgetEnergySource(energySource);
            energySources.remove(robot.getLocation());
          }
        }
      }
      robot.detectEnergySources(energySources);
    }
    return robot.getTravelDistance();
  }
Esempio n. 2
0
  /**
   * This method implements the action move
   *
   * @param Stores the roboter who wants move
   */
  public void doAction(Robot robot) {
    Field downField = null;
    Field field = null;
    Field leftField = null;
    Field rightField = null;
    Field upField = null;
    Object item = null;

    robot.setEnergy(robot.getEnergy() - 1);

    if (robot.getDirection() == Robot.NORTH) {
      // bind field
      field = robot.getField();
      // bind upField
      upField = field.getUp();
      // link item
      item = upField.getItem();
      // if upField empty roboter moves on upField
      if (upField != null && item == null) {
        field.setItem(null);
        upField.setItem(robot);
        if (robot.getArena() != null)
          robot.getArena().drawLogMessage(robot.getName() + " decided to move up\n");
      }
      // if upField not empty, roboter hold position, call item.doAction()
      else if (upField != null && item instanceof Reactable)
        ((Reactable) upField.getItem()).doAction(robot);
      else {
        if (upField != null && item instanceof Robot) {
          try {
            int tmpEnergy = robot.getEnergy() - 10;
            if (tmpEnergy < 0) robot.setEnergy(0);
            else robot.setEnergy(robot.getEnergy() - 10);

            robot
                .getArena()
                .drawLogMessage(robot.getName() + " bumped " + ((Robot) item).getName() + "\n");
          } catch (ClassCastException e) {
          }
        }
      }

      return;
    } else {
      if (robot.getDirection() == Robot.EAST) {
        // bind field
        field = robot.getField();
        // bind rightField
        rightField = field.getRight();
        // link item
        item = rightField.getItem();
        // if rightField empty roboter moves on rightField
        if (rightField != null && item == null) {
          field.setItem(null);
          rightField.setItem(robot);
          if (robot.getArena() != null)
            robot.getArena().drawLogMessage(robot.getName() + " decided to move right\n");
        }
        // if rightField not empty, roboter hold position, call item.doAction()
        else if (rightField != null && item instanceof Reactable)
          ((Reactable) rightField.getItem()).doAction(robot);
        else {
          if (rightField != null && item instanceof Robot) {
            try {
              int tmpEnergy = robot.getEnergy() - 10;
              if (tmpEnergy < 0) robot.setEnergy(0);
              else robot.setEnergy(robot.getEnergy() - 10);

              robot
                  .getArena()
                  .drawLogMessage(robot.getName() + " bumped " + ((Robot) item).getName() + "\n");
            } catch (ClassCastException e) {
            }
          }
        }

        return;
      } else {
        if (robot.getDirection() == Robot.SOUTH) {
          // bind field
          field = robot.getField();
          // bind downField
          downField = field.getDown();
          // link item
          item = downField.getItem();
          // if downField empty roboter moves on downField
          if (downField != null && item == null) {
            field.setItem(null);
            downField.setItem(robot);
            if (robot.getArena() != null)
              robot.getArena().drawLogMessage(robot.getName() + " decided to move down\n");
          }
          // if downField not empty, roboter hold position, call item.doAction()
          else if (downField != null && item instanceof Reactable)
            ((Reactable) downField.getItem()).doAction(robot);
          else {
            if (downField != null && item instanceof Robot) {
              try {
                int tmpEnergy = robot.getEnergy() - 10;
                if (tmpEnergy < 0) robot.setEnergy(0);
                else robot.setEnergy(robot.getEnergy() - 10);

                robot
                    .getArena()
                    .drawLogMessage(robot.getName() + " bumped " + ((Robot) item).getName() + "\n");
              } catch (ClassCastException e) {
              }
            }
          }

          return;

        } else {
          if (robot.getDirection() == Robot.WEST) {
            // bind field
            field = robot.getField();
            // bind leftField
            leftField = field.getLeft();
            // link item
            item = leftField.getItem();
            // if leftField empty roboter moves on leftField
            if (leftField != null && item == null) {
              field.setItem(null);
              leftField.setItem(robot);
              if (robot.getArena() != null)
                robot.getArena().drawLogMessage(robot.getName() + " decided to move left\n");
            }
            // if leftField not empty, roboter hold position, call item.doAction()
            else if (leftField != null && item instanceof Reactable)
              ((Reactable) leftField.getItem()).doAction(robot);
            else {
              if (leftField != null && item instanceof Robot) {
                try {
                  int tmpEnergy = robot.getEnergy() - 10;
                  if (tmpEnergy < 0) robot.setEnergy(0);
                  else robot.setEnergy(robot.getEnergy() - 10);

                  robot
                      .getArena()
                      .drawLogMessage(
                          robot.getName() + " bumped " + ((Robot) item).getName() + "\n");
                } catch (ClassCastException e) {
                }
              }
            }

            return;

          } else {
            return;
          }
        }
      }
    }
  }