Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wood.competition; // This is the "competition" package
- import wood.game.TurnAction;
- import wood.item.InventoryItem;
- import wood.strategy.PlayerBoardView;
- import wood.strategy.WoodPlayerStrategy;
- import wood.tiles.TileType;
- // ^ These classes were provided to you, they do not need to be in the competition package
- import java.awt.*;
- import java.util.*;
- import java.util.List;
- // ^ These classes are a part of Java, they also do not need to be in the competition package
- /**
- * Because this class is in the competition package, it will be compiled and run in the competition.
- * You cannot put more than one WoodPlayerStrategy implementation in the competition package, so you must
- * either delete or modify this class in order to submit your strategy implementation
- */
- public class WoodStrategy implements WoodPlayerStrategy {
- private int boardSize;
- private int maxInventorySize;
- private int winningScore;
- private Point startTileLocation;
- private boolean isRedPlayer;
- private Random random;
- private Point opponentStartTile = new Point();
- // Records if the player is currently searching for seeds, planting, or has a seed in his inventory
- private boolean isSearching;
- private boolean isPlanting;
- private boolean hasSeed;
- private boolean isMovingAway;
- private Point away;
- private final int BEST_CYCLE_NUM = 3;
- private final int BEST_WAITING_TIME = 135;
- private final int MAX_TURNS = 1000;
- private int turnNumber;
- private int turnNumberBeforeWaiting;
- private int inventorySize;
- // Records the cycle of pick up seeds, plant trees, and cut trees that the player is on
- private int cycleNum;
- // List of all the movement enums
- private static List<TurnAction> movementEnums = new ArrayList<>();
- // Remember's the player and opponent's player scores for testing purposes
- private static int totalPlayerScore;
- private static int totalOpponentScore;
- // Getters and Setters created for testing purposes.
- public static List<TurnAction> getMovementEnums() {
- return movementEnums;
- }
- public int getInventorySize() {
- return this.inventorySize;
- }
- public Point getStartingPoint() {
- return this.startTileLocation;
- }
- public static int getTotalPlayerScore() {
- return totalPlayerScore;
- }
- public WoodStrategy() {
- this.isSearching = true;
- this.isPlanting = false;
- this.hasSeed = false;
- this.isMovingAway = false;
- this.cycleNum = 0;
- movementEnums.clear();
- movementEnums.add(TurnAction.MOVE_DOWN);
- movementEnums.add(TurnAction.MOVE_UP);
- movementEnums.add(TurnAction.MOVE_LEFT);
- movementEnums.add(TurnAction.MOVE_RIGHT);
- }
- @Override
- public void initialize(int boardSize, int maxInventorySize, int winningScore, Point startTileLocation,
- boolean isRedPlayer, Random random) {
- this.boardSize = boardSize;
- this.maxInventorySize = maxInventorySize;
- this.winningScore = winningScore;
- this.startTileLocation = startTileLocation;
- this.isRedPlayer = isRedPlayer;
- this.random = random;
- if (startTileLocation.getX() == 0 && startTileLocation.getY() == 0) {
- opponentStartTile.setLocation(boardSize - 1, boardSize - 1);
- } else {
- opponentStartTile.setLocation(0,0);
- }
- this.inventorySize = 0;
- this.turnNumber = 0;
- away = new Point(boardSize / 2,boardSize / 2);
- }
- /**
- * Searches for a seed.
- * @return the turn action to perform
- */
- public TurnAction searchForSeed(PlayerBoardView boardView) {
- if (boardView == null) {
- return null;
- }
- List<Point> listOfSeedPoints = WoodHelper.updateListOfItems(boardView, boardSize, TileType.SEED);
- // Move toward home once inventory is full of seeds.
- if (inventorySize == maxInventorySize) {
- this.isSearching = false;
- return WoodHelper.moveTowardTarget(boardView, startTileLocation);
- }
- // Pick up if it's a seed tile
- if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.SEED) {
- hasSeed = true;
- return TurnAction.PICK_UP;
- }
- Point closestSeed = WoodHelper.getClosest(boardView, listOfSeedPoints, boardSize);
- return WoodHelper.moveTowardTarget(boardView, closestSeed);
- }
- /**
- * Plants the seeds. Begins cutting trees during every fourth cycle for maximum efficiency (against random).
- */
- public TurnAction plantSeeds(PlayerBoardView boardView) {
- if (boardView == null) {
- return null;
- }
- // Plant seed if you're on empty tile.
- if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.EMPTY) {
- if (inventorySize == 1) {
- cycleNum++;
- hasSeed = false;
- isPlanting = false;
- if (cycleNum % BEST_CYCLE_NUM != 0) {
- isSearching = true;
- } else {
- turnNumberBeforeWaiting = turnNumber;
- }
- }
- inventorySize--;
- return TurnAction.PLANT_SEED;
- }
- return movementEnums.get(random.nextInt(movementEnums.size()));
- }
- /**
- * During cut tree, if player reaches the start tile.
- */
- public TurnAction reachedStartTile(PlayerBoardView boardView, List<Point> listOfTrees) {
- if (listOfTrees.size() > 0) {
- inventorySize = 0;
- // Look for more trees to cut
- Point closest = WoodHelper.getClosest(boardView, listOfTrees, boardSize);
- return WoodHelper.moveTowardTarget(boardView, closest);
- }
- inventorySize = 0;
- isSearching = true;
- return searchForSeed(boardView);
- }
- /**
- * Cuts the wood.
- */
- public TurnAction cutWood(PlayerBoardView boardView) {
- if (boardView == null) {
- return null;
- }
- List<Point> listOfTrees = WoodHelper.updateListOfItems(boardView, boardSize, TileType.TREE);
- // If you've reached the start tile
- if (boardView.getYourLocation().equals(startTileLocation)) {
- return reachedStartTile(boardView, listOfTrees);
- }
- // Move towards home if inventory is full
- if (inventorySize == maxInventorySize) {
- return WoodHelper.moveTowardTarget(boardView, startTileLocation);
- } else if (boardView.getTileTypeAtLocation(boardView.getYourLocation()) == TileType.TREE) {
- return TurnAction.CUT_TREE;
- }
- // Look for more trees to cut
- Point closest = WoodHelper.getClosest(boardView, listOfTrees, boardSize);
- return WoodHelper.moveTowardTarget(boardView, closest);
- }
- /**
- * Avoids the opponent start tile if player gets too close
- */
- public TurnAction avoidOpponent(PlayerBoardView boardView) {
- boolean isTooClose = WoodHelper.isTooClose(boardView.getYourLocation(), opponentStartTile);
- if (isTooClose) {
- isMovingAway = true;
- return WoodHelper.moveTowardTarget(boardView, away);
- } else if (isMovingAway) {
- if (!boardView.getYourLocation().equals(away)) {
- return WoodHelper.moveTowardTarget(boardView, away);
- }
- isMovingAway = false;
- isSearching = true;
- }
- return null;
- }
- @Override
- public TurnAction getTurnAction(PlayerBoardView boardView, boolean isRedTurn) {
- if (boardView == null) {
- return null;
- }
- turnNumber++;
- if (avoidOpponent(boardView) != null) {
- return avoidOpponent(boardView);
- }
- if (isSearching) {
- return searchForSeed(boardView);
- } else if (!isPlanting && hasSeed) {
- // Move towards home if your inventory is full and you're getting ready to plant
- TurnAction moveAction = WoodHelper.moveTowardHome(boardView, boardSize, startTileLocation);
- if (moveAction != null) {
- return moveAction;
- }
- // If you are next to your starting tile, start planting.
- isPlanting = true;
- }
- if (isPlanting) {
- return plantSeeds(boardView);
- }
- // Do nothing for a few turns, wait for trees to grow
- if (turnNumber < MAX_TURNS - BEST_WAITING_TIME && turnNumber < turnNumberBeforeWaiting + BEST_WAITING_TIME) {
- return null;
- }
- return cutWood(boardView);
- }
- @Override
- public void receiveItem(InventoryItem itemReceived) {
- if (inventorySize < maxInventorySize) {
- inventorySize++;
- }
- }
- @Override
- public String getName() {
- return "I wood love to die";
- }
- @Override
- public void endRound(int pointsScored, int opponentPointsScored) {
- totalPlayerScore += pointsScored;
- totalOpponentScore += opponentPointsScored;
- inventorySize = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement