Fix to #1128 Deck generation throws IndexOutOfBoundsException.

Reserve cards now counted correctly.
Fixed reserve card adding algorithm to stop trying to add cards out of range.
This commit is contained in:
Simown 2015-07-19 12:11:03 +01:00
parent 4324a6a683
commit cedf16806e
2 changed files with 13 additions and 9 deletions

View file

@ -193,6 +193,7 @@ public class DeckGenerator {
Random random = new Random();
int count = 0;
int reservesAdded = 0;
boolean added;
if (retrievedCount > 0 && retrievedCount >= spellCount) {
int tries = 0;
while (count < spellCount) {
@ -208,9 +209,10 @@ public class DeckGenerator {
count++;
}
} else {
if (reservesAdded < genPool.getDeckSize() / 2) {
genPool.tryAddReserve(card, cardCMC);
reservesAdded++;
if (reservesAdded < (genPool.getDeckSize() / 2)) {
added = genPool.tryAddReserve(card, cardCMC);
if(added)
reservesAdded++;
}
}
}

View file

@ -176,13 +176,15 @@ public class DeckGeneratorPool
* @param card the card to add
* @param cardCMC the converted mana cost of the card
*/
public void tryAddReserve(Card card, int cardCMC) {
public boolean tryAddReserve(Card card, int cardCMC) {
// Only cards with CMC < 7 and don't already exist in the deck
// can be added to our reserve pool as not to overwhelm the curve
// with high CMC cards and duplicates.
if(cardCMC < 7 && getCardCount(card.getName()) == 0) {
this.reserveSpells.add(card);
return true;
}
return false;
}
/**
@ -386,13 +388,13 @@ public class DeckGeneratorPool
List<Card> spellsToAdd = new ArrayList<>(spellsNeeded);
// Initial reservoir
for(int i = 0; i < spellsNeeded-1; i++)
for(int i = 0; i < spellsNeeded; i++)
spellsToAdd.add(reserveSpells.get(i));
for(int j = spellsNeeded+1; j < reserveSpells.size()-1; j++) {
int index = random.nextInt(j);
Card randomCard = reserveSpells.get(index);
if (index < j && isValidSpellCard(randomCard)) {
for(int i = spellsNeeded+1; i < reserveSpells.size()-1; i++) {
int j = random.nextInt(i);
Card randomCard = reserveSpells.get(j);
if (isValidSpellCard(randomCard) && j < spellsToAdd.size()) {
spellsToAdd.set(j, randomCard);
}
}