/** * Sets up the map. Make every region neutral with 2 armies to start with. Adds wastelands (> 2 * armies on a neutral) if wastelandSize > 0. * * @param initMap : the map object that hasn't been set up yet, i.e. no armies yet * @param wastelandSize : the amount of armies that a wasteland contains * @return : the fully initialized and setup Map object */ public static Map setupMap(Map initMap, int wastelandSize) { Map map = initMap; for (Region region : map.regions) { region.setPlayerName("neutral"); region.setArmies(2); } if (wastelandSize > 0) { int nrOfWastelands = (int) (map.getSuperRegions().size() / 2); // amount of wastelands is half of the amount of superRegions for (int i = 0; i < nrOfWastelands; i++) { double rand = Math.random(); int index = (int) (rand * map.getRegions().size()); Region wasteland = map.getRegions().get(index); if (wasteland.getArmies() > 2 && !roomForWasteland(wasteland.getSuperRegion())) { i--; continue; } wasteland.setArmies(wastelandSize); } } return map; }
/** * Determines how much rounds a game can take before it's a draw, depending on map size * * @param map : the created map * @return : the maximum number of rounds for this game */ public static int determineMaxRounds(Map map) { return (int) Math.max( 60, map.getRegions().size() * 2.5); // minimum of 60, otherwise 2.5 times the number of regions }
/** * @param map * @return : the string representation of given map's wastelands */ private static String getWastelandsString(Map map) { String wastelandsString = "setup_map wastelands"; for (Region region : map.getRegions()) { if (region.getArmies() > 2) { int id = region.getId(); wastelandsString = wastelandsString.concat(" " + id); } } return wastelandsString; }
/** * @param mapString : string that represents the map to be created * @return : a Map object to use in the game */ public static Map createMap(String mapString) { Map map = new Map(); // parse the map string try { JSONObject jsonMap = new JSONObject(mapString); // create SuperRegion objects JSONArray superRegions = jsonMap.getJSONArray("SuperRegions"); for (int i = 0; i < superRegions.length(); i++) { JSONObject jsonSuperRegion = superRegions.getJSONObject(i); map.add(new SuperRegion(jsonSuperRegion.getInt("id"), jsonSuperRegion.getInt("bonus"))); } // create Region object JSONArray regions = jsonMap.getJSONArray("Regions"); for (int i = 0; i < regions.length(); i++) { JSONObject jsonRegion = regions.getJSONObject(i); SuperRegion superRegion = map.getSuperRegion(jsonRegion.getInt("superRegion")); map.add(new Region(jsonRegion.getInt("id"), superRegion)); } // add the Regions' neighbors for (int i = 0; i < regions.length(); i++) { JSONObject jsonRegion = regions.getJSONObject(i); Region region = map.getRegion(jsonRegion.getInt("id")); JSONArray neighbors = jsonRegion.getJSONArray("neighbors"); for (int j = 0; j < neighbors.length(); j++) { Region neighbor = map.getRegion(neighbors.getInt(j)); region.addNeighbor(neighbor); } } } catch (JSONException e) { System.err.println("JSON: Can't parse map string: " + e); } map.sort(); return map; }