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;
}
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
/**
@@ -71,10 +80,10 @@ public class Aggregates {
* @return the t
*/
public static final <T> T random(final Iterable<T> source) {
if( null == source )
return null;
if (source == null) { return null; }
Random rnd = MyRandom.getRandom();
if ( source instanceof List<?> ) {
if (source instanceof List<?>) {
List<T> src = (List<T>)source;
int len = src.size();
switch(len) {
@@ -99,16 +108,16 @@ public class Aggregates {
public static final <T> List<T> random(final Iterable<T> source, final int count) {
final List<T> result = new ArrayList<T>();
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;
result.add(null);
}
Random rnd = MyRandom.getRandom();
for(T item : source) {
Random rnd = MyRandom.getRandom();
for (T item : source) {
int next = rnd.nextInt();
for(int i = 0; i < count; i++) {
if(next < randoms[i]) {
for (int i = 0; i < count; i++) {
if (next < randoms[i]) {
randoms[i] = next;
result.set(i, item);
break;
@@ -118,6 +127,11 @@ public class Aggregates {
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
final Map<K, U> uniques = new Hashtable<K, U>();
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) {
if (source == null) { return null; }
if (valueEquals == null) {
for (final TItem c : source) {
if (null == valueAccessor.apply(c)) {
return c;
}
}
} else {
}
else {
for (final TItem c : source) {
if (valueEquals.equals(valueAccessor.apply(c))) {
return c;
@@ -159,10 +175,9 @@ public class Aggregates {
return null;
}
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>();
for(Entry<T, Integer> kv : source) {
for (Entry<T, Integer> kv : source) {
U k = fnGetField.apply(kv.getKey());
Integer v = kv.getValue();
Integer sum = result.get(k);
@@ -172,5 +187,4 @@ public class Aggregates {
}
return result.entrySet();
}
}

View File

@@ -169,7 +169,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
String sel = lstDecks.getSelectedItem().getName();
switch (lstDecks.getGameType()) {
case Commander:
if (sel.equals("Random")) {
if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getCommander();
if (decks.size() > 0) {
return Aggregates.random(decks);
@@ -177,7 +177,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
}
return DeckgenUtil.generateCommanderDeck(isAi);
case Archenemy:
if (sel.equals("Random")) {
if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getScheme();
if (decks.size() > 0) {
return Aggregates.random(decks);
@@ -185,7 +185,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
}
return DeckgenUtil.generateSchemeDeck();
case Planechase:
if (sel.equals("Random")) {
if (sel.equals("Random User Deck")) {
IStorage<Deck> decks = FModel.getDecks().getPlane();
if (decks.size() > 0) {
return Aggregates.random(decks);
@@ -193,9 +193,32 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
}
return DeckgenUtil.generatePlanarDeck();
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