mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
[Simulated AI] Move out anon class to make the code more readable.
This commit is contained in:
@@ -35,7 +35,7 @@ public class SpellAbilityChoicesIterator {
|
|||||||
this.controller = controller;
|
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) {
|
if (modeIterator == null) {
|
||||||
// TODO: Need to skip modes that are invalid (e.g. targets don't exist)!
|
// 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
|
// TODO: Do we need to do something special to support cards that have extra costs
|
||||||
@@ -45,42 +45,7 @@ public class SpellAbilityChoicesIterator {
|
|||||||
} else {
|
} else {
|
||||||
// Note: When allowRepeat is true, it does result in many possibilities being tried.
|
// Note: When allowRepeat is true, it does result in many possibilities being tried.
|
||||||
// We should ideally prune some of those at a higher level.
|
// We should ideally prune some of those at a higher level.
|
||||||
final int numChoices = choices.size();
|
modeIterator = new AllowRepeatModesIterator(choices.size(), min, num);
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
selectedModes = modeIterator.next();
|
selectedModes = modeIterator.next();
|
||||||
advancedToNextMode = true;
|
advancedToNextMode = true;
|
||||||
@@ -211,7 +176,6 @@ public class SpellAbilityChoicesIterator {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<AbilitySub> getModeCombination(List<AbilitySub> choices, int[] modeIndexes) {
|
public static List<AbilitySub> getModeCombination(List<AbilitySub> choices, int[] modeIndexes) {
|
||||||
ArrayList<AbilitySub> modes = new ArrayList<AbilitySub>();
|
ArrayList<AbilitySub> modes = new ArrayList<AbilitySub>();
|
||||||
for (int modeIndex : modeIndexes) {
|
for (int modeIndex : modeIndexes) {
|
||||||
@@ -219,4 +183,48 @@ public class SpellAbilityChoicesIterator {
|
|||||||
}
|
}
|
||||||
return modes;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user