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

Computer generated decks for tournaments use now also appropriate basic lands for the cards included in the deck.

This commit is contained in:
LevelX2 2013-02-06 00:50:00 +01:00
parent a9c1746b02
commit fcd24572dd
4 changed files with 82 additions and 36 deletions
Mage.Client/src/main/java/mage/client/dialog
Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai
Mage/src/mage/cards

View file

@ -37,6 +37,7 @@ package mage.client.dialog;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import javax.swing.JLayeredPane;
import mage.Constants;
import mage.cards.Card;
@ -55,7 +56,7 @@ import mage.client.MageFrame;
public class AddLandDialog extends MageDialog {
private Deck deck;
private List<String> setCodesland;
private Set<String> setCodesland;
/** Creates new form AddLandDialog */
public AddLandDialog() {
@ -65,33 +66,7 @@ public class AddLandDialog extends MageDialog {
public void showDialog(Deck deck) {
this.deck = deck;
// find setCodes with basic lands from cards of the deck
List<String> setCodes = new LinkedList<String>();
for (Card card: this.deck.getCards()) {
if (!setCodes.contains(card.getExpansionSetCode())) {
setCodes.add(card.getExpansionSetCode());
}
}
for (Card card: this.deck.getSideboard()) {
if (!setCodes.contains(card.getExpansionSetCode())) {
setCodes.add(card.getExpansionSetCode());
}
}
List<String> landSets = new LinkedList<String>();
if (!setCodes.isEmpty()) {
// Add parent sets with the basic lands if the setlist don't include them
for (String setCode: setCodes) {
ExpansionSet expansionSet = Sets.findSet(setCode);
if (expansionSet.hasBasicLands()) {
landSets.add(setCode);
} else if (expansionSet.getParentSet() != null && !landSets.contains(expansionSet.getParentSet().getCode())) {
landSets.add(expansionSet.getParentSet().getCode());
}
}
}
this.setCodesland = landSets;
this.setCodesland = Sets.getSetsWithBasicLandsAsCodes(deck.getExpansionSetCodes());
MageFrame.getDesktop().add(this, JLayeredPane.PALETTE_LAYER);
this.setVisible(true);

View file

@ -84,6 +84,8 @@ import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import mage.cards.Sets;
import mage.cards.repository.CardCriteria;
/**
*
@ -1170,7 +1172,15 @@ public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> i
private static void addBasicLands(Deck deck, String landName, int number) {
Random random = new Random();
List<CardInfo> cards = CardRepository.instance.findCards(landName);
Set<String> landSets = Sets.getSetsWithBasicLandsAsCodes(deck.getExpansionSetCodes());
CardCriteria criteria = new CardCriteria();
if (!landSets.isEmpty()) {
criteria.setCodes(landSets.toArray(new String[landSets.size()]));
}
criteria.rarities(Constants.Rarity.LAND).name(landName);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
if (cards.isEmpty()) {
return;
}
@ -1208,29 +1218,46 @@ public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> i
mana.add(card.getManaCost().getMana());
}
double total = mana.getBlack() + mana.getBlue() + mana.getGreen() + mana.getRed() + mana.getWhite();
int mostLand = 0;
String mostLandName = "Forest";
if (mana.getGreen() > 0) {
int number = (int) Math.round(mana.getGreen() / total * 17);
addBasicLands(deck, "Forest", number);
mostLand = number;
}
if (mana.getBlack() > 0) {
int number = (int) Math.round(mana.getBlack() / total * 17);
addBasicLands(deck, "Swamp", number);
if (number > mostLand) {
mostLand = number;
mostLandName = "Swamp";
}
}
if (mana.getBlue() > 0) {
int number = (int) Math.round(mana.getBlue() / total * 17);
addBasicLands(deck, "Island", number);
if (number > mostLand) {
mostLand = number;
mostLandName = "Island";
}
}
if (mana.getWhite() > 0) {
int number = (int) Math.round(mana.getWhite() / total * 17);
addBasicLands(deck, "Plains", number);
if (number > mostLand) {
mostLand = number;
mostLandName = "Plains";
}
}
if (mana.getRed() > 0) {
int number = (int) Math.round(mana.getRed() / total * 17);
addBasicLands(deck, "Mountain", number);
if (number > mostLand) {
mostLandName = "Plains";
}
}
//TODO: improve this
addBasicLands(deck, "Forest", 40 - deck.getCards().size());
addBasicLands(deck, mostLandName, 40 - deck.getCards().size());
return deck;
}

View file

@ -194,5 +194,28 @@ public class Sets extends HashMap<String, ExpansionSet> {
return boosterSets.toArray(new ExpansionSet[0]);
}
/**
* Gives back the set codes from the sets that include basic lands.
* If the input set itself does not incluse basic lands, but it has a parent set,
* only this parent set code is added to the return sets.
*
* @param setCodes
* @return - setCodes that have basic lands
*/
public static Set<String> getSetsWithBasicLandsAsCodes(Set<String> setCodes) {
Set<String> landSets = new LinkedHashSet<String>();
if (setCodes != null && !setCodes.isEmpty()) {
// Add parent sets with the basic lands if the setlist don't include them
for (String setCode: setCodes) {
ExpansionSet expansionSet = Sets.findSet(setCode);
if (expansionSet.hasBasicLands()) {
landSets.add(setCode);
} else if (expansionSet.getParentSet() != null && !landSets.contains(expansionSet.getParentSet().getCode())) {
landSets.add(expansionSet.getParentSet().getCode());
}
}
}
return landSets;
}
}

View file

@ -28,10 +28,12 @@
package mage.cards.decks;
import mage.cards.*;
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import mage.cards.*;
import mage.game.GameException;
public class Deck implements Serializable {
@ -49,20 +51,24 @@ public class Deck implements Serializable {
deck.setName(deckCardLists.getName());
for (String cardName: deckCardLists.getCards()) {
Card card = CardImpl.createCard(cardName);
if (card != null)
if (card != null) {
deck.cards.add(CardImpl.createCard(cardName));
}
else {
if (!ignoreErrors)
if (!ignoreErrors) {
throw new GameException("Error loading card - " + cardName + " for deck - " + deck.getName());
}
}
}
for (String cardName: deckCardLists.getSideboard()) {
Card card = CardImpl.createCard(cardName);
if (card != null)
if (card != null) {
deck.sideboard.add(CardImpl.createCard(cardName));
}
else {
if (!ignoreErrors)
if (!ignoreErrors) {
throw new GameException("Error loading card - " + cardName + " for deck - " + deck.getName());
}
}
}
@ -83,6 +89,21 @@ public class Deck implements Serializable {
return deckCardLists;
}
public Set<String> getExpansionSetCodes() {
Set<String> sets = new LinkedHashSet<String>();
for (Card card : getCards()) {
if (!sets.contains(card.getExpansionSetCode())) {
sets.add(card.getExpansionSetCode());
}
}
for (Card card : getSideboard()) {
if (!sets.contains(card.getExpansionSetCode())) {
sets.add(card.getExpansionSetCode());
}
}
return sets;
}
/**
* @return the name
*/