コード例 #1
0
 @Override
 public int compare(Threat a, Threat b) {
   return a.getThreat() < b.getThreat() ? -1 : a.getThreat() == b.getThreat() ? 0 : 1;
 }
コード例 #2
0
  /**
   * attempts to execute this goal
   *
   * @param player the player
   * @return if the goal is unable to be executed
   */
  public boolean execute(TTRPlayer player) {
    boolean consideringClaimedRoutes = true;
    ArrayList<ArrayList<Route>> neededRoutes = player.getPath(to, from, consideringClaimedRoutes);

    if (neededRoutes == null) {
      isCompleteable = false;
      return false;
    }
    for (ArrayList<Route> routes : neededRoutes) {
      System.out.println(
          "Route from "
              + routes.get(0).getDest1().name()
              + " to "
              + routes.get(0).getDest2().name()
              + " at cost "
              + routes.get(0).getCost());
      // System.out.print(routes.get(0).getDest1().name()+" ");
    }
    // order routes by the level of threat they face
    LinkedList<Threat> routesByThreat = new LinkedList<Threat>();
    for (ArrayList<Route> routeBundle : neededRoutes) {
      int totalThreat =
          calcTotalThreat(routeBundle.get(0).getDest1())
              + calcTotalThreat(routeBundle.get(0).getDest2());
      routesByThreat.add(new Threat(routeBundle, totalThreat));
    }
    routesByThreat.sort(new ThreatComparator());
    for (Threat threat : routesByThreat) {
      Destination dest1 = threat.getRoute().get(0).getDest1();
      Destination dest2 = threat.getRoute().get(0).getDest2();
      int threatAmount = threat.getThreat();
      System.out.print("Route from " + dest1 + " to " + dest2 + " has threat " + threatAmount);
    }
    // try to purchase route
    for (Threat threat : routesByThreat) {
      ArrayList<Route> routeBundle = threat.getRoute();
      for (Route route : routeBundle) {
        if (canPurchase(route, player.getHand(), player.getNumTrainPieces())) {
          System.out.println(
              "I think I can claim the route from " + route.getDest1() + " to " + route.getDest2());
          // if grey route
          if (route.getColor() == TrainCardColor.rainbow) {
            CardAmount maxColor = getColorOfMaxPieces(player.getHand());
            player.claimRoute(route, maxColor.getColor());
            return true;
          }
          // normal route
          player.claimRoute(route, route.getColor());
          return true;
        }
      }
    }
    // get cards for routes
    for (Threat threat : routesByThreat) {
      ArrayList<Route> routeBundle = threat.getRoute();
      for (Route route : routeBundle) {
        if (!canPurchase(route, player.getHand(), player.getNumTrainPieces())
            && route.getOwner() == null) {
          // if grey route
          if (route.getColor() == TrainCardColor.rainbow) {
            player.drawTrainCard(0); // get random card
            return true;
          }
          // normal route
          ArrayList<TrainCard> visibleCards = player.getFaceUpCards();
          for (int i = 0; i < visibleCards.size(); i++) {
            if (visibleCards.get(i).getColor() == route.getColor()) {
              player.drawTrainCard(i + 1);
              return true;
            }
          }
          // otherwise draw random card
          player.drawTrainCard(0);
          return true;
        }
      }
    }
    System.out.println("We have failed to execute");
    return false;
  }