diff --git a/forge-ai/src/main/java/forge/ai/simulation/SpellAbilityChoicesIterator.java b/forge-ai/src/main/java/forge/ai/simulation/SpellAbilityChoicesIterator.java index 75b143080b9..d616baaa55d 100644 --- a/forge-ai/src/main/java/forge/ai/simulation/SpellAbilityChoicesIterator.java +++ b/forge-ai/src/main/java/forge/ai/simulation/SpellAbilityChoicesIterator.java @@ -35,7 +35,7 @@ public class SpellAbilityChoicesIterator { this.controller = controller; } - public List chooseModesForAbility(List choices, final int min, final int num, boolean allowRepeat) { + public List chooseModesForAbility(List choices, int min, int num, boolean allowRepeat) { if (modeIterator == null) { // TODO: Need to skip modes that are invalid (e.g. targets don't exist)! // TODO: Do we need to do something special to support cards that have extra costs @@ -45,42 +45,7 @@ public class SpellAbilityChoicesIterator { } else { // Note: When allowRepeat is true, it does result in many possibilities being tried. // We should ideally prune some of those at a higher level. - final int numChoices = choices.size(); - modeIterator = new Iterator() { - int[] indexes = new int[min]; - - @Override - public boolean hasNext() { - return indexes != null; - } - - // Note: This returns a new int[] array and doesn't modify indexes in place, - // since that gets returned to the caller. - private int[] getNextIndexes() { - for (int i = indexes.length - 1; i >= 0; i--) { - if (indexes[i] < numChoices - 1) { - int[] nextIndexes = new int[indexes.length]; - System.arraycopy(indexes, 0, nextIndexes, 0, i); - nextIndexes[i] = indexes[i] + 1; - return nextIndexes; - } - } - if (indexes.length < num) { - return new int[indexes.length + 1]; - } - return null; - } - - @Override - public int[] next() { - if (indexes == null) { - throw new NoSuchElementException(); - } - int[] result = indexes; - indexes = getNextIndexes(); - return result; - } - }; + modeIterator = new AllowRepeatModesIterator(choices.size(), min, num); } selectedModes = modeIterator.next(); advancedToNextMode = true; @@ -211,7 +176,6 @@ public class SpellAbilityChoicesIterator { return false; } - public static List getModeCombination(List choices, int[] modeIndexes) { ArrayList modes = new ArrayList(); for (int modeIndex : modeIndexes) { @@ -219,4 +183,48 @@ public class SpellAbilityChoicesIterator { } return modes; } + + private static class AllowRepeatModesIterator implements Iterator { + private int numChoices; + private int max; + private int[] indexes; + + public AllowRepeatModesIterator(int numChoices, int min, int max) { + this.numChoices = numChoices; + this.max = max; + this.indexes = new int[min]; + } + + @Override + public boolean hasNext() { + return indexes != null; + } + + // Note: This returns a new int[] array and doesn't modify indexes in place, + // since that gets returned to the caller. + private int[] getNextIndexes() { + for (int i = indexes.length - 1; i >= 0; i--) { + if (indexes[i] < numChoices - 1) { + int[] nextIndexes = new int[indexes.length]; + System.arraycopy(indexes, 0, nextIndexes, 0, i); + nextIndexes[i] = indexes[i] + 1; + return nextIndexes; + } + } + if (indexes.length < max) { + return new int[indexes.length + 1]; + } + return null; + } + + @Override + public int[] next() { + if (indexes == null) { + throw new NoSuchElementException(); + } + int[] result = indexes; + indexes = getNextIndexes(); + return result; + } + } }