RovkirHexus

AbstractGrid

Jun 8th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package feAttempt;
  2.  
  3. /*
  4.  * AP(r) Computer Science GridWorld Case Study:
  5.  * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
  6.  *
  7.  * This code is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This code is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * @author Cay Horstmann
  17.  */
  18.  
  19. import java.util.ArrayList;
  20.  
  21. /**
  22.  * <code>AbstractGrid</code> contains the methods that are common to grid
  23.  * implementations. <br />
  24.  * The implementation of this class is testable on the AP CS AB exam.
  25.  */
  26. public abstract class AbstractGrid<E> implements Grid<E>
  27. {
  28.   public ArrayList<E> getNeighbors(Location loc)
  29.   {
  30.     ArrayList<E> neighbors = new ArrayList<E>();
  31.     for (Location neighborLoc : getOccupiedAdjacentLocations(loc))
  32.       neighbors.add(get(neighborLoc));
  33.     return neighbors;
  34.   }
  35.  
  36.   public ArrayList<Location> getValidAdjacentLocations(Location loc)
  37.   {
  38.     ArrayList<Location> locs = new ArrayList<Location>();
  39.    
  40.     int d = Location.NORTH;
  41.     for (int i = 0; i < Location.FULL_CIRCLE / Location.HALF_RIGHT; i++)
  42.     {
  43.       Location neighborLoc = loc.getAdjacentLocation(d);
  44.       if (isValid(neighborLoc))
  45.         locs.add(neighborLoc);
  46.       d = d + Location.HALF_RIGHT;
  47.     }
  48.     return locs;
  49.   }
  50.  
  51.   public ArrayList<Location> getEmptyAdjacentLocations(Location loc)
  52.   {
  53.     ArrayList<Location> locs = new ArrayList<Location>();
  54.     for (Location neighborLoc : getValidAdjacentLocations(loc))
  55.     {
  56.       if (get(neighborLoc) == null)
  57.         locs.add(neighborLoc);
  58.     }
  59.     return locs;
  60.   }
  61.  
  62.   public ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
  63.   {
  64.     ArrayList<Location> locs = new ArrayList<Location>();
  65.     for (Location neighborLoc : getValidAdjacentLocations(loc))
  66.     {
  67.       if (get(neighborLoc) != null)
  68.         locs.add(neighborLoc);
  69.     }
  70.     return locs;
  71.   }
  72.  
  73.   /**
  74.    * Creates a string that describes this grid.
  75.    * @return a string with descriptions of all objects in this grid (not
  76.    * necessarily in any particular order), in the format {loc=obj, loc=obj,
  77.    * ...}
  78.    */
  79.   public String toString()
  80.   {
  81.     String s = "{";
  82.     for (Location loc : getOccupiedLocations())
  83.     {
  84.       if (s.length() > 1)
  85.         s += ", ";
  86.       s += loc + "=" + get(loc);
  87.     }
  88.     return s + "}";
  89.   }
  90. }
Add Comment
Please, Sign In to add comment