0

我正在实施一个统计+成就系统。基本上结构是:

  • 一个成就有许多相关的统计数据,这种关系必须将每个成就与所需的统计数据(及其价值)相关联。例如,Achievement1 需要值为 50(或更大)的 Statistic1 和值为 100(或更大)的 Statistic2。
  • 给定一个统计数据,我还需要知道相关的成就是什么(以便在统计数据发生变化时检查它们。

统计数据和成就都有一个唯一的 ID。

我的问题是我不知道代表它的最佳数据结构是什么。顺便说一句,我正在使用:

SparseArray<HashMap<Statistic, Integer>> statisticsForAnAchievement;

对于第一个点,数组的索引是成就 ID,HashMap 包含 Statistic/TargetValue 对。还有一个:

SparseArray<Collection<Achievement>> achievementsRelatedToAStatistic;

对于第二个点,索引是 StatisticID,项目是与成就相关的集合。

然后我需要处理两个对象来保持它的连贯性。

有没有更简单的表示方式?谢谢

4

1 回答 1

1

正如一个Statistic(或一组Statistics)描述的Achievement那样/那些Statistic/s不应该存储在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该类应将其目标(Statistic1 的 50 值)值作为字段存储在其中。

一个成就有许多相关的统计数据,这种关系必须将每个成就与所需的统计数据(及其价值)相关联。例如,Achievement1 需要值为 50(或更大)的 Statistic1 和值为 100(或更大)的 Statistic2。

统计数据已经存储在成就中,因此您所要做的就是存储成就(或他们自己的成就)的 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, 映射,每次调用addStaticticorremoveStatistic方法之一时都会更新该映射。

关于您的代码,如果您不需要该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
于 2012-07-17T07:36:17.940 回答