@Override public int compare(Route a, Route b) { // find number int aTotalThreat = calcTotalThreat(a.getDest1()) + calcTotalThreat(a.getDest2()); int bTotalThreat = calcTotalThreat(b.getDest1()) + calcTotalThreat(b.getDest2()); return (aTotalThreat < bTotalThreat) ? -1 : (aTotalThreat == bTotalThreat) ? 0 : 1; }
private boolean canPurchase(Route route, ArrayList<TrainCard> hand, int numPieces) { Routes routes = Routes.getInstance(); TrainCardColor routeColor = route.getColor(); int routeCost = route.getCost(); if (routes.isRouteClaimed(route)) { System.out.println( "This route from " + route.getDest1() + " to " + route.getDest2() + " is owned. You have failed good sir."); return false; } if (routeCost > numPieces) { System.out.println("Not enough pieces"); return false; } // tally up relevant pieces int piecesOfColorAvailible = 0; int piecesOfRainbowAvailible = 0; for (TrainCard card : hand) { if (card.getColor() == routeColor) { piecesOfColorAvailible++; } if (card.getColor() == TrainCardColor.rainbow) { piecesOfRainbowAvailible++; } } // grey routes if (routeColor == TrainCardColor.rainbow) { CardAmount mostCard = getColorOfMaxPieces(hand); if ((mostCard.getAmount() + piecesOfRainbowAvailible) >= routeCost) { return true; } } // normal routes if ((piecesOfColorAvailible + piecesOfRainbowAvailible) >= routeCost) { return true; } return false; }
/** * 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; }