From 19183986d37c5ca1a31a5b308bace92b87430ffe Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 13 Jul 2015 03:31:02 +0200 Subject: Added some statistics functionality, TDD --- src/eu/equalparts/cardbase/decks/Deck.java | 12 ++++ .../equalparts/cardbase/decks/ReferenceDeck.java | 30 ++++++++ .../equalparts/cardbase/decks/StandaloneDeck.java | 12 ++++ src/eu/equalparts/cardbase/decks/Statistics.java | 80 ++++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 src/eu/equalparts/cardbase/decks/Deck.java create mode 100644 src/eu/equalparts/cardbase/decks/ReferenceDeck.java create mode 100644 src/eu/equalparts/cardbase/decks/StandaloneDeck.java create mode 100644 src/eu/equalparts/cardbase/decks/Statistics.java (limited to 'src/eu/equalparts/cardbase/decks') diff --git a/src/eu/equalparts/cardbase/decks/Deck.java b/src/eu/equalparts/cardbase/decks/Deck.java new file mode 100644 index 0000000..1f28d60 --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/Deck.java @@ -0,0 +1,12 @@ +package eu.equalparts.cardbase.decks; + +public abstract class Deck { + + public String name = "Unnamed Deck"; + public int plains = 0, + islands = 0, + swamps = 0, + mountains = 0, + forests = 0; + +} diff --git a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java new file mode 100644 index 0000000..9e8695b --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -0,0 +1,30 @@ +package eu.equalparts.cardbase.decks; + +import java.util.HashMap; +import java.util.Map; + +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.utils.UID; + +public class ReferenceDeck extends Deck { + + public Map cardReferences = new HashMap(); + + public ReferenceDeck() { + + } + + public ReferenceDeck(StandaloneDeck deck) { + this.name = deck.name; + this.plains = deck.plains; + this.islands = deck.islands; + this.swamps = deck.swamps; + this.mountains = deck.mountains; + this.forests = deck.forests; + + for (Card card : deck.cards) { + cardReferences.put(UID.makeHash(card), card.count); + } + } + +} diff --git a/src/eu/equalparts/cardbase/decks/StandaloneDeck.java b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java new file mode 100644 index 0000000..4c6d25c --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java @@ -0,0 +1,12 @@ +package eu.equalparts.cardbase.decks; + +import java.util.HashSet; +import java.util.Set; + +import eu.equalparts.cardbase.cards.Card; + +public class StandaloneDeck extends Deck { + + public Set cards = new HashSet(); + +} diff --git a/src/eu/equalparts/cardbase/decks/Statistics.java b/src/eu/equalparts/cardbase/decks/Statistics.java new file mode 100644 index 0000000..9efc82a --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/Statistics.java @@ -0,0 +1,80 @@ +package eu.equalparts.cardbase.decks; + +import eu.equalparts.cardbase.cards.Card; + +public final class Statistics { + + private Statistics() {} + + + + public static double calculatePercentage(StandaloneDeck deck, String type) { + double allCardsByType = count(deck, type); + double allCards = count(deck); + return allCardsByType / allCards; + } + + public static int count(StandaloneDeck deck, String type) { + int count = type.contains("Land") ? countBasicLands(deck) : 0; + for (Card card : deck.cards) { + if (card.type != null && + card.type.contains(type)) { + count += card.count; + } + } + return count; + } + + public static int count(StandaloneDeck deck) { + int totalCards = countBasicLands(deck); + + for (Card card : deck.cards) { + totalCards += card.count; + } + + return totalCards; + } + + private static int countBasicLands(StandaloneDeck deck) { + return deck.plains + + deck.islands + + deck.swamps + + deck.mountains + + deck.forests; + } + + public static int[] computeDistribution(StandaloneDeck deck, String type) { + int arraySize = 0; + for (Card card : deck.cards) { + if (card.type != null && card.type.contains(type)) + if (card.cmc != null && card.cmc >= arraySize) + arraySize = card.cmc + 1; + } + + int[] costs = new int[arraySize]; + for (Card card : deck.cards) { + if (card.type != null && card.type.contains(type)) + if (card.cmc != null) + costs[card.cmc] += card.count; + } + + return costs; + } + + public static int[] computeDistribution(StandaloneDeck deck) { + int arraySize = 0; + for (Card card : deck.cards) { + if (card.cmc != null && card.cmc >= arraySize) + arraySize = card.cmc + 1; + } + + int[] costs = new int[arraySize]; + for (Card card : deck.cards) { + if (card.cmc != null) + costs[card.cmc] += card.count; + } + + return costs; + } + +} -- cgit v1.2.3