Ejemplo n.º 1
0
  /** This method is the control loop for an active {@link RobotClient}. */
  public void run() {
    // Put a spiffy message in the console
    Mazewar.consolePrintLn("Robot client \"" + this.getName() + "\" activated.");

    // Loop while we are active
    while (active) {
      // Try to move forward
      if (!forward()) {
        // If we fail...
        if (randomGen.nextInt(3) == 1) {
          // turn left!
          turnLeft();
        } else {
          // or perhaps turn right!
          turnRight();
        }
      }

      // Shoot at things once in a while.
      if (randomGen.nextInt(10) == 1) {
        fire();
      }

      // Sleep so the humans can possibly compete.
      try {
        thread.sleep(200);
      } catch (Exception e) {
        // Shouldn't happen.
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Internal helper for handling the death of a {@link Client}.
   *
   * @param source The {@link Client} that fired the projectile.
   * @param target The {@link Client} that was killed.
   */
  private synchronized void killClient(Client source, Client target) {
    assert (source != null);
    assert (target != null);
    Mazewar.consolePrintLn(source.getName() + " just vaporized " + target.getName());
    System.out.println(source.getName() + " just vaporized " + target.getName());

    Object o = clientMap.remove(target);
    assert (o instanceof Point);
    Point point = (Point) o;
    CellImpl cell = getCellImpl(point);
    cell.setContents(null);
    System.out.println("set contents to null!!!");
    // Pick a random starting point, and check to see if it is already occupied
    point = new Point(randomGen.nextInt(maxX), randomGen.nextInt(maxY));

    System.out.println(
        "Choose random point: " + randomGen.nextInt(maxX) + ":" + randomGen.nextInt(maxY));
    cell = getCellImpl(point);
    // Repeat until we find an empty cell
    while (cell.getContents() != null) {
      point = new Point(randomGen.nextInt(maxX), randomGen.nextInt(maxY));
      cell = getCellImpl(point);
      System.out.println("try to find an empty cell");
    }
    System.out.println(
        "Finish chossing point: " + randomGen.nextInt(maxX) + ":" + randomGen.nextInt(maxY));
    // Direction d = Direction.random();
    Direction d = Direction.North;
    /*
    while(cell.isWall(d)) {
            //d = Direction.random();
        d.turnLeft();
    }*/
    cell.setContents(target);

    System.out.println("set contents to target!!!");
    clientMap.put(target, new DirectedPoint(point, d));
    update();
    notifyClientKilled(source, target);
  }