<commit_before>package reignierOfDKE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

import reignierOfDKE.C.MapType;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
import battlecode.common.TerrainTile;

public class MapAnalyzer {

	protected RobotController rc;

	private MapLocation myHq;
	private MapLocation otherHq;
	private int ySize;
	private int xSize;
	private MapType mapType;

	private MapLocation bestForPastr;
	private MapLocation[][] best;
	private char[][] mapRepresentation;
	private double[][] mapCowGrowth;
	private double[][] mapPastrRating;

	private int[][] realDistanceFromOpponentHQ;
	PriorityQueue<MapLocationDistance> queue;
	private boolean realDistanceReady = false;

	private static final int MAP_SIZE_SMALL_THRESHOLD = 40;
	private static final int MAP_SIZE_MEDIUM_THRESHOLD = 60;
	private static final int MAP_SIZE_NO_SQUARE_MEDIUM_THRESHOLD = 50;

	public MapAnalyzer(RobotController rc, MapLocation myHQ,
			MapLocation otherHq, int ySize, int xSize) {
		this.rc = rc;
		this.myHq = myHQ;
		this.otherHq = otherHq;
		this.ySize = ySize;
		this.xSize = xSize;
		this.mapType = determineMapType();
	}

	/**
	 * generates a map. Each location gets assigned the number of moves
	 * necessary to reach it from the opponent's HQ. (shortest path) Currently
	 * takes about 500 rounds to be executed.. way to expensive. The underlying
	 * algorithm is Dijkstra.
	 */
	public void generateRealDistanceMap() {
		realDistanceFromOpponentHQ = new int[ySize][xSize];
		for (int[] row : realDistanceFromOpponentHQ) {
			Arrays.fill(row, Integer.MAX_VALUE - 1);
		}
		queue = new PriorityQueue<MapLocationDistance>();
		MapLocationDistance source = new MapLocationDistance(otherHq, 0);
		queue.add(source);
		while (!queue.isEmpty()) {
			MapLocationDistance current = queue.poll();
			List<MapLocation> neighbours = getNeighbours(current
					.getMapLocation());
			for (MapLocation neighbour : neighbours) {
				MapLocationDistance n = new MapLocationDistance(neighbour,
						realDistanceFromOpponentHQ[neighbour.y][neighbour.x]);
				int distanceOverCurrent = current.getDistance() + 1;
				if (distanceOverCurrent < n.getDistance()) {
					queue.remove(n);
					n.setDistance(distanceOverCurrent);
					realDistanceFromOpponentHQ[n.getMapLocation().y][n
							.getMapLocation().x] = distanceOverCurrent;
					queue.add(n);
				}
				n.setDistance(current.getDistance() + 1);
			}
		}
	}

	private boolean isXonMap(int x) {
		return x >= 0 && x < xSize;
	}

	private boolean isYonMap(int y) {
		return y >= 0 && y < ySize;
	}

