/** * Generates all spells for the deck. Each card is retrieved from the database and checked against * the converted mana cost (CMC) needed for the current card pool. If a card's CMC matches the CMC * range required by the pool, it is added to the deck. This ensures that the majority of cards * fit a fixed mana curve for the deck, and it is playable. Creatures and non-creatures are * retrieved separately to ensure the deck contains a reasonable mix of both. * * @param criteria the criteria to search for in the database. * @param spellCount the number of spells that match the criteria needed in the deck. */ private static void generateSpells(CardCriteria criteria, int spellCount) { List<CardInfo> cardPool = CardRepository.instance.findCards(criteria); int retrievedCount = cardPool.size(); List<DeckGeneratorCMC> deckCMCs = genPool.getCMCsForSpellCount(spellCount); Random random = new Random(); int count = 0; int reservesAdded = 0; if (retrievedCount > 0 && retrievedCount >= spellCount) { int tries = 0; while (count < spellCount) { Card card = cardPool.get(random.nextInt(retrievedCount)).getMockCard(); if (genPool.isValidSpellCard(card)) { int cardCMC = card.getManaCost().convertedManaCost(); for (DeckGeneratorCMC deckCMC : deckCMCs) { if (cardCMC >= deckCMC.min && cardCMC <= deckCMC.max) { int currentAmount = deckCMC.getAmount(); if (currentAmount > 0) { deckCMC.setAmount(currentAmount - 1); genPool.addCard(card.copy()); count++; } } else { if (reservesAdded < genPool.getDeckSize() / 2) { genPool.tryAddReserve(card, cardCMC); reservesAdded++; } } } } tries++; if (tries > MAX_TRIES) { // Break here, we'll fill in random missing ones later break; } } } else { throw new IllegalStateException("Not enough cards to generate deck."); } }