回合制游戏的速度算法



我正在创建一个基于文本的rpg演示项目,我很难找到和/或找到一种使用角色速度形成算法的方法,以确定战斗中的攻击顺序。首先,我希望对已经建立的相关算法提供任何意见或指导。否则,我可以在我的算法想法上寻求帮助。条款和代码如下。它仍然需要大量的工作。提前感谢!

--我如何确定订单?

1) 我在LinkedHashMap中按降序排列6个字符的速度(键值0中的最高速度),以维持的顺序

2) 使用最快的速度作为参考,我将其乘以一个因子,该因子给出了我用来确定字符顺序的速度的一个小百分比。例如,假设最快速度为50,系数为0.1。这将产生一个值5(50*0.1=5)。

3) 现在,我递归地减去从因子计算中接收到的值,以确定速度顺序。假设字符速度为char1=50,char2=40,char3=38,char4=33,char5=30,char6=25。我首先将最快的字符添加到另一个LinkedHashMap中,然后使用我的因子来接收一个新值,本质上我是用50-5来获得45。我检查第二快的字符(char2)的速度是否大于45,如果是,我将char2添加到映射中,否则我将先前添加的字符添加到映射(char1)中。然后我再次做减法,45-5得到40,并做同样的检查。我继续这样做,直到我将所有字符添加到地图上至少一次。我认为这是我的速度模式。我只需要重复这个模式,只需要在角色死亡时修改它。以下是基于我上面提供的示例的订单:

char1 w/50速

char1 w/50速度(再次)

char2 w/40速

char3 w/38速

char4 w/33速

char5 w/30速

char6 w/25速

既然你知道我在努力做什么,我愿意接受建议。以下是我如何编程的,假设我已经按速度订购了LinkedHashMap。

// stores entire speed pattern
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>(); 
// order un-ordered hashMap "list"
List<Entry<Integer, Integer>> list = new LinkedList<Entry<Integer, Integer>>(sortedMap.entrySet());
Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {        
return o2.getValue().compareTo(o1.getValue());
}
});
// maintaining insertion order with the help of LinkedList
Map<Integer, Integer> result = new LinkedHashMap<Integer, Integer>();
for (int i = 0; i < list.size(); i++) {
result.put(i, list.get(i).getValue());
}
int count = 0;
// note: totalChars = 6
// note: multiplierSpeedPattern = 0.1
double highestSpeed = result.get(0); // highest speed
while (Math.round(highestSpeed) > 0 && count < totalChars) {        
double temp = highestSpeed;
highestSpeed = (double) (temp - (multiplierSpeedPattern * temp));
int roundedHighestSpeed = (int) Math.round(highestSpeed);
Log.i(TAG, "roundedHighestSpeed: " + roundedHighestSpeed);
Iterator iter = result.entrySet().iterator();
if (iter.hasNext()) {    
if (result.get(0) > roundedHighestSpeed && result.get(1) < roundedHighestSpeed) {
map.put(count, result.get(count));           
count++;                     
} else if (result.get(1) > roundedHighestSpeed && result.get(2) < roundedHighestSpeed) {
map.put(count, result.get(count));
count++;
} else if (result.get(2) > roundedHighestSpeed && result.get(3) < roundedHighestSpeed) {
map.put(count, result.get(count));
count++;
} else if (result.get(3) > roundedHighestSpeed && result.get(4) < roundedHighestSpeed) {
map.put(count, result.get(count));
count++;
} else if (result.get(4) > roundedHighestSpeed && result.get(5) < roundedHighestSpeed) {
map.put(count, result.get(count));
count++;
} else if (result.get(5) > roundedHighestSpeed) {
map.put(count, result.get(count));
count++;
} else {
map.put(count, result.get(count));
count++;
}
}
}

我的逻辑不正确,因为我还需要检查我是否已经在地图中添加了字符。有什么帮助吗?

很遗憾,我没有时间仔细阅读你的问题。然而,对于这类问题,通常首先要对元素进行排序。

然而,LinkedHashMap保持插入顺序,但不对元素进行排序。使用TreeMap对顺序进行排序(用作键的实例的类必须实现Comparable,否则在运行时将获得类强制转换异常)。

这应该能完成大部分任务。关于算法的详细信息,我真的推荐一本伟大的书《算法导论》(由Cormen、Leierson、Rivest和Stein撰写)。它提供了很好的例子和解释,以及各种各样的算法。对于您的问题,请查看排序/清除算法。

最新更新