1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-04-10 17:00:08 -09:00

Used CardRepository for DeckGenerator

Rewritten the getRandomColors method
This commit is contained in:
North 2012-10-21 15:22:50 +03:00
parent 7ea23a1ba1
commit ca912d4e03

View file

@ -1,51 +1,54 @@
package mage.client.deck.generator; package mage.client.deck.generator;
import mage.Constants.CardType; import mage.Constants.CardType;
import mage.Constants.ColoredManaSymbol; import mage.Constants.ColoredManaSymbol;
import mage.Mana; import mage.Mana;
import mage.cards.Card; import mage.cards.Card;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.client.MageFrame; import mage.client.MageFrame;
import mage.client.cards.CardsStorage; import mage.client.cards.CardsStorage;
import mage.client.util.gui.ColorsChooser; import mage.client.util.gui.ColorsChooser;
import mage.client.util.sets.ConstructedFormats; import mage.client.util.sets.ConstructedFormats;
import mage.interfaces.rate.RateCallback; import mage.interfaces.rate.RateCallback;
import mage.sets.Sets; import mage.sets.Sets;
import mage.utils.CardUtil;
import mage.utils.DeckBuilder; import mage.utils.DeckBuilder;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.util.*; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID;
/** /**
* Generates random card pool and builds a deck. * Generates random card pool and builds a deck.
* *
* @author nantuko * @author nantuko
*/ */
public class DeckGenerator { public class DeckGenerator {
private static JDialog dlg; private static JDialog dlg;
private static String selectedColors; private static String selectedColors;
private static JComboBox formats; private static JComboBox formats;
private static final int SPELL_CARD_POOL_SIZE = 180; private static final int SPELL_CARD_POOL_SIZE = 180;
private static final int DECK_LANDS = 16; private static final int DECK_LANDS = 16;
private static final int MAX_NON_BASIC_SOURCE = DECK_LANDS / 2; private static final int MAX_NON_BASIC_SOURCE = DECK_LANDS / 2;
private static final boolean GENERATE_RANDOM_BASIC_LAND = true; private static final boolean GENERATE_RANDOM_BASIC_LAND = true;
private static final int MAX_TRIES = 4096; private static final int MAX_TRIES = 4096;
private static Deck deck = new Deck(); private static Deck deck = new Deck();
private static final int ADDITIONAL_CARDS_FOR_3_COLOR_DECKS = 20; private static final int ADDITIONAL_CARDS_FOR_3_COLOR_DECKS = 20;
private static String colors = "GWUBR";
/** /**
* Opens color chooser dialog. Generates deck. * Opens color chooser dialog. Generates deck.
* Saves generated deck and use it as selected deck to play. * Saves generated deck and use it as selected deck to play.
@ -55,21 +58,21 @@ public class DeckGenerator {
public static String generateDeck() { public static String generateDeck() {
JPanel p0 = new JPanel(); JPanel p0 = new JPanel();
p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS)); p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS));
JLabel text = new JLabel("Choose color for your deck: "); JLabel text = new JLabel("Choose color for your deck: ");
text.setAlignmentX(Component.CENTER_ALIGNMENT); text.setAlignmentX(Component.CENTER_ALIGNMENT);
p0.add(text); p0.add(text);
p0.add(Box.createVerticalStrut(5)); p0.add(Box.createVerticalStrut(5));
String chosen = MageFrame.getPreferences().get("genDeckColor", "u"); String chosen = MageFrame.getPreferences().get("genDeckColor", "u");
final ColorsChooser colorsChooser = new ColorsChooser(chosen); final ColorsChooser colorsChooser = new ColorsChooser(chosen);
p0.add(colorsChooser); p0.add(colorsChooser);
p0.add(Box.createVerticalStrut(5)); p0.add(Box.createVerticalStrut(5));
JLabel text2 = new JLabel("(X - random color)"); JLabel text2 = new JLabel("(X - random color)");
text2.setAlignmentX(Component.CENTER_ALIGNMENT); text2.setAlignmentX(Component.CENTER_ALIGNMENT);
p0.add(text2); p0.add(text2);
p0.add(Box.createVerticalStrut(5)); p0.add(Box.createVerticalStrut(5));
JPanel jPanel = new JPanel(); JPanel jPanel = new JPanel();
JLabel text3 = new JLabel("Choose format:"); JLabel text3 = new JLabel("Choose format:");
@ -81,9 +84,10 @@ public class DeckGenerator {
jPanel.add(text3); jPanel.add(text3);
jPanel.add(formats); jPanel.add(formats);
p0.add(jPanel); p0.add(jPanel);
final JButton btnGenerate = new JButton("Ok"); final JButton btnGenerate = new JButton("Ok");
btnGenerate.addActionListener(new ActionListener() { btnGenerate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
btnGenerate.setEnabled(false); btnGenerate.setEnabled(false);
colorsChooser.setEnabled(false); colorsChooser.setEnabled(false);
@ -94,6 +98,7 @@ public class DeckGenerator {
}); });
final JButton btnCancel = new JButton("Cancel"); final JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() { btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
dlg.setVisible(false); dlg.setVisible(false);
selectedColors = null; selectedColors = null;
@ -104,7 +109,7 @@ public class DeckGenerator {
dlg = optionPane.createDialog("Generating deck"); dlg = optionPane.createDialog("Generating deck");
dlg.setVisible(true); dlg.setVisible(true);
dlg.dispose(); dlg.dispose();
if (selectedColors != null) { if (selectedColors != null) {
buildDeck(); buildDeck();
try { try {
@ -115,53 +120,53 @@ public class DeckGenerator {
//JOptionPane.showMessageDialog(null, "Deck has been generated."); //JOptionPane.showMessageDialog(null, "Deck has been generated.");
return tmp.getAbsolutePath(); return tmp.getAbsolutePath();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Couldn't generate deck. Try once again."); JOptionPane.showMessageDialog(null, "Couldn't generate deck. Try once again.");
} }
} }
return selectedColors; return selectedColors;
} }
/** /**
* Generates card pool * Generates card pool
*/ */
protected static void buildDeck() { protected static void buildDeck() {
List<ColoredManaSymbol> allowedColors = new ArrayList<ColoredManaSymbol>(); List<ColoredManaSymbol> allowedColors = new ArrayList<ColoredManaSymbol>();
selectedColors = selectedColors.toUpperCase(); selectedColors = selectedColors != null ? selectedColors.toUpperCase() : getRandomColors("X");
String format = (String)formats.getSelectedItem(); String format = (String) formats.getSelectedItem();
List<String> setsToUse = ConstructedFormats.getSetsByFormat(format); List<String> setsToUse = ConstructedFormats.getSetsByFormat(format);
if (setsToUse.isEmpty()) { if (setsToUse.isEmpty()) {
// use all // use all
setsToUse = CardsStorage.getSetCodes(); setsToUse = CardRepository.instance.getSetCodes();
} }
if (selectedColors.contains("X")) { if (selectedColors.contains("X")) {
selectedColors = getRandomColors(selectedColors); selectedColors = getRandomColors(selectedColors);
} }
for (int i = 0; i < selectedColors.length(); i++) { for (int i = 0; i < selectedColors.length(); i++) {
char c = selectedColors.charAt(i); char c = selectedColors.charAt(i);
allowedColors.add(ColoredManaSymbol.lookup(c)); allowedColors.add(ColoredManaSymbol.lookup(c));
} }
int cardPoolSize = SPELL_CARD_POOL_SIZE; int cardPoolSize = SPELL_CARD_POOL_SIZE;
if (selectedColors.length() > 2) { if (selectedColors.length() > 2) {
cardPoolSize += ADDITIONAL_CARDS_FOR_3_COLOR_DECKS; cardPoolSize += ADDITIONAL_CARDS_FOR_3_COLOR_DECKS;
} }
List<Card> spellCardPool = generateSpellCardPool(cardPoolSize, allowedColors, setsToUse); List<Card> spellCardPool = generateSpellCardPool(cardPoolSize, allowedColors, setsToUse);
List<Card> landCardPool = generateNonBasicLandCardPool(MAX_NON_BASIC_SOURCE, allowedColors, setsToUse); List<Card> landCardPool = generateNonBasicLandCardPool(MAX_NON_BASIC_SOURCE, allowedColors, setsToUse);
System.out.println("deck generator card pool: spells=" + spellCardPool.size() + ", lands=" + landCardPool.size()); System.out.println("deck generator card pool: spells=" + spellCardPool.size() + ", lands=" + landCardPool.size());
final List<String> setsToUseFinal = setsToUse; final List<String> setsToUseFinal = setsToUse;
deck = DeckBuilder.buildDeck(spellCardPool, allowedColors, landCardPool, new RateCallback() { deck = DeckBuilder.buildDeck(spellCardPool, allowedColors, landCardPool, new RateCallback() {
@Override @Override
public int rateCard(Card card) { public int rateCard(Card card) {
return CardsStorage.rateCard(card); return CardsStorage.rateCard(card);
} }
@Override @Override
public Card getBestBasicLand(ColoredManaSymbol color) { public Card getBestBasicLand(ColoredManaSymbol color) {
int tries = 100; int tries = 100;
@ -175,29 +180,36 @@ public class DeckGenerator {
} }
}); });
} }
private static String getRandomColors(String _selectedColors) { private static String getRandomColors(String _selectedColors) {
Random random = new Random();
List<Character> availableColors = new ArrayList();
availableColors.add('R');
availableColors.add('G');
availableColors.add('B');
availableColors.add('U');
availableColors.add('W');
StringBuilder generatedColors = new StringBuilder(); StringBuilder generatedColors = new StringBuilder();
Set<String> colors = new HashSet<String>(); int randomColors = 0;
for (int i = 0; i < _selectedColors.length(); i++) { for (int i = 0; i < _selectedColors.length(); i++) {
String color = getRandomColor() + ""; char currentColor = _selectedColors.charAt(i);
int retry = 100; if (currentColor != 'X') {
while (colors.contains(color)) { generatedColors.append(currentColor);
color = getRandomColor() + ""; availableColors.remove(new Character(currentColor));
retry--; } else {
if (retry <= 0) break; randomColors++;
} }
generatedColors.append(color);
colors.add(color);
} }
for (int i = 0; i < randomColors && !availableColors.isEmpty(); i++) {
int index = random.nextInt(availableColors.size());
generatedColors.append(availableColors.remove(index));
}
return generatedColors.toString(); return generatedColors.toString();
} }
private static char getRandomColor() {
Random r = new Random();
return colors.charAt(r.nextInt(colors.length()));
}
/** /**
* Generates card pool of cardsCount cards that have manacost of allowed colors. * Generates card pool of cardsCount cards that have manacost of allowed colors.
* *
@ -207,25 +219,22 @@ public class DeckGenerator {
*/ */
private static List<Card> generateSpellCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) { private static List<Card> generateSpellCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) {
List<Card> spellCardPool = new ArrayList<Card>(); List<Card> spellCardPool = new ArrayList<Card>();
int count = 0; CardCriteria spellCriteria = new CardCriteria();
List<Card> cardPool = new ArrayList<Card>(); spellCriteria.setCodes(setsToUse.toArray(new String[0]));
for (Card card : CardsStorage.getAllCards()) { spellCriteria.notTypes(CardType.LAND);
if (setsToUse.contains(card.getExpansionSetCode())) {
cardPool.add(card); List<CardInfo> cardPool = CardRepository.instance.findCards(spellCriteria);
}
}
int cardPoolCount = cardPool.size(); int cardPoolCount = cardPool.size();
Random random = new Random(); Random random = new Random();
if (cardPoolCount > 0) { if (cardPoolCount > 0) {
int tries = 0; int tries = 0;
int count = 0;
while (count < cardsCount) { while (count < cardsCount) {
Card card = cardPool.get(random.nextInt(cardPoolCount)); Card card = cardPool.get(random.nextInt(cardPoolCount)).getCard();
if (!card.getCardType().contains(CardType.LAND)) { if (cardFitsChosenColors(card, allowedColors)) {
if (cardFitsChosenColors(card, allowedColors)) { spellCardPool.add(card);
spellCardPool.add(card); count++;
count++;
}
} }
tries++; tries++;
if (tries > MAX_TRIES) { // to avoid infinite loop if (tries > MAX_TRIES) { // to avoid infinite loop
@ -235,10 +244,10 @@ public class DeckGenerator {
} else { } else {
throw new IllegalStateException("Not enough cards to generate deck."); throw new IllegalStateException("Not enough cards to generate deck.");
} }
return spellCardPool; return spellCardPool;
} }
/** /**
* Check that card can be played using chosen (allowed) colors. * Check that card can be played using chosen (allowed) colors.
* *
@ -264,7 +273,7 @@ public class DeckGenerator {
} }
return true; return true;
} }
/** /**
* Generates card pool of land cards that can produce allowed colors. * Generates card pool of land cards that can produce allowed colors.
* *
@ -274,25 +283,23 @@ public class DeckGenerator {
*/ */
private static List<Card> generateNonBasicLandCardPool(int landsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) { private static List<Card> generateNonBasicLandCardPool(int landsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) {
List<Card> nonBasicLandCardPool = new ArrayList<Card>(); List<Card> nonBasicLandCardPool = new ArrayList<Card>();
int count = 0; CardCriteria landCriteria = new CardCriteria();
List<Card> landCards = new ArrayList<Card>(); landCriteria.setCodes(setsToUse.toArray(new String[0]));
for (Card land : CardsStorage.getNonBasicLandCards()) { landCriteria.types(CardType.LAND);
if (setsToUse.contains(land.getExpansionSetCode())) { landCriteria.notSupertypes("Basic");
landCards.add(land); List<CardInfo> landCards = CardRepository.instance.findCards(landCriteria);
}
}
int allCount = landCards.size(); int allCount = landCards.size();
Random random = new Random(); Random random = new Random();
if (allCount > 0) { if (allCount > 0) {
int tries = 0; int tries = 0;
int count = 0;
while (count < landsCount) { while (count < landsCount) {
Card card = landCards.get(random.nextInt(allCount)); Card card = landCards.get(random.nextInt(allCount)).getCard();
if (!CardUtil.isBasicLand(card)) { if (cardCardProduceChosenColors(card, allowedColors)) {
if (cardCardProduceChosenColors(card, allowedColors)) { nonBasicLandCardPool.add(card);
nonBasicLandCardPool.add(card); count++;
count++;
}
} }
tries++; tries++;
if (tries > MAX_TRIES) { // to avoid infinite loop if (tries > MAX_TRIES) { // to avoid infinite loop
@ -301,10 +308,10 @@ public class DeckGenerator {
} }
} }
} }
return nonBasicLandCardPool; return nonBasicLandCardPool;
} }
/** /**
* Checks that chosen card can produce mana of specific color. * Checks that chosen card can produce mana of specific color.
* *
@ -324,7 +331,7 @@ public class DeckGenerator {
} }
return false; return false;
} }
/** /**
* Get random basic land that can produce specified color mana. * Get random basic land that can produce specified color mana.
* Random here means random set and collector id for the same mana producing land. * Random here means random set and collector id for the same mana producing land.
@ -348,38 +355,11 @@ public class DeckGenerator {
if (color.equals(ColoredManaSymbol.W)) { if (color.equals(ColoredManaSymbol.W)) {
return Sets.findCard("Plains", GENERATE_RANDOM_BASIC_LAND); return Sets.findCard("Plains", GENERATE_RANDOM_BASIC_LAND);
} }
return null; return null;
} }
protected static boolean isColoredMana(String symbol) { protected static boolean isColoredMana(String symbol) {
return symbol.equals("W") || symbol.equals("G") || symbol.equals("U") || symbol.equals("B") || symbol.equals("R") || symbol.contains("/"); return symbol.equals("W") || symbol.equals("G") || symbol.equals("U") || symbol.equals("B") || symbol.equals("R") || symbol.contains("/");
} }
}
public static void main(String[] args) {
Card selesnyaGuildMage = null;
for (Card card : CardsStorage.getAllCards()) {
if (card.getName().equals("Selesnya Guildmage")) {
selesnyaGuildMage = card;
break;
}
}
if (selesnyaGuildMage == null) {
throw new RuntimeException("Couldn't find card: Selesnya Guildmage");
}
List<ColoredManaSymbol> allowedColors = new ArrayList<ColoredManaSymbol>();
allowedColors.add(ColoredManaSymbol.lookup('B'));
allowedColors.add(ColoredManaSymbol.lookup('R'));
System.out.println(DeckGenerator.cardFitsChosenColors(selesnyaGuildMage, allowedColors));
allowedColors.clear();
allowedColors = new ArrayList<ColoredManaSymbol>();
allowedColors.add(ColoredManaSymbol.lookup('G'));
System.out.println(DeckGenerator.cardFitsChosenColors(selesnyaGuildMage, allowedColors));
allowedColors.clear();
allowedColors = new ArrayList<ColoredManaSymbol>();
allowedColors.add(ColoredManaSymbol.lookup('W'));
System.out.println(DeckGenerator.cardFitsChosenColors(selesnyaGuildMage, allowedColors));
}
}