mirror of
https://github.com/correl/mage.git
synced 2025-04-07 17:00:08 -09:00
Used CardRepository for DeckGenerator
Rewritten the getRandomColors method
This commit is contained in:
parent
7ea23a1ba1
commit
ca912d4e03
1 changed files with 93 additions and 113 deletions
|
@ -5,22 +5,27 @@ import mage.Constants.ColoredManaSymbol;
|
|||
import mage.Mana;
|
||||
import mage.cards.Card;
|
||||
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.cards.CardsStorage;
|
||||
import mage.client.util.gui.ColorsChooser;
|
||||
import mage.client.util.sets.ConstructedFormats;
|
||||
import mage.interfaces.rate.RateCallback;
|
||||
import mage.sets.Sets;
|
||||
import mage.utils.CardUtil;
|
||||
import mage.utils.DeckBuilder;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Generates random card pool and builds a deck.
|
||||
|
@ -44,8 +49,6 @@ public class DeckGenerator {
|
|||
private static Deck deck = new Deck();
|
||||
private static final int ADDITIONAL_CARDS_FOR_3_COLOR_DECKS = 20;
|
||||
|
||||
private static String colors = "GWUBR";
|
||||
|
||||
/**
|
||||
* Opens color chooser dialog. Generates deck.
|
||||
* Saves generated deck and use it as selected deck to play.
|
||||
|
@ -84,6 +87,7 @@ public class DeckGenerator {
|
|||
|
||||
final JButton btnGenerate = new JButton("Ok");
|
||||
btnGenerate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
btnGenerate.setEnabled(false);
|
||||
colorsChooser.setEnabled(false);
|
||||
|
@ -94,6 +98,7 @@ public class DeckGenerator {
|
|||
});
|
||||
final JButton btnCancel = new JButton("Cancel");
|
||||
btnCancel.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
dlg.setVisible(false);
|
||||
selectedColors = null;
|
||||
|
@ -115,7 +120,6 @@ public class DeckGenerator {
|
|||
//JOptionPane.showMessageDialog(null, "Deck has been generated.");
|
||||
return tmp.getAbsolutePath();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JOptionPane.showMessageDialog(null, "Couldn't generate deck. Try once again.");
|
||||
}
|
||||
}
|
||||
|
@ -128,13 +132,13 @@ public class DeckGenerator {
|
|||
*/
|
||||
protected static void buildDeck() {
|
||||
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);
|
||||
if (setsToUse.isEmpty()) {
|
||||
// use all
|
||||
setsToUse = CardsStorage.getSetCodes();
|
||||
setsToUse = CardRepository.instance.getSetCodes();
|
||||
}
|
||||
|
||||
if (selectedColors.contains("X")) {
|
||||
|
@ -162,6 +166,7 @@ public class DeckGenerator {
|
|||
public int rateCard(Card card) {
|
||||
return CardsStorage.rateCard(card);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Card getBestBasicLand(ColoredManaSymbol color) {
|
||||
int tries = 100;
|
||||
|
@ -177,25 +182,32 @@ public class DeckGenerator {
|
|||
}
|
||||
|
||||
private static String getRandomColors(String _selectedColors) {
|
||||
StringBuilder generatedColors = new StringBuilder();
|
||||
Set<String> colors = new HashSet<String>();
|
||||
for (int i = 0; i < _selectedColors.length(); i++) {
|
||||
String color = getRandomColor() + "";
|
||||
int retry = 100;
|
||||
while (colors.contains(color)) {
|
||||
color = getRandomColor() + "";
|
||||
retry--;
|
||||
if (retry <= 0) break;
|
||||
}
|
||||
generatedColors.append(color);
|
||||
colors.add(color);
|
||||
}
|
||||
return generatedColors.toString();
|
||||
}
|
||||
Random random = new Random();
|
||||
List<Character> availableColors = new ArrayList();
|
||||
availableColors.add('R');
|
||||
availableColors.add('G');
|
||||
availableColors.add('B');
|
||||
availableColors.add('U');
|
||||
availableColors.add('W');
|
||||
|
||||
private static char getRandomColor() {
|
||||
Random r = new Random();
|
||||
return colors.charAt(r.nextInt(colors.length()));
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -208,24 +220,21 @@ public class DeckGenerator {
|
|||
private static List<Card> generateSpellCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) {
|
||||
List<Card> spellCardPool = new ArrayList<Card>();
|
||||
|
||||
int count = 0;
|
||||
List<Card> cardPool = new ArrayList<Card>();
|
||||
for (Card card : CardsStorage.getAllCards()) {
|
||||
if (setsToUse.contains(card.getExpansionSetCode())) {
|
||||
cardPool.add(card);
|
||||
}
|
||||
}
|
||||
CardCriteria spellCriteria = new CardCriteria();
|
||||
spellCriteria.setCodes(setsToUse.toArray(new String[0]));
|
||||
spellCriteria.notTypes(CardType.LAND);
|
||||
|
||||
List<CardInfo> cardPool = CardRepository.instance.findCards(spellCriteria);
|
||||
int cardPoolCount = cardPool.size();
|
||||
Random random = new Random();
|
||||
if (cardPoolCount > 0) {
|
||||
int tries = 0;
|
||||
int count = 0;
|
||||
while (count < cardsCount) {
|
||||
Card card = cardPool.get(random.nextInt(cardPoolCount));
|
||||
if (!card.getCardType().contains(CardType.LAND)) {
|
||||
if (cardFitsChosenColors(card, allowedColors)) {
|
||||
spellCardPool.add(card);
|
||||
count++;
|
||||
}
|
||||
Card card = cardPool.get(random.nextInt(cardPoolCount)).getCard();
|
||||
if (cardFitsChosenColors(card, allowedColors)) {
|
||||
spellCardPool.add(card);
|
||||
count++;
|
||||
}
|
||||
tries++;
|
||||
if (tries > MAX_TRIES) { // to avoid infinite loop
|
||||
|
@ -275,24 +284,22 @@ public class DeckGenerator {
|
|||
private static List<Card> generateNonBasicLandCardPool(int landsCount, List<ColoredManaSymbol> allowedColors, List<String> setsToUse) {
|
||||
List<Card> nonBasicLandCardPool = new ArrayList<Card>();
|
||||
|
||||
int count = 0;
|
||||
List<Card> landCards = new ArrayList<Card>();
|
||||
for (Card land : CardsStorage.getNonBasicLandCards()) {
|
||||
if (setsToUse.contains(land.getExpansionSetCode())) {
|
||||
landCards.add(land);
|
||||
}
|
||||
}
|
||||
CardCriteria landCriteria = new CardCriteria();
|
||||
landCriteria.setCodes(setsToUse.toArray(new String[0]));
|
||||
landCriteria.types(CardType.LAND);
|
||||
landCriteria.notSupertypes("Basic");
|
||||
List<CardInfo> landCards = CardRepository.instance.findCards(landCriteria);
|
||||
|
||||
int allCount = landCards.size();
|
||||
Random random = new Random();
|
||||
if (allCount > 0) {
|
||||
int tries = 0;
|
||||
int count = 0;
|
||||
while (count < landsCount) {
|
||||
Card card = landCards.get(random.nextInt(allCount));
|
||||
if (!CardUtil.isBasicLand(card)) {
|
||||
if (cardCardProduceChosenColors(card, allowedColors)) {
|
||||
nonBasicLandCardPool.add(card);
|
||||
count++;
|
||||
}
|
||||
Card card = landCards.get(random.nextInt(allCount)).getCard();
|
||||
if (cardCardProduceChosenColors(card, allowedColors)) {
|
||||
nonBasicLandCardPool.add(card);
|
||||
count++;
|
||||
}
|
||||
tries++;
|
||||
if (tries > MAX_TRIES) { // to avoid infinite loop
|
||||
|
@ -355,31 +362,4 @@ public class DeckGenerator {
|
|||
protected static boolean isColoredMana(String symbol) {
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue