Thanks for the reply Regenuluz this is the path I ended up taking after a little more research.
For those looking to implement a similar system here is a test program called “Three Monkeys” that I made to get my head around the comparable method.
The program will generate 3 Monkey objects and place them in an ArayList, then randomly generate an initiative roll for each. The ArrayList is then re-ordered based on highest roll and each monkey takes it in turn to say what they have to say. Hope this helps someone. 8)
public class Monkeys implements Comparable{
public int initiative;
int speed;
String action;
public Monkeys(String action) {
super();
this.action = action;
speed = 5;
}
public int genInit(int roll) {
this.initiative = roll + speed;
return this.initiative;
}
public int compareTo(Object o) {
Monkeys tmp = (Monkeys)o;
if(this.initiative < tmp.initiative) {
return -1;
}else if(this.initiative > tmp.initiative) {
return 1;
}return 0;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class MonkeyMagic {
public static void main(String[] args) {
ArrayList<Monkeys> m = new ArrayList();
m.add(new Monkeys("I am a bezerker monkey"));
m.add(new Monkeys("I just wants the bananas"));
m.add(new Monkeys("I am a lovely monkey"));
Random rand = new Random();
for(Monkeys i: m) {
int r = rand.nextInt(100);
i.genInit(r);
}
Collections.sort(m);
for(Monkeys i: m) {
System.out.println(i.initiative + " " + i.action);
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
}
}