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();
  }
示例#2
0
  @Test
  public void testShoot() {
    Board board = facade.createBoard(35, 47);
    Energy enoughEnergy = new Energy(5000, unitOfPower.Ws);
    Energy enoughEnergy2 = new Energy(5000, unitOfPower.Ws);
    Energy notEnoughEnergy = new Energy(500, unitOfPower.Ws);
    Robot robotEnoughEnergy = new Robot(enoughEnergy, Orientation.RIGHT);
    Robot robotNotEnoughEnergy = new Robot(notEnoughEnergy, Orientation.LEFT);
    Robot robotShootAtBoardEdge = new Robot(enoughEnergy2, Orientation.LEFT);
    Position positionRobotEnoughEnergy = Position.newPosition(0, 0, board);
    Position positionRobotNotEnoughEnergy = Position.newPosition(20, 0, board);
    Position positionRobotShootAtBoardEdge = Position.newPosition(3, 2, board);

    robotEnoughEnergy.setPosition(positionRobotEnoughEnergy);
    robotNotEnoughEnergy.setPosition(positionRobotNotEnoughEnergy);
    robotShootAtBoardEdge.setPosition(positionRobotShootAtBoardEdge);

    assertTrue(positionRobotEnoughEnergy.equals(robotEnoughEnergy.getPosition()));

    try {
      facade.shoot(robotNotEnoughEnergy);
    } catch (AssertionError ae) {
      System.err.println("testShoot(facadetest): This assertionerror is to be expected.");
    }

    assertTrue(positionRobotEnoughEnergy.equals(robotEnoughEnergy.getPosition()));
    assertTrue(positionRobotNotEnoughEnergy.equals(robotNotEnoughEnergy.getPosition()));
    assertEquals(
        notEnoughEnergy.getAmountOfEnergy(), robotNotEnoughEnergy.getAmountOfEnergy(), epsilon);
    assertEquals(enoughEnergy.getAmountOfEnergy(), robotEnoughEnergy.getAmountOfEnergy(), epsilon);

    facade.shoot(robotEnoughEnergy);

    assertTrue(positionRobotEnoughEnergy.equals(robotEnoughEnergy.getPosition()));
    assertEquals(
        notEnoughEnergy.getAmountOfEnergy(), robotNotEnoughEnergy.getAmountOfEnergy(), epsilon);
    assertEquals(16000, robotNotEnoughEnergy.getMaxEnergy().getAmountOfEnergy(), epsilon);
    assertEquals(4000, robotEnoughEnergy.getAmountOfEnergy(), epsilon);

    facade.shoot(robotShootAtBoardEdge);

    assertEquals(4000, robotShootAtBoardEdge.getAmountOfEnergy(), epsilon);
  }