Пример #1
0
  @Override
  public void receiveMessage(Message m) {
    if (m instanceof MapUpdate) {
      // int del = -1;
      for (int i = 0; i < teamPosition.size(); ++i) {
        Bag b = (Bag) teamPosition.get(i);
        if (((MapUpdate) m).sender.equals(((MapUpdate) b.get(0)).sender)) {
          // del = i;
          System.out.printf(
              "Newer map received from sender. Already collected %d positions.\n",
              ((Bag) teamPosition.get(i)).size() - 1);
          teamPosition.remove(i);
          break;
        }
      }
      // if (del != -1) {
      // }

      MapUpdate mu = (MapUpdate) m;
      // for position some more steps are needed -> wait for pings from sender while "traveling
      // around"
      Bag b = new Bag();
      b.add(mu);
      b.add(new Int2D(super.pos.x, super.pos.y));
      teamPosition.add(b);
    } else if (m instanceof Ping) {
      Ping p = (Ping) m;
      // Look for all teams that want to update the map
      for (int i = 0; i < teamPosition.size(); ++i) {
        Bag b = (Bag) teamPosition.get(i);
        if (p.sender.equals(((MapUpdate) b.get(0)).sender)) {
          boolean newPos = true;
          for (int j = 1; j < b.size(); ++j) {
            Int2D pos = (Int2D) b.get(j);
            if (pos.x == super.pos.x && pos.y == super.pos.y) {
              newPos = false;
            }
          }
          if (newPos) {
            if (b.size() < 1 + neededPositions - 1) {
              b.add(new Int2D(super.pos.x, super.pos.y));
              // System.out.printf("New position collected.\n");
            } else {
              // all needed positions collected
              MapUpdate msg = (MapUpdate) b.get(0);
              System.out.printf("Collected all needed positions!\n");
              MapUpdate answer = new MapUpdate(this, msg.sender, msg.number, null, null, true);
              sendMessage(answer);
              // System.out.printf("Sender position:
              // %d,%d.\n",msg.sender.getPos().x,msg.sender.getPos().y);
              updateBaseMap(
                  msg.map,
                  msg.agentpos,
                  posToLocalMapPos(msg.sender.getPos(), msg.sender.getLocalMapScale()));
            }
          }
        }
      }
    }
  }
  /**
   * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) passed
   * to their dorm rooms, in a way that does not take race into account. (compare {@link
   * #assignByRace}.) As a precursor (side effect) to this, any students already existing in
   * upperclass dorms will be removed.
   */
  public void assign(Bag students) {

    emptyDorms();

    // Purge all freshmen. (We're not assigning those.)
    Bag upperclassmen = null;
    try {
      upperclassmen = (Bag) students.clone();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    for (int x = upperclassmen.size() - 1; x >= 0; x--) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        upperclassmen.remove(s);
      }
    }

    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        // We're only assigning upperclassmen here.
        continue;
      }
      s.leaveRoom();
      for (int y = 0; y < dorms.size(); y++) {
        if (s.hasRoom()) {
          break;
        }
        Dorm d = (Dorm) dorms.get(y);
        if (d.isFull()) {
          continue;
        }
        if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) {
          continue;
        }
        int i = 0;
        while (i < d.getNumRooms()) {
          if (s.hasRoom()) {
            break;
          }
          try {
            d.getRoomByIndex(i).addResident(s);
          } catch (IllegalArgumentException e) { // System.out.println(e);
          }
          i++;
        }
      }
    }

    // Error testing to see if there are upperclassmen who don't have a
    //  room/dorms that aren't full
    // int q=0;
    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (!s.hasRoom()) {
        System.out.println(s + " has no upperclass dorm room!");
      }
    }
  }
Пример #3
0
 public void removeTeamMember(AbstractAgent a) {
   team.remove(a);
 }