Fix so random decks works properly for constructed game

This commit is contained in:
drdev
2014-08-03 15:21:36 +00:00
parent 4419a85ea2
commit 846fd1aa34
2 changed files with 55 additions and 18 deletions

View File

@@ -61,6 +61,15 @@ public class Aggregates {
return result; return result;
} }
public static final <T> T random(final T[] source) {
if (source == null) { return null; }
switch (source.length) {
case 0: return null;
case 1: return source[0];
default: return source[MyRandom.getRandom().nextInt(source.length)];
}
}
// Random - algorithm adapted from Braid's GeneratorFunctions // Random - algorithm adapted from Braid's GeneratorFunctions
/** /**
@@ -71,10 +80,10 @@ public class Aggregates {
* @return the t * @return the t
*/ */
public static final <T> T random(final Iterable<T> source) { public static final <T> T random(final Iterable<T> source) {
if( null == source ) if (source == null) { return null; }
return null;
Random rnd = MyRandom.getRandom(); Random rnd = MyRandom.getRandom();
if ( source instanceof List<?> ) { if (source instanceof List<?>) {
List<T> src = (List<T>)source; List<T> src = (List<T>)source;
int len = src.size(); int len = src.size();
switch(len) { switch(len) {
@@ -99,16 +108,16 @@ public class Aggregates {
public static final <T> List<T> random(final Iterable<T> source, final int count) { public static final <T> List<T> random(final Iterable<T> source, final int count) {
final List<T> result = new ArrayList<T>(); final List<T> result = new ArrayList<T>();
final int[] randoms = new int[count]; final int[] randoms = new int[count];
for(int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
randoms[i] = Integer.MAX_VALUE; randoms[i] = Integer.MAX_VALUE;
result.add(null); result.add(null);
} }
Random rnd = MyRandom.getRandom(); Random rnd = MyRandom.getRandom();
for(T item : source) { for (T item : source) {
int next = rnd.nextInt(); int next = rnd.nextInt();
for(int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if(next < randoms[i]) { if (next < randoms[i]) {
randoms[i] = next; randoms[i] = next;
result.set(i, item); result.set(i, item);
break; break;
@@ -118,6 +127,11 @@ public class Aggregates {
return result; return result;
} }
public static int randomInt(int min, int max) {
Random rnd = MyRandom.getRandom();
return rnd.nextInt(max - min + 1) + min;
}
public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic
final Map<K, U> uniques = new Hashtable<K, U>(); final Map<K, U> uniques = new Hashtable<K, U>();
for (final U c : source) { for (final U c : source) {
@@ -143,13 +157,15 @@ public class Aggregates {
public static <TItem, TField> TItem firstFieldEquals(List<TItem> source, Function<TItem, TField> valueAccessor, TField valueEquals) { public static <TItem, TField> TItem firstFieldEquals(List<TItem> source, Function<TItem, TField> valueAccessor, TField valueEquals) {
if (source == null) { return null; } if (source == null) { return null; }
if (valueEquals == null) { if (valueEquals == null) {
for (final TItem c : source) { for (final TItem c : source) {
if (null == valueAccessor.apply(c)) { if (null == valueAccessor.apply(c)) {
return c; return c;
} }
} }
} else { }
else {
for (final TItem c : source) { for (final TItem c : source) {
if (valueEquals.equals(valueAccessor.apply(c))) { if (valueEquals.equals(valueAccessor.apply(c))) {
return c; return c;
@@ -159,10 +175,9 @@ public class Aggregates {
return null; return null;
} }
public static <T, U> Iterable<Entry<U, Integer>> groupSumBy(Iterable<Entry<T, Integer>> source, Function<T, U> fnGetField) { public static <T, U> Iterable<Entry<U, Integer>> groupSumBy(Iterable<Entry<T, Integer>> source, Function<T, U> fnGetField) {
Map<U, Integer> result = new HashMap<U, Integer>(); Map<U, Integer> result = new HashMap<U, Integer>();
for(Entry<T, Integer> kv : source) { for (Entry<T, Integer> kv : source) {
U k = fnGetField.apply(kv.getKey()); U k = fnGetField.apply(kv.getKey());
Integer v = kv.getValue(); Integer v = kv.getValue();
Integer sum = result.get(k); Integer sum = result.get(k);
@@ -172,5 +187,4 @@ public class Aggregates {
} }
return result.entrySet(); return result.entrySet();
} }
} }

View File

@@ -169,7 +169,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
String sel = lstDecks.getSelectedItem().getName(); String sel = lstDecks.getSelectedItem().getName();
switch (lstDecks.getGameType()) { switch (lstDecks.getGameType()) {
case Commander: case Commander:
if (sel.equals("Random")) { if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getCommander(); IStorage<Deck> decks = FModel.getDecks().getCommander();
if (decks.size() > 0) { if (decks.size() > 0) {
return Aggregates.random(decks); return Aggregates.random(decks);
@@ -177,7 +177,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
} }
return DeckgenUtil.generateCommanderDeck(isAi); return DeckgenUtil.generateCommanderDeck(isAi);
case Archenemy: case Archenemy:
if (sel.equals("Random")) { if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getScheme(); IStorage<Deck> decks = FModel.getDecks().getScheme();
if (decks.size() > 0) { if (decks.size() > 0) {
return Aggregates.random(decks); return Aggregates.random(decks);
@@ -185,7 +185,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
} }
return DeckgenUtil.generateSchemeDeck(); return DeckgenUtil.generateSchemeDeck();
case Planechase: case Planechase:
if (sel.equals("Random")) { if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getPlane(); IStorage<Deck> decks = FModel.getDecks().getPlane();
if (decks.size() > 0) { if (decks.size() > 0) {
return Aggregates.random(decks); return Aggregates.random(decks);
@@ -193,9 +193,32 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
} }
return DeckgenUtil.generatePlanarDeck(); return DeckgenUtil.generatePlanarDeck();
default: default:
break; if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getConstructed();
if (decks.size() > 0) {
return Aggregates.random(decks);
}
}
while (true) {
switch (Aggregates.random(DeckType.values())) {
case PRECONSTRUCTED_DECK:
return Aggregates.random(DeckProxy.getAllPreconstructedDecks(QuestController.getPrecons())).getDeck();
case QUEST_OPPONENT_DECK:
return Aggregates.random(DeckProxy.getAllQuestEventAndChallenges()).getDeck();
case COLOR_DECK:
List<String> colors = new ArrayList<String>();
int count = Aggregates.randomInt(1, 3);
for (int i = 1; i <= count; i++) {
colors.add("Random " + i);
}
return DeckgenUtil.buildColorDeck(colors, isAi);
case THEME_DECK:
return Aggregates.random(DeckProxy.getAllThemeDecks()).getDeck();
default:
continue;
}
}
} }
return null;
} }
@Override @Override