package tournament; import java.util.Random; import java.util.Vector; /** * Handles the selection of maps for each game. In the constructor a Vector * containing all of the maps to choose from and a Vector containing all of the * bots that are playing are passed in. In each bot a random 32 bit hex value * which was set according to a hex value selected by each bot writer is XOR * together to form the seed for the Java random number generator. The method * nextMap then gets the next int in the range of the number of maps and then * returns that map. * * @author Jason */ public class MapPicker { /** The particular instantiation of the Java random class */ private Random generator; /** * A vector containing all of the available maps for this class to choose * between */ private Vector maps; /** The number of maps that are available to choose between */ private int numMaps; /** * The seed that was calculated for the XORing of each bot writers chosen * value */ private int seed; /** * The next iD number */ private int iD; /** A Vector Containing all of the bots */ private Vector bots; private MapPicker upper; /** * Constructor for the Map Picker class. Calculates the seed from the * preselected 32 bit hex numbers each bot writer provided and creates the * instantiation of the Random class. * * @param bots * A vector containing all the bots that are playing in the * tournament that this Map picker is picking for. * @param maps * A vector containing all of the availible maps to choose * between. */ public MapPicker(Vector bots, Vector maps) { this.bots = bots; seed = 0; for (int i = 0; i < bots.size(); i++) { seed = seed ^ bots.get(i).getRandomHex(); } generator = new Random(seed); this.maps = maps; numMaps = maps.size(); iD = 0; upper = null; } /** * Constructor for the Map Picker class. Calculates the seed from the * preselected 32 bit hex numbers each bot writer provided and creates the * instantiation of the Random class. * * @param bots * A vector containing all the bots that are playing in the * tournament that this Map picker is picking for. * @param maps * A vector containing all of the availible maps to choose * between. */ public MapPicker(Vector bots, Vector maps, MapPicker upper) { this.bots = bots; seed = 0; for (int i = 0; i < bots.size(); i++) { seed = seed ^ bots.get(i).getRandomHex(); } generator = new Random(seed); this.maps = maps; numMaps = maps.size(); this.upper = upper; this.iD = 0; } /** * Returns the next randomly selected map to be played on. * * @return Map, the next randomly selected map to be played on. */ public Map nextMap() { return maps.get(generator.nextInt(numMaps)); } /** * Returns the iD number with which to number all games and then increments * it for the next use * * @return iD */ public int nextID() { if(upper != null){ return upper.nextID(); } return iD++; } /** * Generates a Map picker for a pair of bots using the random numbers of * every other bot as a seed. * * @param host * The first bot this MapPicker is being Generated for * @param away * The second bot this MapPicker is being Generated for * @return MapPicker for a pair of bots */ public MapPicker ourMapPicker(Bot host, Bot away) { Vector otherBot = new Vector(); for (int i = 0; i < bots.size(); i++) { if (!(bots.get(i).getName().equals(host.getName()))) { if (!(bots.get(i).getName().equals(away.getName()))) { otherBot.add(bots.get(i)); } } } return new MapPicker(otherBot, this.maps, this); } }