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