[Simulated AI] Move out anon class to make the code more readable.

This commit is contained in:
Myrd
2017-01-02 00:53:21 +00:00
parent 7f068eb87a
commit d45da5f3db

View File

@@ -35,7 +35,7 @@ public class SpellAbilityChoicesIterator {
this.controller = controller;
}
public List<AbilitySub> chooseModesForAbility(List<AbilitySub> choices, final int min, final int num, boolean allowRepeat) {
public List<AbilitySub> chooseModesForAbility(List<AbilitySub> 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[]>() {
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<AbilitySub> getModeCombination(List<AbilitySub> choices, int[] modeIndexes) {
ArrayList<AbilitySub> modes = new ArrayList<AbilitySub>();
for (int modeIndex : modeIndexes) {
@@ -219,4 +183,48 @@ public class SpellAbilityChoicesIterator {
}
return modes;
}
private static class AllowRepeatModesIterator implements Iterator<int[]> {
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;
}
}
}