mirror of
https://github.com/correl/mage.git
synced 2025-04-11 09:11:12 -09:00
added random deck test
This commit is contained in:
parent
aed200bc11
commit
def31fef83
2 changed files with 128 additions and 13 deletions
|
@ -38,9 +38,13 @@ import java.util.logging.Level;
|
|||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import mage.Constants.CardType;
|
||||
import mage.Constants.ColoredManaSymbol;
|
||||
import mage.Mana;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.CardImpl;
|
||||
import mage.cards.ExpansionSet;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.util.Logging;
|
||||
|
||||
|
@ -53,6 +57,7 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
private final static Logger logger = Logging.getLogger(Sets.class.getName());
|
||||
private static final Sets fINSTANCE = new Sets();
|
||||
private static Set<String> names;
|
||||
private static List<Card> cards;
|
||||
protected static Random rnd = new Random();
|
||||
|
||||
public static Sets getInstance() {
|
||||
|
@ -61,6 +66,7 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
|
||||
private Sets() {
|
||||
names = new TreeSet<String>();
|
||||
cards = new ArrayList<Card>();
|
||||
this.addSet(AlaraReborn.getInstance());
|
||||
this.addSet(Conflux.getInstance());
|
||||
this.addSet(Dissension.getInstance());
|
||||
|
@ -83,6 +89,7 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
private void addSet(ExpansionSet set) {
|
||||
this.put(set.getCode(), set);
|
||||
for (Card card: set.getCards()) {
|
||||
cards.add(card);
|
||||
names.add(card.getName());
|
||||
}
|
||||
}
|
||||
|
@ -91,6 +98,97 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
return names;
|
||||
}
|
||||
|
||||
public static Card getRandomCard() {
|
||||
return cards.get(rnd.nextInt(cards.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates card pool of cardsCount cards that have manacost of allowed colors.
|
||||
*
|
||||
* @param cardsCount
|
||||
* @param allowedColors
|
||||
* @return
|
||||
*/
|
||||
public static List<Card> generateRandomCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors) {
|
||||
List<Card> cardPool = new ArrayList<Card>();
|
||||
|
||||
int count = 0;
|
||||
int tries = 0;
|
||||
while (count < cardsCount) {
|
||||
Card card = getRandomCard();
|
||||
if (!card.getCardType().contains(CardType.LAND)) {
|
||||
if (cardFitsChosenColors(card, allowedColors)) {
|
||||
cardPool.add(card);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
if (tries > 4096) { // to avoid infinite loop
|
||||
throw new IllegalStateException("Not enough cards for chosen colors to generate deck: " + allowedColors);
|
||||
}
|
||||
}
|
||||
|
||||
return cardPool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that card can be played using chosen (allowed) colors.
|
||||
*
|
||||
* @param card
|
||||
* @param allowedColors
|
||||
* @return
|
||||
*/
|
||||
private static boolean cardFitsChosenColors(Card card, List<ColoredManaSymbol> allowedColors) {
|
||||
if (card.getCardType().contains(CardType.LAND)) {
|
||||
if (!card.getSupertype().contains("Basic")) {
|
||||
int score = 0;
|
||||
for (Mana mana : card.getMana()) {
|
||||
for (ColoredManaSymbol color : allowedColors) {
|
||||
score += mana.getColor(color);
|
||||
}
|
||||
}
|
||||
if (score > 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (String symbol : card.getManaCost().getSymbols()) {
|
||||
boolean found = false;
|
||||
symbol = symbol.replace("{", "").replace("}", "");
|
||||
if (isColoredMana(symbol)) {
|
||||
for (ColoredManaSymbol allowed : allowedColors) {
|
||||
if (allowed.toString().equals(symbol)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static boolean isColoredMana(String symbol) {
|
||||
return symbol.equals("W") || symbol.equals("G") || symbol.equals("U") || symbol.equals("B") || symbol.equals("R");
|
||||
}
|
||||
|
||||
public static Deck generateDeck() {
|
||||
List<ColoredManaSymbol> allowedColors = new ArrayList<ColoredManaSymbol>();
|
||||
int numColors = rnd.nextInt(2) + 1;
|
||||
int cardPoolSize = 60;
|
||||
if (numColors > 2) {
|
||||
cardPoolSize += 20;
|
||||
}
|
||||
Deck deck = new Deck();
|
||||
|
||||
return deck;
|
||||
}
|
||||
|
||||
public static Card findCard(String name) {
|
||||
for (ExpansionSet set: fINSTANCE.values()) {
|
||||
Card card = set.findCard(name);
|
||||
|
|
|
@ -1,36 +1,40 @@
|
|||
package org.mage.test.serverside;
|
||||
|
||||
import mage.Constants;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.game.Game;
|
||||
import mage.game.GameException;
|
||||
import mage.game.GameOptions;
|
||||
import mage.game.TwoPlayerDuel;
|
||||
import mage.game.permanent.PermanentCard;
|
||||
import mage.players.Player;
|
||||
import mage.server.game.PlayerFactory;
|
||||
import mage.sets.Sets;
|
||||
import org.junit.Test;
|
||||
import org.mage.test.serverside.base.MageTestBase;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import mage.Constants.ColoredManaSymbol;
|
||||
import mage.cards.Card;
|
||||
import mage.player.ai.ComputerPlayer;
|
||||
|
||||
/**
|
||||
* @author ayratn
|
||||
*/
|
||||
public class PlayGameTest extends MageTestBase {
|
||||
|
||||
private static List<String> colorChoices = Arrays.asList("bu","bg","br","bw","ug","ur","uw","gr","gw","rw","bur","buw","bug","brg","brw","bgw","wur","wug","wrg","rgu");
|
||||
|
||||
@Test
|
||||
public void playOneGame() throws GameException, FileNotFoundException, IllegalArgumentException {
|
||||
Game game = new TwoPlayerDuel(Constants.MultiplayerAttackOption.LEFT, Constants.RangeOfInfluence.ALL);
|
||||
|
||||
Player computerA = createPlayer("ComputerA", "Computer - minimax hybrid");
|
||||
// Player computerA = createPlayer("ComputerA", "Computer - mad");
|
||||
Deck deck = Deck.load(Sets.loadDeck("RB Aggro.dck"));
|
||||
// Deck deck = Deck.load(Sets.loadDeck("RB Aggro.dck"));
|
||||
Deck deck = generateRandomDeck();
|
||||
|
||||
if (deck.getCards().size() < 40) {
|
||||
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size());
|
||||
|
@ -40,18 +44,19 @@ public class PlayGameTest extends MageTestBase {
|
|||
|
||||
Player computerB = createPlayer("ComputerB", "Computer - minimax hybrid");
|
||||
// Player computerB = createPlayer("ComputerB", "Computer - mad");
|
||||
Deck deck2 = Deck.load(Sets.loadDeck("RB Aggro.dck"));
|
||||
// Deck deck2 = Deck.load(Sets.loadDeck("RB Aggro.dck"));
|
||||
Deck deck2 = generateRandomDeck();
|
||||
if (deck2.getCards().size() < 40) {
|
||||
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size());
|
||||
}
|
||||
game.addPlayer(computerB, deck2);
|
||||
game.loadCards(deck2.getCards(), computerB.getId());
|
||||
|
||||
parseScenario("scenario8.txt");
|
||||
game.cheat(computerA.getId(), commandsA);
|
||||
game.cheat(computerA.getId(), libraryCardsA, handCardsA, battlefieldCardsA, graveyardCardsA);
|
||||
game.cheat(computerB.getId(), commandsB);
|
||||
game.cheat(computerB.getId(), libraryCardsB, handCardsB, battlefieldCardsB, graveyardCardsB);
|
||||
// parseScenario("scenario1.txt");
|
||||
// game.cheat(computerA.getId(), commandsA);
|
||||
// game.cheat(computerA.getId(), libraryCardsA, handCardsA, battlefieldCardsA, graveyardCardsA);
|
||||
// game.cheat(computerB.getId(), commandsB);
|
||||
// game.cheat(computerB.getId(), libraryCardsB, handCardsB, battlefieldCardsB, graveyardCardsB);
|
||||
|
||||
//boolean testMode = false;
|
||||
boolean testMode = true;
|
||||
|
@ -68,4 +73,16 @@ public class PlayGameTest extends MageTestBase {
|
|||
throw new RuntimeException("Lost :(");
|
||||
}*/
|
||||
}
|
||||
|
||||
private Deck generateRandomDeck() {
|
||||
String selectedColors = colorChoices.get(new Random().nextInt(colorChoices.size())).toUpperCase();
|
||||
List<ColoredManaSymbol> allowedColors = new ArrayList<ColoredManaSymbol>();
|
||||
logger.info("Building deck with colors: " + selectedColors);
|
||||
for (int i = 0; i < selectedColors.length(); i++) {
|
||||
char c = selectedColors.charAt(i);
|
||||
allowedColors.add(ColoredManaSymbol.lookup(c));
|
||||
}
|
||||
List<Card> cardPool = Sets.generateRandomCardPool(45, allowedColors);
|
||||
return ComputerPlayer.buildDeck(cardPool, allowedColors);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue