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]; }
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; }