2012-07-16 48 views
0

我正在實施統計+成就係統。基本結構是:統計和成就係統的數據結構

  • 的成就有很多相關的統計,這個關係必須與所需統計的每個成就關聯(和它的值)。例如,Achievement1需要Statistic1的值爲50(或更高),Statistic2的值爲100(或更高)。
  • 給定一個統計我也需要知道什麼是相關成就(以檢查他們的統計變化的時候。

兩個統計和成就有一個唯一的ID。

我的問題是我不'T知道最新最好的數據(一個或多個)結構的(一個或多個),用於表示順便說我使用:

SparseArray<HashMap<Statistic, Integer>> statisticsForAnAchievement; 

對於第一點,其中陣列的索引是成就ID和HashMap中包含Statistic/TargetValue對。和:

SparseArray<Collection<Achievement>> achievementsRelatedToAStatistic; 

對於第二點,其中指數是StatisticID與產品相關的成果集。

然後我需要處理這兩個對象保持一致性。

是否有更容易的方式來表示?感謝

回答

1

作爲Statistic(或一組Statistics)描述了一種Achievement不應該/這些Statistic /秒被存儲在Achievement類?例如,改進的Achievement類:

public class Achievement { 
    SparseArray<Statistic> mStatistics = new SparseArray<Statistic>(); 

    // to get a reference to the statisctics that make this achievement 
    public SparseArray<Statistic> getStatics() { 
     return mStatistics; 
    } 

    // add a new Statistic to these Achievement 
    public void addStatistic(int statisticId, Statistic newStat) { 
     // if we don't already have this particular statistic, add it 
     // or maybe update the underlining Statistic?!? 
     if (mStatistics.get(statisticId) == null) { 
      mStatistic.add(newStat); 
     } 
    } 

    // remove the Statistic 
    public void removeStatistic(int statisticId) { 
     mStatistic.delete(statisticId); 
    } 

    // check to see if this achievment has a statistic with this id 
    public boolean hasStatistics(int statisticId) { 
     return mStatistic.get(statisticId) == null ? false : true; 
    } 

    // rest of your code 
} 

此外,Statistic類應該存儲它的目標(50值Statistic1)值在它作爲一個字段。

的成就有很多相關的統計,這個關係必須 關聯的每個成就所要求的統計(和它 值)。例如,Achievement1需要Statistic1的值爲 50(或更高),Statistic2的值爲100(或更高)。

的統計數據已經存儲在個成就因此,所有你需要做的是保存個成就(或成就他們自己)的ID數組/列表,這樣,你將有機會獲得的統計數據取得了這些成就。

鑑於一個統計我還需要知道什麼是相關的成就 (爲了檢查他們統計變化時。

你會使用的成就上述陣列/列表,迭代和檢查,看是否實現認爲特別Statistic

ArrayList<Achievement> relatedAchievements = new ArrayList<Achievement>(); 
for (Achievement a : theListOfAchievments) { 
    if (a.hasStatistics(targetStatistic)) { 
      relatedAchievements.add(a); // at the end this will store the achievements related(that contain) the targetStatistic 
    } 
} 

另一種選擇是有地方靜態映射,其存儲成就有一個Statistic,映射將在每次調用addStaticticremoveStatistic方法時得到更新。

關於你的代碼,如果不需要Statistic對象,並很高興與只是抱着它id參考,那麼你可以提高statisticsForAnAchievement有:

SparseArray<SparseIntArray> statisticsForAnAchievement; 
// the index of the SparseArray is the Achievement's id 
// the index of the SparseIntArray is the Statistic's id 
// the value of the SparseIntArray is the Statistic's value