Advertisement
dyang5200

Untitled

Feb 26th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. package wood.competition;
  2. import wood.game.TurnAction;
  3. import wood.strategy.PlayerBoardView;
  4. import wood.tiles.TileType;
  5. import wood.util.DistanceUtilities;
  6.  
  7. import java.awt.*;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11.  
  12. /**
  13. * This is just an example that you can submit multiple classes to the competition.
  14. * If your strategy relies on any other classes that were not initially given to you, they must also be in the
  15. * competition package or they will not be submitted/compiled.
  16. */
  17. public class WoodHelper {
  18.  
  19. /**
  20. * Returns if the point is a valid location.
  21. *
  22. * Copied from GameBoard class in game package.
  23. */
  24. public static boolean isValidLocation(Point location, int boardSize) {
  25. return isValidLocation(location.x, location.y, boardSize);
  26. }
  27.  
  28. public static boolean isValidLocation(int x, int y, int boardSize) {
  29. int xIndex = x;
  30. int yIndex = (boardSize - 1) - y;
  31.  
  32. boolean xIndexInBounds = (xIndex >= 0 && xIndex < boardSize);
  33. boolean yIndexInBounds = (yIndex >= 0 && yIndex < boardSize);
  34.  
  35. return xIndexInBounds && yIndexInBounds;
  36. }
  37.  
  38. /**
  39. * Creates a hash map that maps tile types to the corresponding turn action to get to that tile for all of
  40. * a player's adjacent actions.
  41. * @param boardView the board view
  42. * @return hash map
  43. */
  44. public static HashMap<TileType, TurnAction> getAdjacentTiles(PlayerBoardView boardView, int boardSize) {
  45. if (boardView == null) {
  46. return null;
  47. }
  48. HashMap<TileType, TurnAction> adjacentTiles = new HashMap<>();
  49. Point yourLocation = boardView.getYourLocation();
  50.  
  51. for (TurnAction movement : WoodStrategy.getMovementEnums()) {
  52. Point newPoint = new Point();
  53. switch (movement) {
  54. case MOVE_DOWN: {
  55. newPoint.setLocation(yourLocation.getX(), yourLocation.getY() - 1);
  56. break;
  57. }
  58. case MOVE_UP: {
  59. newPoint.setLocation(yourLocation.getX(), yourLocation.getY() + 1);
  60. break;
  61. }
  62. case MOVE_LEFT: {
  63. newPoint.setLocation(yourLocation.getX() - 1, yourLocation.getY());
  64. break;
  65. }
  66. case MOVE_RIGHT: {
  67. newPoint.setLocation(yourLocation.getX() + 1, yourLocation.getY());
  68. break;
  69. }
  70. }
  71. if (isValidLocation(newPoint, boardSize)) {
  72. adjacentTiles.put(boardView.getTileTypeAtLocation(newPoint), movement);
  73. }
  74. }
  75. return adjacentTiles;
  76. }
  77.  
  78. /**
  79. * Returns the TurnAction to move the player towards a target location (ie: a seed, a tree, home)
  80. */
  81. public static TurnAction moveTowardTarget(PlayerBoardView boardView, Point targetPosition) {
  82. if (boardView == null || targetPosition == null) {
  83. return null;
  84. }
  85.  
  86. if (boardView.getYourLocation().getX() < targetPosition.getX()) {
  87. return TurnAction.MOVE_RIGHT;
  88. } else if (boardView.getYourLocation().getX() > targetPosition.getX()) {
  89. return TurnAction.MOVE_LEFT;
  90. }
  91.  
  92. if (boardView.getYourLocation().getY() < targetPosition.getY()) {
  93. return TurnAction.MOVE_UP;
  94. }
  95. return TurnAction.MOVE_DOWN;
  96. }
  97.  
  98. /**
  99. * Gets the closest point on the board given a list of points.
  100. */
  101. public static Point getClosest(PlayerBoardView boardView, List<Point> listOfPoints, int boardSize) {
  102. if (boardView == null || listOfPoints == null || boardSize == 0) {
  103. return null;
  104. }
  105. Point closest = new Point();
  106. int shortestDistance = boardSize + boardSize;
  107.  
  108. for (Point point : listOfPoints) {
  109. int tempDistance = DistanceUtilities.getManhattanDistance(point, boardView.getYourLocation());
  110.  
  111. if (tempDistance < shortestDistance) {
  112. shortestDistance = tempDistance;
  113. closest = point;
  114. }
  115. }
  116. return closest;
  117. }
  118.  
  119. /**
  120. * Updates the list of all points containing seeds at the start of every round.
  121. */
  122. public static List<Point> updateListOfItems(PlayerBoardView boardView, int boardSize, TileType tileType) {
  123. if (boardView == null || boardSize == 0 || tileType == null) {
  124. return null;
  125. }
  126. List<Point> listOfSeedPoints = new ArrayList<>();
  127. for (int i = 0; i < boardSize; i++) {
  128. for (int j = 0; j < boardSize; j++) {
  129. if (boardView.getTileTypeAtLocation(i, j) == tileType) {
  130. listOfSeedPoints.add(new Point(i, j));
  131. }
  132. }
  133. }
  134. return listOfSeedPoints;
  135. }
  136.  
  137. /**
  138. * Moves player toward home if he has seeds and is ready to plant them.
  139. */
  140. public static TurnAction moveTowardHome(PlayerBoardView boardView, int boardSize, Point startTileLocation) {
  141. HashMap<TileType, TurnAction> adjacentTiles = WoodHelper.getAdjacentTiles(boardView, boardSize);
  142.  
  143. // If you're not next to your starting tile yet.
  144. if (!adjacentTiles.containsKey(TileType.START)) {
  145. return WoodHelper.moveTowardTarget(boardView, startTileLocation);
  146. }
  147.  
  148. return null;
  149. }
  150.  
  151. /**
  152. * Returns true if player is adjacent to opponent's starting tile. Returns false otherwise.
  153. */
  154. public static boolean isTooClose(Point yourLocation, Point opponentStartTile) {
  155. Point rightOpponentTile = new Point((int) opponentStartTile.getX() + 1, (int) opponentStartTile.getY());
  156. Point leftOpponentTile = new Point((int) opponentStartTile.getX(), (int) opponentStartTile.getY() + 1);
  157.  
  158. if (yourLocation.equals(rightOpponentTile)
  159. || yourLocation.equals(leftOpponentTile)) {
  160. return true;
  161. }
  162. return false;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement