我现在正在做我的项目的人工智能部分。我正在调用我的AI类中的一个方法,该方法旨在计算我绘制的角斗士对象实际需要结束的位置。我向该方法传递了一个列表,其中包含我想要放置的所有对象。AI类之前的一个方法已经确定了它们之间的距离,我将其存储为gladiator0..1..2..etc.movementGoal。
虽然该项目不是实时的,即我会想通过它的最后一步,我确实希望同步运动发生。这意味着我迭代列表的标准方法将不起作用,因为我需要关于另一个角斗士的移动决策的信息,以便在这些决策交互时找出任何一个角斗士的实际移动。
当我在类之外并且只有列表形式的变量时,我如何访问另一个特定角斗士的变量?
编辑:
我想我可以遍历并测试变量gladiatorNumber是否正确,然后当它拉出信息时?那将是相当绕来绕去的,但这是我能想到的全部。
Edit2:
按照要求,一些代码。我在Ai类中的方法如下所示:
public void moveAI(List<Gladiator> gladiators) {我的角斗士是这样定义的:
public class Gladiator {角斗士类被创建为一个数组,然后添加到一个单独的主类的列表中。我真的不想包含比这更多的代码,因为有一大堆代码。基本上,归结为我如何从AI类调用角斗士,即使我在主类中创建了上述对象,并且在AI类中只有列表形式的对象。假设角斗士中的所有变量都是公共的。我得到的错误是找不到引用gladiator0...1...2...etc的符号。
发布于 2013-06-04 23:51:01
我认为你的问题可以归结为想要将角斗士的数组传递给另一个类。这应该很容易。如果你在main-class中有这两个定义(注意你只需要一个,我推荐列表,因为它更通用,数组有固定长度)。
你想要这样的东西:
public class Main {
// ....stuff
// This is the main class that keeps the list of gladiators
private List<Gladiator> gladiatorsList;
private Gladiator[] gladiatorsArray;
private MovementAI movementAI;
public Main() {
// You initialize gladiatorsList and gladiatorsArray as before
// gladiatorsList = ...
// gladiatorsArrray = ...
// Now you want to pass this list/array to another class (the AI), you
// can do this in the constructor of that class like so:
movementAI = new MovementAI(gladiatorsList);
}
// ...stuff as before
}人工智能
public class MovementAI {
private List<Gladiator> gladiators;
// Giving the class the list-reference, this list will be the same as the
// list in main, when main-list changes so does this one, they point to the
// same list-object, so the reference is only needed once.
public MovementAI(List<Gladiator> gladiatorsList) {
this.gladiators = gladiatorsList;
}
// The class already has a reference to the list from its constructor so it
// doesn't need the list again as a parameter
public void moveAI() {
}
// If you don't want to keep a reference to the list in this class but only
// use it in a method (I would not recommend this)
public MovementAI() {
}
// You need to pass it gladiatorsList everytime you call this method.
public void moveAI(List<Gladiator> gladiators) {
}
}我在你的上一条评论中看到,你已经决定让AI决定如果它满足一个标准,那么就重新绘制,这是不推荐的,你应该在你的类中保持职责分离,减少错误倾向和更好的开发。建议让AI改变角斗士的列表(移动他们,杀死他们等),渲染类简单地绘制每个角斗士。
似乎你也想让每个角斗士都能持有另一个角斗士作为目标,对他们来说,最好是将目标作为对象持有,这样你就不必搜索整个列表来找出角斗士编号所指的是哪个角斗士,也不必考虑在列表中的排序。如下所示:
public class Gladiator {
// ..other stuff
private Gladiator target;
public Gladiator getTarget() {
return target;
}
public void setTarget(Gladiator target) {
this.target = target;
}
}https://stackoverflow.com/questions/16921650
复制相似问题