Friday, 9 August 2013

ArrayList select coordinates, if there are less clients than coordinates, give each client a coordinate

ArrayList select coordinates, if there are less clients than coordinates,
give each client a coordinate

Coordinate[] coords = {
new Coordinate(3093, 3630), new Coordinate(3095, 3632), new
Coordinate(3098, 3633),
new Coordinate(3101, 3633), new Coordinate(3104, 3631), new
Coordinate(3106, 3629),
new Coordinate(3107, 3627), new Coordinate(3108, 3624), new
Coordinate(3109, 3620),
new Coordinate(3108, 3617), new Coordinate(3106, 3614), new
Coordinate(3102, 3613),
new Coordinate(3099, 3613), new Coordinate(3097, 3613), new
Coordinate(3093, 3614),
new Coordinate(3090, 3617), new Coordinate(3087, 3619)
};
int random = Misc.random(coords.length - 1);
Coordinate coord = coords[random];
boolean found = false;
if (insidePlayers.size() < coords.length) {
if (spawnPoints.contains(coord)) {
found = false;
}
while (!found) {
random = Misc.random(coords.length - 1);
coord = coords[random];
if (!spawnPoints.contains(coord)) {
player.spawnPointX = coords[random].getX();
player.spawnPointY = coords[random].getY();
spawnPoints.add(coord);
found = true;
break;
}
}
}
else {
player.spawnPointX = coords[random].getX();
player.spawnPointX = coords[random].getY();
}
Basically what I am trying to do here is, if there are more clients than
available coordinates (spots), then give each player his own coordinate
(So other clients can't have the same coords).
But unfortunately it doesn't work, sometimes clients are getting the same
coordinates. Why is it happening? what did I do wrong?
Coordinate class:
public class Coordinate {
private int x = 0;
private int y = 0;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
ArrayList:
public static ArrayList<Coordinate> spawnPoints = new
ArrayList<Coordinate>();
So what's wrong there?

No comments:

Post a Comment