now we depend on apache.commons.io

updated set aliases
more predicates for good use
deckeditorbase concept preview
This commit is contained in:
Maxmtg
2011-08-28 15:33:26 +00:00
parent b67e86c573
commit fc95a74ada
5 changed files with 137 additions and 10 deletions

View File

@@ -0,0 +1,75 @@
package forge;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import net.slightlymagic.maxmtg.Predicate;
import forge.card.CardCoreType;
import forge.card.CardRules;
import forge.card.CardRules.Predicates;
import forge.card.CardPoolView;
public class Gui_DeckEditorBase extends JFrame {
private static final long serialVersionUID = -401223933343539977L;
//public JCheckBox whiteCheckBox = new GuiFilterCheckBox("white", "White");
public JCheckBox whiteCheckBox = new JCheckBox("W", true);
public JCheckBox blueCheckBox = new JCheckBox("U", true);
public JCheckBox blackCheckBox = new JCheckBox("B", true);
public JCheckBox redCheckBox = new JCheckBox("R", true);
public JCheckBox greenCheckBox = new JCheckBox("G", true);
public JCheckBox colorlessCheckBox = new JCheckBox("C", true);
public JCheckBox landCheckBox = new JCheckBox("Land", true);
public JCheckBox creatureCheckBox = new JCheckBox("Creature", true);
public JCheckBox sorceryCheckBox = new JCheckBox("Sorcery", true);
public JCheckBox instantCheckBox = new JCheckBox("Instant", true);
public JCheckBox planeswalkerCheckBox = new JCheckBox("Planeswalker", true);
public JCheckBox artifactCheckBox = new JCheckBox("Artifact", true);
public JCheckBox enchantmentCheckBox = new JCheckBox("Enchant", true);
public static String getStats(CardPoolView deck) {
int total = deck.countAll();
int creature = CardRules.Predicates.Presets.isCreature.aggregate(deck, CardPoolView.fnToCard, CardPoolView.fnToCount);
int land = CardRules.Predicates.Presets.isLand.aggregate(deck, CardPoolView.fnToCard, CardPoolView.fnToCount);
StringBuffer show = new StringBuffer();
show.append("Total - ").append(total).append(", Creatures - ").append(creature).append(", Land - ")
.append(land);
String[] color = Constant.Color.onlyColors;
List<Predicate<CardRules>> predicates = CardRules.Predicates.Presets.colors;
for (int i = 0; i < color.length; ++i) {
show.append(String.format(", %s - %d", color[i], predicates.get(i).count(deck, CardPoolView.fnToCard)));
}
return show.toString();
}// getStats()
public final Predicate<CardRules> buildFilter() {
List<Predicate<CardRules>> colors = new ArrayList<Predicate<CardRules>>();
if (whiteCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isWhite); }
if (blueCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isBlue); }
if (blackCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isBlack); }
if (redCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isRed); }
if (greenCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isGreen); }
if (colorlessCheckBox.isSelected()) { colors.add(CardRules.Predicates.Presets.isColorless); }
Predicate<CardRules> filterByColor = colors.size() == 6 ? Predicate.getTrue(CardRules.class) : Predicate.or(colors);
List<Predicate<CardRules>> types = new ArrayList<Predicate<CardRules>>();
if (landCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isLand); }
if (creatureCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isCreature); }
if (sorceryCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isSorcery); }
if (instantCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isInstant); }
if (planeswalkerCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isPlaneswalker); }
if (artifactCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isArtifact); }
if (enchantmentCheckBox.isSelected()) { types.add(CardRules.Predicates.Presets.isEnchantment); }
Predicate<CardRules> filterByType = colors.size() == 7 ? Predicate.getTrue(CardRules.class) : Predicate.or(types);
return Predicate.and(filterByColor, filterByType);
}
}

View File

