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

View file

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