added missing targeting cases

This commit is contained in:
BetaSteward 2011-03-14 21:57:27 -04:00
parent 315fb7f24e
commit 86d2086b70

View file

@ -226,20 +226,31 @@ public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> i
if (!target.isRequired()) if (!target.isRequired())
return false; return false;
} }
if (target instanceof TargetDiscard) { if (target instanceof TargetDiscard || target instanceof TargetCardInHand) {
findPlayables(game); if (outcome.isGood()) {
if (unplayable.size() > 0) { Card card = pickBestCard(new ArrayList<Card>(hand.getCards(game)), null);
for (int i = unplayable.size() - 1; i >= 0; i--) { if (card != null) {
if (target.canTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game)) { if (target.canTarget(card.getId(), source, game)) {
target.addTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game); target.addTarget(card.getId(), source, game);
return true; return true;
} }
} }
} }
if (hand.size() > 0) { else {
if (target.canTarget(hand.toArray(new UUID[0])[0], source, game)) { findPlayables(game);
target.addTarget(hand.toArray(new UUID[0])[0], source, game); if (unplayable.size() > 0) {
return true; for (int i = unplayable.size() - 1; i >= 0; i--) {
if (target.canTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game)) {
target.addTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game);
return true;
}
}
}
if (hand.size() > 0) {
if (target.canTarget(hand.toArray(new UUID[0])[0], source, game)) {
target.addTarget(hand.toArray(new UUID[0])[0], source, game);
return true;
}
} }
} }
if (!target.isRequired()) if (!target.isRequired())
@ -308,28 +319,57 @@ public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> i
return false; return false;
} }
if (target instanceof TargetCardInGraveyard) { if (target instanceof TargetCardInGraveyard) {
//TODO: implement List<Card> cards = new ArrayList<Card>();
logger.error("Needs to be implemented"); for (Player player: game.getPlayers().values()) {
return false; cards.addAll(player.getGraveyard().getCards(game));
} }
if (target instanceof TargetCardInHand) { Card card = pickTarget(cards, outcome, target, source, game);
//TODO: implement if (card != null) {
logger.error("Needs to be implemented"); target.addTarget(card.getId(), source, game);
return false; return true;
}
if (!target.isRequired())
return false;
} }
if (target instanceof TargetCardInLibrary) { if (target instanceof TargetCardInLibrary) {
//TODO: implement List<Card> cards = new ArrayList<Card>(game.getPlayer(playerId).getLibrary().getCards(game));
logger.error("Needs to be implemented"); Card card = pickTarget(cards, outcome, target, source, game);
return false; if (card != null) {
target.addTarget(card.getId(), source, game);
return true;
}
if (!target.isRequired())
return false;
} }
if (target instanceof TargetCardInYourGraveyard) { if (target instanceof TargetCardInYourGraveyard) {
//TODO: implement List<Card> cards = new ArrayList<Card>(game.getPlayer(playerId).getGraveyard().getCards(game));
logger.error("Needs to be implemented"); Card card = pickTarget(cards, outcome, target, source, game);
return false; if (card != null) {
target.addTarget(card.getId(), source, game);
return true;
}
if (!target.isRequired())
return false;
} }
throw new IllegalStateException("Target wasn't handled. class:" + target.getClass().toString()); throw new IllegalStateException("Target wasn't handled. class:" + target.getClass().toString());
} }
protected Card pickTarget(List<Card> cards, Outcome outcome, Target target, Ability source, Game game) {
Card card = null;
while (!cards.isEmpty()) {
if (outcome.isGood()) {
card = pickBestCard(cards, null);
}
else {
card = pickWorstCard(cards, null);
}
if (target.canTarget(card.getId(), source, game)) {
return card;
}
cards.remove(card);
}
return null;
}
@Override @Override
public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) { public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) {
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
@ -918,21 +958,46 @@ public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> i
tournament.submitDeck(playerId, deck); tournament.submitDeck(playerId, deck);
} }
public Card pickBestCard(List<Card> cards, List<Constants.ColoredManaSymbol> chosenColors) {
if (cards.size() == 0) {
return null;
}
Card bestCard = null;
int maxScore = 0;
for (Card card : cards) {
int score = RateCard.rateCard(card, chosenColors);
if (bestCard == null || score > maxScore) {
maxScore = score;
bestCard = card;
}
}
return bestCard;
}
public Card pickWorstCard(List<Card> cards, List<Constants.ColoredManaSymbol> chosenColors) {
if (cards.size() == 0) {
return null;
}
Card worstCard = null;
int minScore = Integer.MAX_VALUE;
for (Card card : cards) {
int score = RateCard.rateCard(card, chosenColors);
if (worstCard == null || score < minScore) {
minScore = score;
worstCard = card;
}
}
return worstCard;
}
@Override @Override
public void pickCard(List<Card> cards, Deck deck, Draft draft) { public void pickCard(List<Card> cards, Deck deck, Draft draft) {
if (cards.size() == 0) { if (cards.size() == 0) {
throw new IllegalArgumentException("No cards to pick from."); throw new IllegalArgumentException("No cards to pick from.");
} }
try { try {
Card bestCard = null; Card bestCard = pickBestCard(cards, chosenColors);
int maxScore = 0; int maxScore = RateCard.rateCard(bestCard, chosenColors);
for (Card card : cards) {
int score = RateCard.rateCard(card, chosenColors);
if (bestCard == null || score > maxScore) {
maxScore = score;
bestCard = card;
}
}
String colors = "not chosen yet"; String colors = "not chosen yet";
// remember card if colors are not chosen yet // remember card if colors are not chosen yet
if (chosenColors == null) { if (chosenColors == null) {