@@ -117,7 +117,7 @@ public abstract class Predicate<T> {
}
// Random - algorithm adapted from Braid's GeneratorFunctions
public final T random(final Iterable<T> source) { //
public final T random(final Iterable<T> source) {
int n = 0;
T candidate = null;
for (T item : source) {
@@ -126,7 +126,7 @@ public abstract class Predicate<T> {
}
return candidate;
}
public final <U> U random(final Iterable<U> source, final Lambda1<T, U> accessor) {
public final <U> U random(final Iterable<U> source, final Lambda1<T, U> accessor) {
int n = 0;
U candidate = null;
for (U item : source) {
@@ -135,6 +135,28 @@ public abstract class Predicate<T> {
}
return candidate;
}
// Get several random values
// should improve to make 1 pass over source and track N candidates at once
public final List<T> random(final Iterable<T> source, final int count) {
List<T> result = new ArrayList<T>();
for (int i = 0; i < count; ++i) {
T toAdd = random(source);
if (toAdd == null) { break; }
result.add(toAdd);
}
return result;
}
public final <U> List<U> random(final Iterable<U> source, final Lambda1<T, U> accessor, final int count) {
List<U> result = new ArrayList<U>();
for (int i = 0; i < count; ++i) {
U toAdd = random(source, accessor);
if (toAdd == null) { break; }
result.add(toAdd);
}
return result;
}
// Static builder methods - they choose concrete implementation by themselves
public static <T> Predicate<T> not(final Predicate<T> operand1) { return new Not<T>(operand1); }
public static <T> Predicate<T> compose(final Predicate<T> operand1,
@@ -142,14 +164,14 @@ public abstract class Predicate<T> {
{
return new Node<T>(operand1, operator, operand2);
}
public static <T> Predicate<T> and(final Predicate<T> operand1, final Predicate<T> operand2) {
return new NodeAnd<T>(operand1, operand2);
}
// Predefined operators: and, or
public static <T> Predicate<T> and(final Predicate<T> operand1, final Predicate<T> operand2) { return new NodeAnd<T>(operand1, operand2); }
public static <T> Predicate<T> and(final Iterable<Predicate<T>> operand) { return new MultiNodeAnd<T>(operand); }
public static <T> Predicate<T> or(final Predicate<T> operand1, final Predicate<T> operand2) {
return new NodeOr<T>(operand1, operand2);
}
public static <T, U> Predicate<T> and(final Predicate<T> operand1, final Predicate<U> operand2, Lambda1<U, T> bridge) { return new NodeAndBridged<T, U>(operand1, operand2, bridge); }
public static <T> Predicate<T> or(final Predicate<T> operand1, final Predicate<T> operand2) { return new NodeOr<T>(operand1, operand2); }
public static <T> Predicate<T> or(final Iterable<Predicate<T>> operand) { return new MultiNodeOr<T>(operand); }
public static <T, U> Predicate<T> or(final Predicate<T> operand1, final Predicate<U> operand2, Lambda1<U, T> bridge) { return new NodeOrBridged<T, U>(operand1, operand2, bridge); }
// Concrete implementations
// unary operators
@@ -197,6 +219,30 @@ public abstract class Predicate<T> {
@Override public boolean isTrue(final T card) { return filter1.isTrue(card) && filter2.isTrue(card); }
}
// Bridged OR and AND
protected static final class NodeOrBridged<T,U> extends Predicate<T> {
private final Predicate<T> filter1;
private final Predicate<U> filter2;
private final Lambda1<U, T> bridge;
public NodeOrBridged(final Predicate<T> operand1, final Predicate<U> operand2, final Lambda1<U, T> accessor) {
filter1 = operand1;
filter2 = operand2;
bridge = accessor;
}
@Override public boolean isTrue(final T card) { return filter1.isTrue(card) || filter2.isTrue(bridge.apply(card)); }
}
protected static final class NodeAndBridged<T,U> extends Predicate<T> {
private final Predicate<T> filter1;
private final Predicate<U> filter2;
private final Lambda1<U, T> bridge;
public NodeAndBridged(final Predicate<T> operand1, final Predicate<U> operand2, final Lambda1<U, T> accessor) {
filter1 = operand1;
filter2 = operand2;
bridge = accessor;
}
@Override public boolean isTrue(final T card) { return filter1.isTrue(card) && filter2.isTrue(bridge.apply(card)); }
}
// multi-operand operators
protected abstract static class MultiNode<T> extends Predicate<T> {
protected final Iterable<Predicate<T>> operands;