public static Tile bestBarbarianMove(Unit currUnit) { if (!(currUnit instanceof Barbarian)) return null; int newX, newY; int currMax = -999, curr; int currX = currUnit.getX(), currY = currUnit.getY(); Tile newTile = null; List<Tile> arrTiles = new LinkedList<Tile>(); for (int k = 0; k < 8; ++k) { newX = currX + nextTiles[k][0]; newY = currY + nextTiles[k][1]; newTile = Grid.getTile(newX, newY); if (newTile == null) continue; if (newTile.isHasBase() && newTile.isBarbaric()) continue; if (newTile.hasUnit() && newTile.getUnit() instanceof Barbarian) continue; curr = dfsBarbarian(newTile, 3); if (currMax < curr) { currMax = curr; arrTiles.clear(); arrTiles.add(newTile); } else if (currMax == curr) { arrTiles.add(newTile); } } if (arrTiles.size() == 0) return null; int currNum = (int) (Math.random() * arrTiles.size()); return (Tile) arrTiles.toArray()[currNum]; }
public static int getTileDirection(Tile currTile, Tile nextTile) { int currX = currTile.getX(), currY = currTile.getY(); int nextX = nextTile.getX(), nextY = nextTile.getY(); int newX, newY; for (int k = 0; k < 8; ++k) { newX = currX + nextTiles[k][0]; newY = currY + nextTiles[k][1]; if (nextX == newX && nextY == newY) { return k; } } return -1; }
public static boolean nextTo(Unit currUnit, Tile destination) { int currX = currUnit.getX(), currY = currUnit.getY(); int nextX = destination.getX(), nextY = destination.getY(); int newX, newY; for (int k = 0; k < 8; ++k) { newX = currX + nextTiles[k][0]; newY = currY + nextTiles[k][1]; if (newX == nextX && newY == nextY) { return true; } } return false; }
public static Tile nearestEmptyTile(Tile currTile) { int currX = currTile.getX(), currY = currTile.getY(), newX, newY; if (!currTile.hasUnit()) { return currTile; } for (int k = 0; k < 8; ++k) { newX = currX + TileUtils.nextTiles[k][0]; newY = currY + TileUtils.nextTiles[k][1]; if (newX < 0 || newX >= Grid.getSizeX() || newY < 0 || newY >= Grid.getSizeY()) continue; if (!Grid.masterGrid[newX][newY].hasUnit()) { return Grid.getTile(newX, newY); } } return null; }
public static Tile calculateNextMove(Tile currTile, Tile destination) { int newX, newY, currDistance, bestDistance = 9999999; int currX = currTile.getX(), currY = currTile.getY(); int endX = destination.getX(), endY = destination.getY(); Tile bestTile = null, newTile = null; if (currX == endX && currY == endY) return null; for (int k = 0; k < 8; ++k) { newX = currX + nextTiles[k][0]; newY = currY + nextTiles[k][1]; newTile = Grid.getTile(newX, newY); if (!newTile.hasUnit() && !newTile.isHasBase()) { currDistance = Math.max(Math.abs(newX - endX), Math.abs(newY - endY)); // System.out.println("(" + newX + ", " + newY + ")-> " + currDistance); if (bestDistance > currDistance) { bestDistance = currDistance; bestTile = newTile; } } } return bestTile; }
private static int dfsBarbarian(Tile currTile, int depth) { if (depth == 0) return 0; if (currTile == null) return 0; if (currTile.hasUnit()) { Unit currUnit = currTile.getUnit(); if (currUnit instanceof Barbarian) return 0; return 3; } if (currTile.isHasBase()) { if (currTile.isBarbaric()) return 0; return 10; } int newX, newY; int currMax = -999; for (int k = 0; k < 8; ++k) { newX = currTile.getX() + nextTiles[k][0]; newY = currTile.getY() + nextTiles[k][1]; currMax = Math.max(currMax, dfsBarbarian(Grid.getTile(newX, newY), depth - 1) - 1); } return currMax; }