From 7edffebb5d6c49d3393fb44daed47be80ad55613 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Thu, 13 Jan 2011 19:53:24 +0300 Subject: [PATCH] Separated DeckGenerator and DeckBuilder. Moved DeckBuilder to mage.common (will be used by ai to construct draft decks). --- .../client/deck/generator/DeckGenerator.java | 254 +++------------- .../mage/interfaces/rate/RateCallback.java | 14 + Mage.Common/src/mage/utils/DeckBuilder.java | 272 ++++++++++++++++++ 3 files changed, 320 insertions(+), 220 deletions(-) create mode 100644 Mage.Common/src/mage/interfaces/rate/RateCallback.java create mode 100644 Mage.Common/src/mage/utils/DeckBuilder.java diff --git a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGenerator.java b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGenerator.java index 6ff6b175e5..4a76b56bf5 100644 --- a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGenerator.java +++ b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGenerator.java @@ -13,6 +13,7 @@ import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; +import mage.Constants; import mage.Constants.CardType; import mage.Constants.ColoredManaSymbol; import mage.Mana; @@ -22,9 +23,16 @@ import mage.cards.ExpansionSet; import mage.cards.decks.Deck; import mage.client.cards.CardsStorage; import mage.client.util.gui.ColorsChooser; +import mage.interfaces.rate.RateCallback; import mage.sets.Sets; import mage.utils.CardUtil; +import mage.utils.DeckBuilder; +/** + * Generates random card pool and builds a deck. + * + * @author nantuko + */ public class DeckGenerator { private static JDialog dlg; @@ -32,22 +40,21 @@ public class DeckGenerator { private static final int SPELL_CARD_POOL_SIZE = 60; - private static final int DECK_COUNT[] = {3, 6, 6, 4, 3, 2}; - private static final int DECK_COST[] = {1, 2, 3, 4, 6, 10}; - private static final int DECK_SPELLS = 24; private static final int DECK_LANDS = 16; - private static final int DECK_SIZE = DECK_SPELLS + DECK_LANDS; - private static final int MIN_CARD_SCORE = 25; - private static final int MIN_SOURCE = 16; private static final int MAX_NON_BASIC_SOURCE = DECK_LANDS / 2; private static final boolean GENERATE_RANDOM_BASIC_LAND = true; private static final int MAX_TRIES = 4096; private static Deck deck = new Deck(); - private static String manaSource; private static final int ADDITIONAL_CARDS_FOR_3_COLOR_DECKS = 20; + /** + * Opens color chooser dialog. Generates deck. + * Saves generated deck and use it as selected deck to play. + * + * @return + */ public static String generateDeck() { JPanel p0 = new JPanel(); p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS)); @@ -97,8 +104,10 @@ public class DeckGenerator { return selectedColors; } + /** + * Generates card pool + */ protected static void buildDeck() { - deck = new Deck(); List allowedColors = new ArrayList(); selectedColors = selectedColors.toUpperCase(); for (int i = 0; i < selectedColors.length(); i++) { @@ -115,19 +124,16 @@ public class DeckGenerator { System.out.println("deck generator card pool: spells=" + spellCardPool.size() + ", lands=" + landCardPool.size()); - final Collection remainingCards = new ArrayList(); - for (final Card card : spellCardPool) { - remainingCards.add(new MageScoredCard(card, allowedColors)); - } - int min = 0; - for (int index = 0; index < DECK_COUNT.length; index++) { - final int max = DECK_COST[index]; - addCardsToDeck(remainingCards, min, max, DECK_COUNT[index]); - min = max + 1; - } - addCardsToDeck(remainingCards, 0, 4, DECK_SPELLS - deck.getCards().size()); - addCardsToDeck(remainingCards, 5, 10, DECK_SPELLS - deck.getCards().size()); - addLandsToDeck(allowedColors, landCardPool); + deck = DeckBuilder.buildDeck(spellCardPool, allowedColors, landCardPool, new RateCallback() { + @Override + public int rateCard(Card card) { + return CardsStorage.rateCard(card); + } + @Override + public Card getBestBasicLand(ColoredManaSymbol color) { + return DeckGenerator.getBestBasicLand(color); + } + }); } /** @@ -249,111 +255,14 @@ public class DeckGenerator { return false; } - private static void addCardsToDeck(final Collection remainingCards, final int minCost, final int maxCost, - final int count) { - - for (int c = count; c > 0; c--) { - - MageScoredCard bestCard = null; - int bestScore = -1; - - for (final MageScoredCard draftedCard : remainingCards) { - - final int score = draftedCard.getScore(); - final int cost = draftedCard.getConvertedCost(); - if (score > bestScore && cost >= minCost && cost <= maxCost) { - bestScore = score; - bestCard = draftedCard; - } - } - - if (bestCard == null || bestScore < MIN_CARD_SCORE) { - break; - } - deck.getCards().add(bestCard.card); - remainingCards.remove(bestCard); - } - } - - private static void addLandsToDeck(List allowedColors, List landCardPool) { - - // Calculate statistics per color. - final Map colorCount = new HashMap(); - for (final Card card : deck.getCards()) { - - for (String symbol : card.getManaCost().getSymbols()) { - int count = 0; - symbol = symbol.replace("{", "").replace("}", ""); - if (isColoredMana(symbol)) { - for (ColoredManaSymbol allowed : allowedColors) { - if (allowed.toString().equals(symbol)) { - count++; - } - } - if (count > 0) { - Integer typeCount = colorCount.get(symbol); - if (typeCount == null) { - typeCount = new Integer(0); - } - typeCount += 1; - colorCount.put(symbol, typeCount); - } - } - } - } - - // Add suitable non basic lands to deck in order of pack. - final Map colorSource = new HashMap(); - for (final ColoredManaSymbol color : ColoredManaSymbol.values()) { - colorSource.put(color.toString(), 0); - } - for (final Card landCard : landCardPool) { - deck.getCards().add(landCard); - for (Mana mana : landCard.getMana()) { - for (ColoredManaSymbol color : allowedColors) { - int amount = mana.getColor(color); - if (amount > 0) { - Integer count = colorSource.get(color.toString()); - count += amount; - colorSource.put(color.toString(), count); - } - } - } - - } - - // Add optimal basic lands to deck. - while (deck.getCards().size() < DECK_SIZE) { - - ColoredManaSymbol bestColor = null; - int lowestRatio = Integer.MAX_VALUE; - for (final ColoredManaSymbol color : ColoredManaSymbol.values()) { - - final Integer count = colorCount.get(color.toString()); - if (count != null && count > 0) { - final int source = colorSource.get(color.toString()); - final int ratio; - if (source < MIN_SOURCE) { - ratio = source - count; - } else { - ratio = source * 100 / count; - } - if (ratio < lowestRatio) { - lowestRatio = ratio; - bestColor = color; - } - } - } - final Card landCard = getBestBasicLand(bestColor); - Integer count = colorSource.get(bestColor.toString()); - count++; - colorSource.put(bestColor.toString(), count); - deck.getCards().add(landCard); - } - } - + /** + * Get random basic land that can produce specified color mana. + * Random here means random set and collector id for the same mana producing land. + * + * @param color + * @return + */ private static Card getBestBasicLand(ColoredManaSymbol color) { - manaSource = color.toString(); if (color.equals(ColoredManaSymbol.G)) { return Sets.findCard("Forest", GENERATE_RANDOM_BASIC_LAND); } @@ -373,102 +282,7 @@ public class DeckGenerator { return null; } - private static class MageScoredCard { - - private Card card; - private int score; - - private static final int SINGLE_PENALTY[] = {0, 1, 1, 3, 6, 9}; - //private static final int DOUBLE_PENALTY[] = { 0, 0, 1, 2, 4, 6 }; - - public MageScoredCard(Card card, List allowedColors) { - this.card = card; - - int type = 0; - if (card.getCardType().contains(CardType.CREATURE)) { - type = 10; - } else if (card.getSubtype().contains("Equipment")) { - type = 8; - } else if (card.getSubtype().contains("Aura")) { - type = 5; - } else if (card.getCardType().contains(CardType.INSTANT)) { - type = 7; - } else { - type = 6; - } - - this.score = - // 5*card.getValue() + // not possible now - 3 * CardsStorage.rateCard(card) + - // 3*card.getRemoval() + // not possible now - type + getManaCostScore(card, allowedColors); - } - - private int getManaCostScore(Card card, List allowedColors) { - int converted = card.getManaCost().convertedManaCost(); - final Map singleCount = new HashMap(); - int maxSingleCount = 0; - for (String symbol : card.getManaCost().getSymbols()) { - int count = 0; - symbol = symbol.replace("{", "").replace("}", ""); - if (isColoredMana(symbol)) { - for (ColoredManaSymbol allowed : allowedColors) { - if (allowed.toString().equals(symbol)) { - count++; - } - } - if (count == 0) { - return -30; - } - Integer typeCount = singleCount.get(symbol); - if (typeCount == null) { - typeCount = new Integer(0); - } - typeCount += 1; - singleCount.put(symbol, typeCount); - maxSingleCount = Math.max(maxSingleCount, typeCount); - } - } - return 2 * converted + 3 * (10 - SINGLE_PENALTY[maxSingleCount]/*-DOUBLE_PENALTY[doubleCount]*/); - } - - public int getScore() { - return this.score; - } - - public int getConvertedCost() { - return this.card.getManaCost().convertedManaCost(); - } - - public Card getCard() { - return this.card; - } - } - protected static boolean isColoredMana(String symbol) { return symbol.equals("W") || symbol.equals("G") || symbol.equals("U") || symbol.equals("B") || symbol.equals("R"); } - - public static void main(String[] args) { - for (Card card : CardsStorage.getAllCards()) { - System.out.println(card.getName()); - System.out.print(" "); - for (String symbol : card.getManaCost().getSymbols()) { - symbol = symbol.replace("{", "").replace("}", ""); - System.out.print(symbol + " "); - } - System.out.println(CardUtil.isBasicLand(card)); - List allowedColors = new ArrayList(); - allowedColors.add(ColoredManaSymbol.lookup('B')); - allowedColors.add(ColoredManaSymbol.lookup('G')); - DeckGenerator.MageScoredCard m = new DeckGenerator.MageScoredCard(card, allowedColors); - - System.out.println(); - System.out.println(" score: " + m.getScore()); - System.out.println(); - } - - //System.out.println("Done! Path: " + generateDeck()); - - } } diff --git a/Mage.Common/src/mage/interfaces/rate/RateCallback.java b/Mage.Common/src/mage/interfaces/rate/RateCallback.java new file mode 100644 index 0000000000..d686ac1215 --- /dev/null +++ b/Mage.Common/src/mage/interfaces/rate/RateCallback.java @@ -0,0 +1,14 @@ +package mage.interfaces.rate; + +import mage.Constants; +import mage.cards.Card; + +/** + * Interface for the class responsible for rating cards. + * + * @author nantuko + */ +public interface RateCallback { + int rateCard(Card card); + Card getBestBasicLand(Constants.ColoredManaSymbol color); +} \ No newline at end of file diff --git a/Mage.Common/src/mage/utils/DeckBuilder.java b/Mage.Common/src/mage/utils/DeckBuilder.java new file mode 100644 index 0000000000..922fc79754 --- /dev/null +++ b/Mage.Common/src/mage/utils/DeckBuilder.java @@ -0,0 +1,272 @@ +package mage.utils; + +import mage.Constants; +import mage.Constants.ColoredManaSymbol; +import mage.Mana; +import mage.cards.Card; +import mage.cards.decks.Deck; +import mage.interfaces.rate.RateCallback; + +import java.util.*; + +/** + * Builds deck from provided card pool. + * + * @author nantuko + */ +public class DeckBuilder { + + private static String selectedColors; + + private static final int SPELL_CARD_POOL_SIZE = 60; + + private static final int DECK_COUNT[] = {3, 6, 6, 4, 3, 2}; + private static final int DECK_COST[] = {1, 2, 3, 4, 6, 10}; + private static final int DECK_SPELLS = 24; + private static final int DECK_LANDS = 16; + private static final int DECK_SIZE = DECK_SPELLS + DECK_LANDS; + private static final int MIN_CARD_SCORE = 25; + private static final int MIN_SOURCE = 16; + private static Deck deck = new Deck(); + + /** + * Hide constructor. + */ + private DeckBuilder() { + } + + public synchronized static Deck buildDeck(List spellCardPool, List allowedColors, List landCardPool, RateCallback callback) { + deck = new Deck(); + + final Collection remainingCards = new ArrayList(); + for (final Card card : spellCardPool) { + remainingCards.add(new MageScoredCard(card, allowedColors, callback)); + } + int min = 0; + for (int index = 0; index < DECK_COUNT.length; index++) { + final int max = DECK_COST[index]; + addCardsToDeck(remainingCards, min, max, DECK_COUNT[index]); + min = max + 1; + } + addCardsToDeck(remainingCards, 0, 4, DECK_SPELLS - deck.getCards().size()); + addCardsToDeck(remainingCards, 5, 10, DECK_SPELLS - deck.getCards().size()); + addLandsToDeck(allowedColors, landCardPool, callback); + + return deck; + } + + /** + * Checks that chosen card can produce mana of specific color. + * + * @param card + * @param allowedColors + * @return + */ + private static boolean cardCardProduceChosenColors(Card card, List allowedColors) { + int score = 0; + for (Mana mana : card.getMana()) { + for (ColoredManaSymbol color : allowedColors) { + score = score + mana.getColor(color); + } + } + if (score > 1) { + return true; + } + return false; + } + + /** + * Chosed best scored card and adds it to the deck. + * + * @param remainingCards + * @param minCost + * @param maxCost + * @param count + */ + private static void addCardsToDeck(final Collection remainingCards, final int minCost, final int maxCost, + final int count) { + + for (int c = count; c > 0; c--) { + + MageScoredCard bestCard = null; + int bestScore = -1; + + for (final MageScoredCard draftedCard : remainingCards) { + + final int score = draftedCard.getScore(); + final int cost = draftedCard.getConvertedCost(); + if (score > bestScore && cost >= minCost && cost <= maxCost) { + bestScore = score; + bestCard = draftedCard; + } + } + + if (bestCard == null || bestScore < MIN_CARD_SCORE) { + break; + } + deck.getCards().add(bestCard.card); + remainingCards.remove(bestCard); + } + } + + /** + * Adds lands from non basic land (if provided), adds basic lands getting them from provided {@link RateCallback}}. + * + * @param allowedColors + * @param landCardPool + * @param callback + */ + private static void addLandsToDeck(List allowedColors, List landCardPool, RateCallback callback) { + + // Calculate statistics per color. + final Map colorCount = new HashMap(); + for (final Card card : deck.getCards()) { + + for (String symbol : card.getManaCost().getSymbols()) { + int count = 0; + symbol = symbol.replace("{", "").replace("}", ""); + if (isColoredMana(symbol)) { + for (ColoredManaSymbol allowed : allowedColors) { + if (allowed.toString().equals(symbol)) { + count++; + } + } + if (count > 0) { + Integer typeCount = colorCount.get(symbol); + if (typeCount == null) { + typeCount = new Integer(0); + } + typeCount += 1; + colorCount.put(symbol, typeCount); + } + } + } + } + + // Add suitable non basic lands to deck in order of pack. + final Map colorSource = new HashMap(); + for (final ColoredManaSymbol color : ColoredManaSymbol.values()) { + colorSource.put(color.toString(), 0); + } + if (landCardPool != null) { + for (final Card landCard : landCardPool) { + deck.getCards().add(landCard); + for (Mana mana : landCard.getMana()) { + for (ColoredManaSymbol color : allowedColors) { + int amount = mana.getColor(color); + if (amount > 0) { + Integer count = colorSource.get(color.toString()); + count += amount; + colorSource.put(color.toString(), count); + } + } + } + } + } + + // Add optimal basic lands to deck. + while (deck.getCards().size() < DECK_SIZE) { + ColoredManaSymbol bestColor = null; + int lowestRatio = Integer.MAX_VALUE; + for (final ColoredManaSymbol color : ColoredManaSymbol.values()) { + + final Integer count = colorCount.get(color.toString()); + if (count != null && count > 0) { + final int source = colorSource.get(color.toString()); + final int ratio; + if (source < MIN_SOURCE) { + ratio = source - count; + } else { + ratio = source * 100 / count; + } + if (ratio < lowestRatio) { + lowestRatio = ratio; + bestColor = color; + } + } + } + final Card landCard = callback.getBestBasicLand(bestColor); + Integer count = colorSource.get(bestColor.toString()); + count++; + colorSource.put(bestColor.toString(), count); + deck.getCards().add(landCard); + } + } + + private static class MageScoredCard { + + private Card card; + private int score; + + private static final int SINGLE_PENALTY[] = {0, 1, 1, 3, 6, 9}; + //private static final int DOUBLE_PENALTY[] = { 0, 0, 1, 2, 4, 6 }; + + public MageScoredCard(Card card, List allowedColors, RateCallback cardRater) { + this.card = card; + + int type = 0; + if (card.getCardType().contains(Constants.CardType.CREATURE)) { + type = 10; + } else if (card.getSubtype().contains("Equipment")) { + type = 8; + } else if (card.getSubtype().contains("Aura")) { + type = 5; + } else if (card.getCardType().contains(Constants.CardType.INSTANT)) { + type = 7; + } else { + type = 6; + } + + this.score = + // 5*card.getValue() + // not possible now + 3 * cardRater.rateCard(card) + + // 3*card.getRemoval() + // not possible now + type + getManaCostScore(card, allowedColors); + } + + private int getManaCostScore(Card card, List allowedColors) { + int converted = card.getManaCost().convertedManaCost(); + final Map singleCount = new HashMap(); + int maxSingleCount = 0; + for (String symbol : card.getManaCost().getSymbols()) { + int count = 0; + symbol = symbol.replace("{", "").replace("}", ""); + if (isColoredMana(symbol)) { + for (ColoredManaSymbol allowed : allowedColors) { + if (allowed.toString().equals(symbol)) { + count++; + } + } + if (count == 0) { + return -30; + } + Integer typeCount = singleCount.get(symbol); + if (typeCount == null) { + typeCount = new Integer(0); + } + typeCount += 1; + singleCount.put(symbol, typeCount); + maxSingleCount = Math.max(maxSingleCount, typeCount); + } + } + return 2 * converted + 3 * (10 - SINGLE_PENALTY[maxSingleCount]/*-DOUBLE_PENALTY[doubleCount]*/); + } + + public int getScore() { + return this.score; + } + + public int getConvertedCost() { + return this.card.getManaCost().convertedManaCost(); + } + + public Card getCard() { + return this.card; + } + } + + protected static boolean isColoredMana(String symbol) { + return symbol.equals("W") || symbol.equals("G") || symbol.equals("U") || symbol.equals("B") || symbol.equals("R"); + } + +} \ No newline at end of file