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); }
/** * Builds a deck out of the selected block/set/format. * * @return a path to the generated deck. */ public static String generateDeck() { genDialog = new DeckGeneratorDialog(); if (genDialog.getSelectedColors() != null) { Deck deck = buildDeck(); return genDialog.saveDeck(deck); } // If the deck couldn't be generated or the user cancelled, repopulate the deck selection with // its cached value return PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TABLE_DECK_FILE, null); }
/** * Generates all the cards to use in the deck. Adds creatures, non-creatures, lands (including * non-basic). Fixes the deck, adjusting for size and color of the cards retrieved. * * @param deckSize how big the deck is to generate. * @param allowedColors which colors are allowed in the deck. * @param setsToUse which sets to use to retrieve cards for this deck. * @return the final deck to use. */ private static Deck generateDeck( int deckSize, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) { genPool = new DeckGeneratorPool(deckSize, allowedColors, genDialog.isSingleton()); final String[] sets = setsToUse.toArray(new String[setsToUse.size()]); // Creatures final CardCriteria creatureCriteria = new CardCriteria(); creatureCriteria.setCodes(sets); creatureCriteria.notTypes(CardType.LAND); creatureCriteria.types(CardType.CREATURE); if (!(genDialog.useArtifacts())) creatureCriteria.notTypes(CardType.ARTIFACT); // Non-creatures (sorcery, instant, enchantment, artifact etc.) final CardCriteria nonCreatureCriteria = new CardCriteria(); nonCreatureCriteria.setCodes(sets); nonCreatureCriteria.notTypes(CardType.LAND); nonCreatureCriteria.notTypes(CardType.CREATURE); if (!(genDialog.useArtifacts())) nonCreatureCriteria.notTypes(CardType.ARTIFACT); // Non-basic land final CardCriteria nonBasicLandCriteria = new CardCriteria(); nonBasicLandCriteria.setCodes(sets); nonBasicLandCriteria.types(CardType.LAND); nonBasicLandCriteria.notSupertypes("Basic"); // Generate basic land cards Map<String, List<CardInfo>> basicLands = generateBasicLands(setsToUse); generateSpells(creatureCriteria, genPool.getCreatureCount()); generateSpells(nonCreatureCriteria, genPool.getNonCreatureCount()); generateLands(nonBasicLandCriteria, genPool.getLandCount(), basicLands); // Reconstructs the final deck and adjusts for Math rounding and/or missing cards return genPool.getDeck(); }
/** * Generates all the lands for the deck. Generates non-basic if selected by the user and if the * deck isn't monocolored. Will fetch non-basic lands if required and then fill up the remaining * space with basic lands. Basic lands are adjusted according to the mana symbols seen in the * cards used in this deck. Usually the lands will be well balanced relative to the color of * cards. * * @param criteria the criteria of the lands to search for in the database. * @param landsCount the amount of lands required for this deck. * @param basicLands information about the basic lands from the sets used. */ private static void generateLands( CardCriteria criteria, int landsCount, Map<String, List<CardInfo>> basicLands) { int tries = 0; int countNonBasic = 0; // Store the nonbasic lands (if any) we'll add List<Card> deckLands = new ArrayList<>(); // Calculates the percentage of colored mana symbols over all spells in the deck Map<String, Double> percentage = genPool.calculateSpellColorPercentages(); // Only dual/tri color lands are generated for now, and not non-basic lands that only produce // colorless mana. if (!genPool.isMonoColoredDeck() && genDialog.useNonBasicLand()) { List<Card> landCards = genPool.filterLands(CardRepository.instance.findCards(criteria)); int allCount = landCards.size(); Random random = new Random(); if (allCount > 0) { while (countNonBasic < landsCount / 2) { Card card = landCards.get(random.nextInt(allCount)); if (genPool.isValidLandCard(card)) { Card addedCard = card.copy(); deckLands.add(addedCard); genPool.addCard(addedCard); countNonBasic++; } tries++; // to avoid infinite loop if (tries > MAX_TRIES) { // Not a problem, just use what we have break; } } } } // Calculate the amount of colored mana already can be produced by the non-basic lands Map<String, Integer> count = genPool.countManaProduced(deckLands); // Fill up the rest of the land quota with basic lands adjusted to fit the deck's mana costs addBasicLands(landsCount - countNonBasic, percentage, count, basicLands); }