/** * This class creates a map of locations which represents * the game board for Oregon Trail **/ public class Map { //------------------------------------------------------------ // DATA MEMBERS //------------------------------------------------------------ /** The 2d array of locations in the map, indexed by row-col coordinates. */ private Location[][] map; /** The density of the number of Depots on the map */ private double depotDensity = Game.DEPOT_CHANCE; /** The density of the number of forests on the map. */ private double forestDensity = Game.DENSITY_FOREST; /** The density of the number of mountains on the map. */ private double mountainDensity = Game.DENSITY_MOUNTAIN; /** * Default constructor. Creates a 2d array of null Location references using the * {@link Game#BOARD_HEIGHT} and {@link Game#BOARD_WIDTH} as dimensions. * After the constructor is executed, the map will contain BOARD_HEIGHT * by BOARD_WIDTH null Location references. */ public Map () { // your implementation here } /** * This constructor initializes a 2D map of null Location references using the values * specified by height and width as row and column dimensions, if they are positive. * If height is 0 or negative, the row dimension is set to 1. * If width is 0 or negative, the column dimension is set to 1. * * @param height The height of the map * @param width The width of the map */ public Map (int height, int width) { // your implementation here } /** * This specific constructor initializes the 2D map using the given height * and width parameters, if positive, as the row and column dimensions. * There will be height x width null Location references. * (See the 2-parameter specific constructor.) * The remaining parameters are used to set the other private data member * values by calling the appropriate mutator methods. * * @param height The height of the map * @param width The width of the map * @param theDepotDensity The density value for Depots. Must be greater than 0.0. * @param theForestDensity The density value for Forests. Must be greater than 0.0. * @param theMountainDensity The density value for Mountains. Must be greater than 0.0. * */ public Map (int height, int width, double theDepotDensity, double theForestDensity, double theMountainDensity) { // your implementation here } //------------------------------------------------------------ // ACCESSORS AND MUTATORS //------------------------------------------------------------ /** * Returns the value of the private data member depotDensity * * @return Returns the depotDensity. */ public double getDepotDensity() { // your implementation here } /** * Sets the depotDensity property to the value passed. If the value passed * is negative, then the density is set to {@link Game#DEPOT_CHANCE}. * * @param theDepotDensity The depotDensity to set. */ public void setDepotDensity(double theDepotDensity) { // your implementation here } /** * Returns the value of the private data member forestDensity * * @return Returns the forestDensity. */ public double getForestDensity() { // your implementation here } /** * Sets the forestDensity property to the value passed. If the value passed * is negative, then the density is set to {@link Game#DENSITY_FOREST}. * * @param theForestDensity The forestDensity to set. */ public void setForestDensity(double theForestDensity) { // your implementation here } /** * Returns the 2-D private data member map * * @return Returns the map. */ public Location[][] getMap() { // your implementation here } /** * Sets the private 2-D map array to the 2-D map that is passed as a parameter. * * @param map The map to set. */ public void setMap(Location[][] map) { // your implementation here } /** * Returns the value of the private data member mountainDensity. * * @return Returns the mountainDensity. */ public double getMountainDensity() { // your implementation here } /** * Sets the mountainDensity property to the parameter value passed. * If the parameter value passed is negative, then the * density is set to {@link Game#DENSITY_MOUNTAIN}. * * @param theMountainDensity The mountainDensity to set. */ public void setMountainDensity(double theMountainDensity) { // your implementation here } //------------------------------------------------------------ // OTHER METHODS //------------------------------------------------------------ /** * Returns the number of columns on the map. The columns * correspond to the second dimension of the 2-D map. Since * the map is a 2-D array, this method returns the value * of the length attribute of map's second dimension. * * @return The width (number of columns) of the Map */ public int getNumCols() { // your implementation here } /** * Returns the number of rows on the Map. The rows * correspond to the first dimension of the 2-D map. Since * the map is a 2-D array, this method returns the value * of the length attribute of the map's first dimension. * * @return The height (number of rows) of the Map */ public int getNumRows() { // your implementation here } /** * Sets the map location at the coordinates specified * by row and col (if valid) to the passed Location, loc. * Row and col may not be negative values, nor may they be * larger than the Map's dimensions. * * If invalid row or col values are given, an error message is printed * and loc is not assigned to any Location in the 2-D map. * * @param row The row of the location in the map * @param col The column of the location in the map * @param loc The location to set */ public void setLocationAt (int row, int col, Location loc) { // your implementation here } /** * Returns the location at the coordinates specified by row and col. * if an invalid row or column index is given, null is returned. Invalid * index values are negative or larger than the corresponding dimension of * the map. * * @param row The row of the location in the map * @param col The column of the location in the map * @return the location at the position (i,j) */ public Location getLocationAt (int row, int col) { // your implementation here } /** * Sets the isVisited flag to true for all the locations within * visibility distance from the location specified by row and col. * This method examines every map coordinate and calculates the row * distance and the column distance. If both row distance AND * column distance are less than or equal to the value of the visibility value, * then that Location's isVisited data member is set to true using the * corresponding Location mutator. * * @param row The row position * @param col The column position * @param visibility The number of squares of visibility that * can be seen beyond those that have been visited. */ public void setVisibility (int row, int col, int visibility) { // your implementation here } /** * This method takes care of populating the map. * This consists of several tasks: * 1) Create new locations for each map coordinate * 2) Create a Depot at the Location at the default Game starting coordinates * The starting row is the largest possible row index on the map * The starting column is the largest possible column index on the map * 3) Set the initial visibility from the Game starting coordinates, * using the default visibility using the setVisibility() method. * 4) Set the price factors for each Location on the map using * the setPriceFactors() method. */ public void populate() { // your implementation here } /** * Sets the price factors for each Depot that exists on the map. * * This method examines every Location on the map. * If a Depot exists at the Location being examined, * set the Depot's price factor based on its distance from * the starting position using the equation: * 1 + (distance/Game.PRICE_FACTOR_MODIFIER) * */ // We give you this method as an example implementation public void setPriceFactors() { for ( int r = 0; r < getNumRows(); r++ ) for ( int c = 0; c < getNumCols(); c++ ) { // If there is no Depot at this location, // continue to next iteration of the loop if ( map[r][c].getDepot() == null ) continue; // The current location being examined Location location = map[r][c]; // determine price factor based on distance from starting position int xDis = getNumCols() - location.getPosCol() - 1; int yDis = getNumRows() - location.getPosRow() - 1; // The distance is the square root of (colDist^2) + (rowDist^2) double distance = Math.sqrt (xDis*xDis+yDis*yDis); // Set the price factor at the depot at the location location.getDepot().setPriceFactor(1 + distance/Game.PRICE_FACTOR_MODIFIER); } } }