/** * If the user has selected random colors, pick them randomly for the user. * * @param selectedColors a string of the colors selected. * @return a String representation of the new colors chosen. */ private static String getRandomColors(String selectedColors) { Random random = new Random(); List<Character> availableColors = new ArrayList<>(); for (ColoredManaSymbol cms : ColoredManaSymbol.values()) { availableColors.add(cms.toString().charAt(0)); } StringBuilder generatedColors = new StringBuilder(); int randomColors = 0; for (int i = 0; i < selectedColors.length(); i++) { char currentColor = selectedColors.charAt(i); if (currentColor != 'X') { generatedColors.append(currentColor); availableColors.remove(new Character(currentColor)); } else { randomColors++; } } for (int i = 0; i < randomColors && !availableColors.isEmpty(); i++) { int index = random.nextInt(availableColors.size()); generatedColors.append(availableColors.remove(index)); } return generatedColors.toString(); }
/** * Returns a map of colored mana symbol to basic land cards of that color. * * @param setsToUse which sets to retrieve basic lands from. * @return a map of color to basic lands. */ private static Map<String, List<CardInfo>> generateBasicLands(List<String> setsToUse) { List<String> landSets = new LinkedList<>(); // decide from which sets basic lands are taken from for (String setCode : setsToUse) { ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); if (expansionInfo.hasBasicLands()) { landSets.add(expansionInfo.getCode()); } } // if sets have no basic land, take land from block if (landSets.isEmpty()) { for (String setCode : setsToUse) { ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName()); for (ExpansionInfo blockSet : blockSets) { if (blockSet.hasBasicLands()) { landSets.add(blockSet.getCode()); } } } } // if still no set with lands found, take one by random if (landSets.isEmpty()) { // if sets have no basic lands and also it has no parent or parent has no lands get last set // with lands // select a set with basic lands by random Random generator = new Random(); List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate(); if (basicLandSets.size() > 0) { landSets.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode()); } } if (landSets.isEmpty()) { throw new IllegalArgumentException("No set with basic land was found"); } CardCriteria criteria = new CardCriteria(); if (!landSets.isEmpty()) { criteria.setCodes(landSets.toArray(new String[landSets.size()])); } Map<String, List<CardInfo>> basicLandMap = new HashMap<>(); for (ColoredManaSymbol c : ColoredManaSymbol.values()) { String landName = DeckGeneratorPool.getBasicLandName(c.toString()); criteria.rarities(Rarity.LAND).name(landName); List<CardInfo> cards = CardRepository.instance.findCards(criteria); basicLandMap.put(landName, cards); } return basicLandMap; }
protected static Deck buildDeck() { String selectedColors = genDialog.getSelectedColors(); List<ColoredManaSymbol> allowedColors = new ArrayList<>(); selectedColors = selectedColors != null ? selectedColors.toUpperCase() : getRandomColors("X"); String format = genDialog.getSelectedFormat(); List<String> setsToUse = ConstructedFormats.getSetsByFormat(format); if (setsToUse.isEmpty()) { // Default to using all sets setsToUse = ExpansionRepository.instance.getSetCodes(); } int deckSize = genDialog.getDeckSize(); if (selectedColors.contains("X")) { selectedColors = getRandomColors(selectedColors); } for (int i = 0; i < selectedColors.length(); i++) { char c = selectedColors.charAt(i); allowedColors.add(ColoredManaSymbol.lookup(c)); } return generateDeck(deckSize, allowedColors, setsToUse); }
/** * Once any non-basic lands are added, add basic lands until the deck is filled. * * @param landsNeeded how many remaining lands are needed. * @param percentage the percentage needed for each color in the final deck. * @param count how many of each color can be produced by non-basic lands. * @param basicLands list of information about basic lands from the database. */ private static void addBasicLands( int landsNeeded, Map<String, Double> percentage, Map<String, Integer> count, Map<String, List<CardInfo>> basicLands) { int colorTotal = 0; ColoredManaSymbol colorToAdd = null; // Add up the totals for all colors, to keep track of the percentage a color is. for (Map.Entry<String, Integer> c : count.entrySet()) { colorTotal += c.getValue(); } // Keep adding basic lands until we fill the deck while (landsNeeded > 0) { double minPercentage = Integer.MIN_VALUE; for (ColoredManaSymbol color : ColoredManaSymbol.values()) { // What percentage of this color is requested double neededPercentage = percentage.get(color.toString()); // If there is a 0% need for basic lands of this color, skip it if (neededPercentage <= 0) { continue; } int currentCount = count.get(color.toString()); double thisPercentage = 0.0; // Calculate the percentage of lands so far that produce this color if (currentCount > 0) thisPercentage = (currentCount / (double) colorTotal) * 100.0; // Check if the color is the most "needed" (highest percentage) we have seen so far if (neededPercentage - thisPercentage > minPercentage) { // Put this color land forward to be added colorToAdd = color; minPercentage = (neededPercentage - thisPercentage); } } if (colorToAdd != null) { genPool.addCard(getBasicLand(colorToAdd, basicLands)); count.put(colorToAdd.toString(), count.get(colorToAdd.toString()) + 1); colorTotal++; landsNeeded--; } } }
/** * Return a random basic land of the chosen color. * * @param color the color the basic land should produce. * @param basicLands list of information about basic lands from the database. * @return a single basic land that produces the color needed. */ private static Card getBasicLand( ColoredManaSymbol color, Map<String, List<CardInfo>> basicLands) { Random random = new Random(); String landName = DeckGeneratorPool.getBasicLandName(color.toString()); return basicLands.get(landName).get(random.nextInt(basicLands.size() - 1)).getMockCard().copy(); }
@Override public String getText() { return "{2/" + mana.toString() + "}"; }
@Override public boolean containsColor(ColoredManaSymbol coloredManaSymbol) { return mana.equals(coloredManaSymbol); }