	/**
	 * returns all neighbours of a location fulfilling the following conditions:
	 * <li>the terrain type is NORMAL or ROAD</li> <li>the location is located
	 * on the map</li><li>the location is not an HQ-location</li> <br>
	 * in short: any location which a soldier can move to</br>
	 * 
	 * @param from
	 * @return
	 */
	private List<MapLocation> getNeighbours(MapLocation from) {
		List<MapLocation> neighbours = new ArrayList<MapLocation>();
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y));
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y + 1));
		addNeighbour(neighbours, new MapLocation(from.x, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x, from.y + 1));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y + 1));
		for (int i = neighbours.size() - 1; i >= 0; i--) {
			MapLocation current = neighbours.get(i);
			TerrainTile terrain = rc.senseTerrainTile(new MapLocation(
					current.x, current.y));
			if (terrain == TerrainTile.VOID || terrain == TerrainTile.OFF_MAP
					|| current.equals(myHq) || current.equals(otherHq)) {
				neighbours.remove(i);
			}
		}
		return neighbours;
	}

	private void addNeighbour(List<MapLocation> neighbours,
			MapLocation potentialNeighbour) {
		if (isXonMap(potentialNeighbour.x) && isYonMap(potentialNeighbour.y)) {
			neighbours.add(potentialNeighbour);
		}
	}

	private MapType determineMapType() {
		if (ySize < MAP_SIZE_SMALL_THRESHOLD
				&& xSize < MAP_SIZE_SMALL_THRESHOLD) {
			// If both dimensions are smaller than the Small threshold
			return MapType.Small;
		}
		if (ySize < MAP_SIZE_MEDIUM_THRESHOLD
				&& xSize < MAP_SIZE_MEDIUM_THRESHOLD) {
			// If both dimensions are smaller than the Medium threshold
			return MapType.Medium;
		}
		if (ySize > MAP_SIZE_MEDIUM_THRESHOLD
				&& xSize > MAP_SIZE_MEDIUM_THRESHOLD) {
			// If both dimensions are larger than the Medium threshold
			return MapType.Large;
		}
		// Get the largest of the two dimensions
		int largestDimension = Math.max(ySize, xSize);
		if (largestDimension < MAP_SIZE_NO_SQUARE_MEDIUM_THRESHOLD) {
			// If the largest dimension is smaller than the 'no-square' medium
			// threshold
			return MapType.Medium;
		} else {
			// Otherwise it is a large map, because the largest dimension will
			// be larger than the 'no-square' medium threshold
			return MapType.Large;
		}
	}

	public MapType getMapType() {<commit_msg>fixed boundary bug in MapAnalyzer<commit_after>package reignierOfDKE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

import reignierOfDKE.C.MapType;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
import battlecode.common.TerrainTile;

public class MapAnalyzer {

	protected RobotController rc;

	private MapLocation myHq;
	private MapLocation otherHq;
	private int ySize;
	private int xSize;
	private MapType mapType;

	private MapLocation bestForPastr;
	private MapLocation[][] best;
	private char[][] mapRepresentation;
	private double[][] mapCowGrowth;
	private double[][] mapPastrRating;

	private int[][] realDistanceFromOpponentHQ;
	PriorityQueue<MapLocationDistance> queue;
	private boolean realDistanceReady = false;

	private static final int MAP_SIZE_SMALL_THRESHOLD = 40;
	private static final int MAP_SIZE_MEDIUM_THRESHOLD = 60;
	private static final int MAP_SIZE_NO_SQUARE_MEDIUM_THRESHOLD = 50;

	public MapAnalyzer(RobotController rc, MapLocation myHQ,
			MapLocation otherHq, int ySize, int xSize) {
		this.rc = rc;
		this.myHq = myHQ;
		this.otherHq = otherHq;
		this.ySize = ySize;
		this.xSize = xSize;
		this.mapType = determineMapType();
	}

	/**
	 * generates a map. Each location gets assigned the number of moves
	 * necessary to reach it from the opponent's HQ. (shortest path) Currently
	 * takes about 500 rounds to be executed.. way to expensive. The underlying
	 * algorithm is Dijkstra.
	 */
	public void generateRealDistanceMap() {
		realDistanceFromOpponentHQ = new int[ySize][xSize];
		for (int[] row : realDistanceFromOpponentHQ) {
			Arrays.fill(row, Integer.MAX_VALUE - 1);
		}
		queue = new PriorityQueue<MapLocationDistance>();
		MapLocationDistance source = new MapLocationDistance(otherHq, 0);
		queue.add(source);
		while (!queue.isEmpty()) {
			MapLocationDistance current = queue.poll();
			List<MapLocation> neighbours = getNeighbours(current
					.getMapLocation());
			for (MapLocation neighbour : neighbours) {
				MapLocationDistance n = new MapLocationDistance(neighbour,
						realDistanceFromOpponentHQ[neighbour.y][neighbour.x]);
				int distanceOverCurrent = current.getDistance() + 1;
				if (distanceOverCurrent < n.getDistance()) {
					queue.remove(n);
					n.setDistance(distanceOverCurrent);
					realDistanceFromOpponentHQ[n.getMapLocation().y][n
							.getMapLocation().x] = distanceOverCurrent;
					queue.add(n);
				}
				n.setDistance(current.getDistance() + 1);
			}
		}
	}

	private boolean isXonMap(int x) {
		return x >= 0 && x < xSize;
	}

	private boolean isYonMap(int y) {
		return y >= 0 && y < ySize;
	}

	/**
	 * returns all neighbours of a location fulfilling the following conditions:
	 * <li>the terrain type is NORMAL or ROAD</li> <li>the location is located
	 * on the map</li><li>the location is not an HQ-location</li> <br>
	 * in short: any location which a soldier can move to</br>
	 * 
	 * @param from
	 * @return
	 */
	private List<MapLocation> getNeighbours(MapLocation from) {
		List<MapLocation> neighbours = new ArrayList<MapLocation>();
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y));
		addNeighbour(neighbours, new MapLocation(from.x - 1, from.y + 1));
		addNeighbour(neighbours, new MapLocation(from.x, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x, from.y + 1));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y - 1));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y));
		addNeighbour(neighbours, new MapLocation(from.x + 1, from.y + 1));
		for (int i = neighbours.size() - 1; i >= 0; i--) {
			MapLocation current = neighbours.get(i);
			TerrainTile terrain = rc.senseTerrainTile(new MapLocation(
					current.x, current.y));
			if (terrain == TerrainTile.VOID || terrain == TerrainTile.OFF_MAP
					|| current.equals(myHq) || current.equals(otherHq)) {
				neighbours.remove(i);
			}
		}
		return neighbours;
	}

	private void addNeighbour(List<MapLocation> neighbours,
			MapLocation potentialNeighbour) {
		if (isXonMap(potentialNeighbour.x) && isYonMap(potentialNeighbour.y)) {
			neighbours.add(potentialNeighbour);
		}
	}

	private MapType determineMapType() {
		if (ySize <= MAP_SIZE_SMALL_THRESHOLD
				&& xSize <= MAP_SIZE_SMALL_THRESHOLD) {
			// If both dimensions are smaller than the Small threshold
			return MapType.Small;
		}
		if (ySize <= MAP_SIZE_MEDIUM_THRESHOLD
				&& xSize <= MAP_SIZE_MEDIUM_THRESHOLD) {
			// If both dimensions are smaller than the Medium threshold
			return MapType.Medium;
		}
		if (ySize > MAP_SIZE_MEDIUM_THRESHOLD
				&& xSize > MAP_SIZE_MEDIUM_THRESHOLD) {
			// If both dimensions are larger than the Medium threshold
			return MapType.Large;
		}
		// Get the largest of the two dimensions
		int largestDimension = Math.max(ySize, xSize);
		if (largestDimension < MAP_SIZE_NO_SQUARE_MEDIUM_THRESHOLD) {
			// If the largest dimension is smaller than the 'no-square' medium
			// threshold
			return MapType.Medium;
		} else {
			// Otherwise it is a large map, because the largest dimension will
			// be larger than the 'no-square' medium threshold
			return MapType.Large;
		}
	}

	public MapType getMapType() {
{'fraction_non_alphanumeric': 0.10563246728617486, 'fraction_numerical': 0.005499715531955243, 'mean_word_length': 4.213544241225902, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2111004', 'n_tokens_mistral': 3947, 'n_tokens_neox': 3416, 'n_words': 900}