Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wood.competition;
- import wood.game.TurnAction;
- import wood.strategy.PlayerBoardView;
- import wood.tiles.TileType;
- import wood.util.DistanceUtilities;
- import java.awt.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- /**
- * This is just an example that you can submit multiple classes to the competition.
- * If your strategy relies on any other classes that were not initially given to you, they must also be in the
- * competition package or they will not be submitted/compiled.
- */
- public class WoodHelper {
- /**
- * Returns if the point is a valid location.
- *
- * Copied from GameBoard class in game package.
- */
- public static boolean isValidLocation(Point location, int boardSize) {
- return isValidLocation(location.x, location.y, boardSize);
- }
- public static boolean isValidLocation(int x, int y, int boardSize) {
- int xIndex = x;
- int yIndex = (boardSize - 1) - y;
- boolean xIndexInBounds = (xIndex >= 0 && xIndex < boardSize);
- boolean yIndexInBounds = (yIndex >= 0 && yIndex < boardSize);
- return xIndexInBounds && yIndexInBounds;
- }
- /**
- * Creates a hash map that maps tile types to the corresponding turn action to get to that tile for all of
- * a player's adjacent actions.
- * @param boardView the board view
- * @return hash map
- */
- public static HashMap<TileType, TurnAction> getAdjacentTiles(PlayerBoardView boardView, int boardSize) {
- if (boardView == null) {
- return null;
- }
- HashMap<TileType, TurnAction> adjacentTiles = new HashMap<>();
- Point yourLocation = boardView.getYourLocation();
- for (TurnAction movement : WoodStrategy.getMovementEnums()) {
- Point newPoint = new Point();
- switch (movement) {
- case MOVE_DOWN: {
- newPoint.setLocation(yourLocation.getX(), yourLocation.getY() - 1);
- break;
- }
- case MOVE_UP: {
- newPoint.setLocation(yourLocation.getX(), yourLocation.getY() + 1);
- break;
- }
- case MOVE_LEFT: {
- newPoint.setLocation(yourLocation.getX() - 1, yourLocation.getY());
- break;
- }
- case MOVE_RIGHT: {
- newPoint.setLocation(yourLocation.getX() + 1, yourLocation.getY());
- break;
- }
- }
- if (isValidLocation(newPoint, boardSize)) {
- adjacentTiles.put(boardView.getTileTypeAtLocation(newPoint), movement);
- }
- }
- return adjacentTiles;
- }
- /**
- * Returns the TurnAction to move the player towards a target location (ie: a seed, a tree, home)
- */
- public static TurnAction moveTowardTarget(PlayerBoardView boardView, Point targetPosition) {
- if (boardView == null || targetPosition == null) {
- return null;
- }
- if (boardView.getYourLocation().getX() < targetPosition.getX()) {
- return TurnAction.MOVE_RIGHT;
- } else if (boardView.getYourLocation().getX() > targetPosition.getX()) {
- return TurnAction.MOVE_LEFT;
- }
- if (boardView.getYourLocation().getY() < targetPosition.getY()) {
- return TurnAction.MOVE_UP;
- }
- return TurnAction.MOVE_DOWN;
- }
- /**
- * Gets the closest point on the board given a list of points.
- */
- public static Point getClosest(PlayerBoardView boardView, List<Point> listOfPoints, int boardSize) {
- if (boardView == null || listOfPoints == null || boardSize == 0) {
- return null;
- }
- Point closest = new Point();
- int shortestDistance = boardSize + boardSize;
- for (Point point : listOfPoints) {
- int tempDistance = DistanceUtilities.getManhattanDistance(point, boardView.getYourLocation());
- if (tempDistance < shortestDistance) {
- shortestDistance = tempDistance;
- closest = point;
- }
- }
- return closest;
- }
- /**
- * Updates the list of all points containing seeds at the start of every round.
- */
- public static List<Point> updateListOfItems(PlayerBoardView boardView, int boardSize, TileType tileType) {
- if (boardView == null || boardSize == 0 || tileType == null) {
- return null;
- }
- List<Point> listOfSeedPoints = new ArrayList<>();
- for (int i = 0; i < boardSize; i++) {
- for (int j = 0; j < boardSize; j++) {
- if (boardView.getTileTypeAtLocation(i, j) == tileType) {
- listOfSeedPoints.add(new Point(i, j));
- }
- }
- }
- return listOfSeedPoints;
- }
- /**
- * Moves player toward home if he has seeds and is ready to plant them.
- */
- public static TurnAction moveTowardHome(PlayerBoardView boardView, int boardSize, Point startTileLocation) {
- HashMap<TileType, TurnAction> adjacentTiles = WoodHelper.getAdjacentTiles(boardView, boardSize);
- // If you're not next to your starting tile yet.
- if (!adjacentTiles.containsKey(TileType.START)) {
- return WoodHelper.moveTowardTarget(boardView, startTileLocation);
- }
- return null;
- }
- /**
- * Returns true if player is adjacent to opponent's starting tile. Returns false otherwise.
- */
- public static boolean isTooClose(Point yourLocation, Point opponentStartTile) {
- Point rightOpponentTile = new Point((int) opponentStartTile.getX() + 1, (int) opponentStartTile.getY());
- Point leftOpponentTile = new Point((int) opponentStartTile.getX(), (int) opponentStartTile.getY() + 1);
- if (yourLocation.equals(rightOpponentTile)
- || yourLocation.equals(leftOpponentTile)) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement