Advertisement
dyang5200

Untitled

Feb 26th, 2019
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. package wood.competition; // This is the "competition" package
  2. import wood.game.TurnAction;
  3. import wood.item.InventoryItem;
  4. import wood.strategy.PlayerBoardView;
  5. import wood.strategy.WoodPlayerStrategy;
  6. import wood.tiles.TileType;
  7. // ^ These classes were provided to you, they do not need to be in the competition package
  8.  
  9. import java.awt.*;
  10. import java.util.*;
  11. import java.util.List;
  12. // ^ These classes are a part of Java, they also do not need to be in the competition package
  13.  
  14. /**
  15. * Because this class is in the competition package, it will be compiled and run in the competition.
  16. * You cannot put more than one WoodPlayerStrategy implementation in the competition package, so you must
  17. * either delete or modify this class in order to submit your strategy implementation
  18. */
  19. public class WoodStrategy implements WoodPlayerStrategy {
  20. private int boardSize;
  21. private int maxInventorySize;
  22. private int winningScore;
  23. private Point startTileLocation;
  24. private boolean isRedPlayer;
  25. private Random random;
  26.  
  27. private Point opponentStartTile = new Point();
  28.  
  29. // Records if the player is currently searching for seeds, planting, or has a seed in his inventory
  30. private boolean isSearching;
  31. private boolean isPlanting;
  32. private boolean hasSeed;
  33. private boolean isMovingAway;
  34. private Point away;
  35.  
  36. private final int BEST_CYCLE_NUM = 3;
  37. private final int BEST_WAITING_TIME = 135;
  38. private final int MAX_TURNS = 1000;
  39. private int turnNumber;
  40. private int turnNumberBeforeWaiting;
  41.  
  42. private int inventorySize;
  43.  
  44. // Records the cycle of pick up seeds, plant trees, and cut trees that the player is on
  45. private int cycleNum;
  46.  
  47. // List of all the movement enums
  48. private static List<TurnAction> movementEnums = new ArrayList<>();
  49.  
  50. // Remember's the player and opponent's player scores for testing purposes
  51. private static int totalPlayerScore;
  52. private static int totalOpponentScore;
  53.  
  54. // Getters and Setters created for testing purposes.
  55.  
  56. public static List<TurnAction> getMovementEnums() {
  57. return movementEnums;
  58. }
  59.  
  60. public int getInventorySize() {
  61. return this.inventorySize;
  62. }
  63.  
  64. public Point getStartingPoint() {
  65. return this.startTileLocation;
  66. }
  67.  
  68. public static int getTotalPlayerScore() {
  69. return totalPlayerScore;
  70. }
  71.  
  72. public WoodStrategy() {
  73. this.isSearching = true;
  74. this.isPlanting = false;
  75. this.hasSeed = false;
  76. this.isMovingAway = false;
  77. this.cycleNum = 0;
  78.  
  79. movementEnums.clear();
  80. movementEnums.add(TurnAction.MOVE_DOWN);
  81. movementEnums.add(TurnAction.MOVE_UP);
  82. movementEnums.add(TurnAction.MOVE_LEFT);
  83. movementEnums.add(TurnAction.MOVE_RIGHT);
  84. }
  85.  
  86. @Override
  87. public void initialize(int boardSize, int maxInventorySize, int winningScore, Point startTileLocation,
  88. boolean isRedPlayer, Random random) {
  89. this.boardSize = boardSize;
  90. this.maxInventorySize = maxInventorySize;
  91. this.winningScore = winningScore;
  92. this.startTileLocation = startTileLocation;
  93. this.isRedPlayer = isRedPlayer;
  94. this.random = random;
  95.  
  96. if (startTileLocation.getX() == 0 && startTileLocation.getY() == 0) {
  97. opponentStartTile.setLocation(boardSize - 1, boardSize - 1);
  98. } else {
  99. opponentStartTile.setLocation(0,0);
  100. }
  101.  
  102. this.inventorySize = 0;
  103. this.turnNumber = 0;
  104. away = new Point(boardSize / 2,boardSize / 2);
  105. }
  106.  
  107. /**
  108. * Searches for a seed.
  109. * @return the turn action to perform
  110. */
  111. public TurnAction searchForSeed(PlayerBoardView boardView) {
  112. if (boardView == null) {
  113. return null;
  114. }
  115. List<Point> listOfSeedPoints = WoodHelper.updateListOfItems(boardView, boardSize, TileType.SEED);
  116.  
  117. // Move toward home once inventory is full of seeds.
  118. if (inventorySize == maxInventorySize) {
  119. this.isSearching = false;
  120. return WoodHelper.moveTowardTarget(boardView, startTileLocation);
  121. }
  122. // Pick up if it's a seed tile
  123. if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.SEED) {
  124. hasSeed = true;
  125. return TurnAction.PICK_UP;
  126. }
  127.  
  128. Point closestSeed = WoodHelper.getClosest(boardView, listOfSeedPoints, boardSize);
  129. return WoodHelper.moveTowardTarget(boardView, closestSeed);
  130. }
  131.  
  132. /**
  133. * Plants the seeds. Begins cutting trees during every fourth cycle for maximum efficiency (against random).
  134. */
  135. public TurnAction plantSeeds(PlayerBoardView boardView) {
  136. if (boardView == null) {
  137. return null;
  138. }
  139.  
  140. // Plant seed if you're on empty tile.
  141. if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.EMPTY) {
  142. if (inventorySize == 1) {
  143. cycleNum++;
  144. hasSeed = false;
  145. isPlanting = false;
  146.  
  147. if (cycleNum % BEST_CYCLE_NUM != 0) {
  148. isSearching = true;
  149. } else {
  150. turnNumberBeforeWaiting = turnNumber;
  151. }
  152. }
  153. inventorySize--;
  154. return TurnAction.PLANT_SEED;
  155. }
  156. return movementEnums.get(random.nextInt(movementEnums.size()));
  157. }
  158.  
  159. /**
  160. * During cut tree, if player reaches the start tile.
  161. */
  162. public TurnAction reachedStartTile(PlayerBoardView boardView, List<Point> listOfTrees) {
  163. if (listOfTrees.size() > 0) {
  164. inventorySize = 0;
  165. // Look for more trees to cut
  166. Point closest = WoodHelper.getClosest(boardView, listOfTrees, boardSize);
  167. return WoodHelper.moveTowardTarget(boardView, closest);
  168. }
  169. inventorySize = 0;
  170. isSearching = true;
  171. return searchForSeed(boardView);
  172. }
  173.  
  174. /**
  175. * Cuts the wood.
  176. */
  177. public TurnAction cutWood(PlayerBoardView boardView) {
  178. if (boardView == null) {
  179. return null;
  180. }
  181. List<Point> listOfTrees = WoodHelper.updateListOfItems(boardView, boardSize, TileType.TREE);
  182.  
  183. // If you've reached the start tile
  184. if (boardView.getYourLocation().equals(startTileLocation)) {
  185. return reachedStartTile(boardView, listOfTrees);
  186. }
  187.  
  188. // Move towards home if inventory is full
  189. if (inventorySize == maxInventorySize) {
  190. return WoodHelper.moveTowardTarget(boardView, startTileLocation);
  191. } else if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.TREE) {
  192. return TurnAction.CUT_TREE;
  193. }
  194.  
  195. // Look for more trees to cut
  196. Point closest = WoodHelper.getClosest(boardView, listOfTrees, boardSize);
  197. return WoodHelper.moveTowardTarget(boardView, closest);
  198. }
  199.  
  200. /**
  201. * Avoids the opponent start tile if player gets too close
  202. */
  203. public TurnAction avoidOpponent(PlayerBoardView boardView) {
  204. boolean isTooClose = WoodHelper.isTooClose(boardView.getYourLocation(), opponentStartTile);
  205. if (isTooClose) {
  206. isMovingAway = true;
  207. return WoodHelper.moveTowardTarget(boardView, away);
  208. } else if (isMovingAway) {
  209. if (!boardView.getYourLocation().equals(away)) {
  210. return WoodHelper.moveTowardTarget(boardView, away);
  211. }
  212. isMovingAway = false;
  213. isSearching = true;
  214. }
  215. return null;
  216. }
  217.  
  218. @Override
  219. public TurnAction getTurnAction(PlayerBoardView boardView, boolean isRedTurn) {
  220. if (boardView == null) {
  221. return null;
  222. }
  223.  
  224. turnNumber++;
  225.  
  226. if (avoidOpponent(boardView) != null) {
  227. return avoidOpponent(boardView);
  228. }
  229.  
  230. if (isSearching) {
  231. return searchForSeed(boardView);
  232. } else if (!isPlanting && hasSeed) {
  233.  
  234. // Move towards home if your inventory is full and you're getting ready to plant
  235. TurnAction moveAction = WoodHelper.moveTowardHome(boardView, boardSize, startTileLocation);
  236. if (moveAction != null) {
  237. return moveAction;
  238. }
  239.  
  240. // If you are next to your starting tile, start planting.
  241. isPlanting = true;
  242. }
  243. if (isPlanting) {
  244. return plantSeeds(boardView);
  245. }
  246.  
  247. // Do nothing for a few turns, wait for trees to grow
  248. if (turnNumber < MAX_TURNS - BEST_WAITING_TIME && turnNumber < turnNumberBeforeWaiting + BEST_WAITING_TIME) {
  249. return null;
  250. }
  251. return cutWood(boardView);
  252. }
  253.  
  254. @Override
  255. public void receiveItem(InventoryItem itemReceived) {
  256. if (inventorySize < maxInventorySize) {
  257. inventorySize++;
  258. }
  259. }
  260.  
  261. @Override
  262. public String getName() {
  263. return "I wood love to die";
  264. }
  265.  
  266. @Override
  267. public void endRound(int pointsScored, int opponentPointsScored) {
  268. totalPlayerScore += pointsScored;
  269. totalOpponentScore += opponentPointsScored;
  270. inventorySize = 0;
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement