Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  import java.util.Collections;  
  2.  import java.util.Iterator;  
  3.  import java.util.LinkedList;  
  4.  import java.util.List;  
  5.  import java.util.Random;  
  6.  /**  
  7.   * Represent a rectangular grid of field positions.  
  8.   * Each position is able to store a single animal.  
  9.   *  
  10.   * @author Gede
  11.   * @version (22/11/2018)
  12.   */  
  13.  public class Field  
  14.  {  
  15.    // A random number generator for providing random locations.  
  16.    private static final Random rand = Randomizer.getRandom();  
  17.    // The depth and width of the field.  
  18.    private int depth, width;  
  19.    // Storage for the animals.  
  20.    private Object[][] field;  
  21.    /**  
  22.     * Represent a field of the given dimensions.  
  23.     * @param depth The depth of the field.  
  24.     * @param width The width of the field.  
  25.     */  
  26.    public Field(int depth, int width)  
  27.    {  
  28.      this.depth = depth;  
  29.      this.width = width;  
  30.      field = new Object[depth][width];  
  31.    }  
  32.    /**  
  33.     * Empty the field.  
  34.     */  
  35.    public void clear()  
  36.    {  
  37.      for(int row = 0; row < depth; row++) {  
  38.        for(int col = 0; col < width; col++) {  
  39.          field[row][col] = null;  
  40.        }  
  41.      }  
  42.    }  
  43.    /**  
  44.     * Clear the given location.  
  45.     * @param location The location to clear.  
  46.     */  
  47.    public void clear(Location location)  
  48.    {  
  49.      field[location.getRow()][location.getCol()] = null;  
  50.    }  
  51.    /**  
  52.     * Place an animal at the given location.  
  53.     * If there is already an animal at the location it will  
  54.     * be lost.  
  55.     * @param animal The animal to be placed.  
  56.     * @param row Row coordinate of the location.  
  57.     * @param col Column coordinate of the location.  
  58.     */  
  59.    public void place(Object animal, int row, int col)  
  60.    {  
  61.      place(animal, new Location(row, col));  
  62.    }  
  63.    /**  
  64.     * Place an animal at the given location.  
  65.     * If there is already an animal at the location it will  
  66.     * be lost.  
  67.     * @param animal The animal to be placed.  
  68.     * @param location Where to place the animal.  
  69.     */  
  70.    public void place(Object animal, Location location)  
  71.    {  
  72.      field[location.getRow()][location.getCol()] = animal;  
  73.    }  
  74.    /**  
  75.     * Return the animal at the given location, if any.  
  76.     * @param location Where in the field.  
  77.     * @return The animal at the given location, or null if there is none.  
  78.     */  
  79.    public Object getObjectAt(Location location)  
  80.    {  
  81.      return getObjectAt(location.getRow(), location.getCol());  
  82.    }  
  83.    /**  
  84.     * Return the animal at the given location, if any.  
  85.     * @param row The desired row.  
  86.     * @param col The desired column.  
  87.     * @return The animal at the given location, or null if there is none.  
  88.     */  
  89.    public Object getObjectAt(int row, int col)  
  90.    {  
  91.      return field[row][col];  
  92.    }  
  93.    /**  
  94.     * Generate a random location that is adjacent to the  
  95.     * given location, or is the same location.  
  96.     * The returned location will be within the valid bounds  
  97.     * of the field.  
  98.     * @param location The location from which to generate an adjacency.  
  99.     * @return A valid location within the grid area.  
  100.     */  
  101.    public Location randomAdjacentLocation(Location location)  
  102.    {  
  103.      List<Location> adjacent = adjacentLocations(location);  
  104.      return adjacent.get(0);  
  105.    }  
  106.    /**  
  107.     * Get a shuffled list of the free adjacent locations.  
  108.     * @param location Get locations adjacent to this.  
  109.     * @return A list of free adjacent locations.  
  110.     */  
  111.    public List<Location> getFreeAdjacentLocations(Location location)  
  112.    {  
  113.      List<Location> free = new LinkedList<Location>();  
  114.      List<Location> adjacent = adjacentLocations(location);  
  115.      for(Location next : adjacent) {  
  116.        if(getObjectAt(next) == null) {  
  117.          free.add(next);  
  118.        }  
  119.      }  
  120.      return free;  
  121.    }  
  122.    /**  
  123.     * Try to find a free location that is adjacent to the  
  124.     * given location. If there is none, return null.  
  125.     * The returned location will be within the valid bounds  
  126.     * of the field.  
  127.     * @param location The location from which to generate an adjacency.  
  128.     * @return A valid location within the grid area.  
  129.     */  
  130.    public Location freeAdjacentLocation(Location location)  
  131.    {  
  132.      // The available free ones.  
  133.      List<Location> free = getFreeAdjacentLocations(location);  
  134.      if(free.size() > 0) {  
  135.        return free.get(0);  
  136.      }  
  137.      else {  
  138.        return null;  
  139.      }  
  140.    }  
  141.    /**  
  142.     * Return a shuffled list of locations adjacent to the given one.  
  143.     * The list will not include the location itself.  
  144.     * All locations will lie within the grid.  
  145.     * @param location The location from which to generate adjacencies.  
  146.     * @return A list of locations adjacent to that given.  
  147.     */  
  148.    public List<Location> adjacentLocations(Location location)  
  149.    {  
  150.      assert location != null : "Null location passed to adjacentLocations";  
  151.      // The list of locations to be returned.  
  152.      List<Location> locations = new LinkedList<Location>();  
  153.      if(location != null) {  
  154.        int row = location.getRow();  
  155.        int col = location.getCol();  
  156.        for(int roffset = -1; roffset <= 1; roffset++) {  
  157.          int nextRow = row + roffset;  
  158.          if(nextRow >= 0 && nextRow < depth) {  
  159.            for(int coffset = -1; coffset <= 1; coffset++) {  
  160.              int nextCol = col + coffset;  
  161.              // Exclude invalid locations and the original location.  
  162.              if(nextCol >= 0 && nextCol < width && (roffset != 0 || coffset != 0)) {  
  163.                locations.add(new Location(nextRow, nextCol));  
  164.              }  
  165.            }  
  166.          }  
  167.        }  
  168.        // Shuffle the list. Several other methods rely on the list  
  169.        // being in a random order.  
  170.        Collections.shuffle(locations, rand);  
  171.      }  
  172.      return locations;  
  173.    }  
  174.    /**  
  175.     * Return the depth of the field.  
  176.     * @return The depth of the field.  
  177.     */  
  178.    public int getDepth()  
  179.    {  
  180.      return depth;  
  181.    }  
  182.    /**  
  183.     * Return the width of the field.  
  184.     * @return The width of the field.  
  185.     */  
  186.    public int getWidth()  
  187.    {  
  188.      return width;  
  189.    }  
  190.  }