Exemplo n.º 1
0
 // Set if the application is in editing mode based on the state of the shift key
 public void setEditingMode() {
   try {
     Robot robot = new Robot();
     robot.keyRelease(KeyEvent.VK_SHIFT);
   } catch (AWTException e) {
     e.printStackTrace();
   }
 }
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    while (true) {

      int N = sc.nextInt(), M = sc.nextInt(), S = sc.nextInt();
      sc.nextLine();
      if (N + M + S == 0) {
        break;
      }

      char[][] map = new char[N][M];
      boolean robotFound = false;
      Robot robot = null;
      for (int i = 0; i < N; i++) {
        map[i] = sc.nextLine().toCharArray();
        if (!robotFound) {
          for (int j = 0; j < map[i].length; j++) {

            char c = map[i][j];
            if (c == 'N' || c == 'S' || c == 'L' || c == 'O') {
              robot = new Robot(i, j, c);
              robotFound = true;
            }
          }
        }
      }
      char[] queries = sc.nextLine().toCharArray();
      // for each query
      for (int i = 0; i < S; i++) {
        char query = queries[i];
        if (query == 'D') {
          robot.turnRight();
        } else if (query == 'E') {
          robot.turnLeft();
        } else if (query == 'F') {
          int tempRow = robot.row, tempCol = robot.col;
          robot.moveForward();
          if (robot.row < 0 || robot.row > N - 1 || robot.col > M - 1 || robot.col < 0) {
            robot.row = tempRow;
            robot.col = tempCol;
          } else {
            char currentChar = map[robot.row][robot.col];
            if (currentChar == '*') {
              robot.collectSticker();
              map[robot.row][robot.col] = '.';
            } else if (currentChar == '#') {
              robot.row = tempRow;
              robot.col = tempCol;
            }
          }
        }
      }
      System.out.println(robot.sticker);
    }
  }
  public static void main(String s[]) {

    // Getting save directory
    String saveDir;
    if (s.length > 0) {
      saveDir = s[0];
    } else {
      saveDir =
          JOptionPane.showInputDialog(
              null,
              "Please enter directory where "
                  + "the images is/will be saved\n\n"
                  + "Also possible to specifiy as argument 1 when "
                  + "running this program.",
              "l:\\webcamtest");
    }

    String layout = "";
    if (s.length > 1) {
      layout = s[1];
    }

    // Move mouse to the point 5000,5000 px (out of the screen)
    Robot rob;
    try {
      rob = new Robot();
      rob.setAutoDelay(500); // 0,5 s
      rob.mouseMove(5000, 5000);
    } catch (AWTException e) {
      e.printStackTrace();
    }

    // Make the main window
    JFrame frame = new JFrame();
    frame.setAlwaysOnTop(true);
    frame.setTitle(
        "Webcam capture and imagefading - "
            + "Vitenfabrikken Jærmuseet - "
            + "made by Hallvard Nygård - "
            + "Vitenfabrikken.no / Jaermuseet.no");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setUndecorated(true);

    WebcamCaptureAndFadePanel panel = new WebcamCaptureAndFadePanel(saveDir, layout);
    frame.getContentPane().add(panel);
    frame.addKeyListener(panel);
    frame.pack();

    frame.setVisible(true);
  }
Exemplo n.º 4
0
  public void placeTower() {
    idealTowerSpawnLocations = null;
    turnsWaitedForTowerSpawnLocationMessage = 0;
    towerSpawnFromLocation = null;
    towerSpawnLocation = null;
    turnsWaitedForMove = 0;

    ArrayList<MapLocation> towers; // //sensing.senseAlliedTeleporters();
    int towerID = BroadcastMessage.everyone;
    MapLocation location;
    Robot robot;

    towers = sensing.senseAlliedTowerLocations();
    if (towers.size() > 0) {
      // no teles in range, but there are other towers.  they should be talking to the tele and
      // should know the status of where to build
      try {
        location = navigation.findClosest(towers);
        if (controller.canSenseSquare(location) && location != null) {
          robot = controller.senseGroundRobotAtLocation(location);
          if (robot != null) towerID = robot.getID();
          else pa("cannot sense robot at " + location);
        }
        messaging.sendTowerBuildLocationRequest(towerID);
        setGoal(Goal.askingForTowerLocation);
        return;
      } catch (Exception e) {
        pa("----Caught exception in place tower. " + e.toString());
      }
    }

    // no towers in range, lets just ask everyone
    messaging.sendTowerBuildLocationRequest(BroadcastMessage.everyone);
    setGoal(Goal.askingForTowerLocation);
    return;
  }
  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();
  }
Exemplo n.º 6
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;
          }
        }
      }
    }
  }