Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cern.ais.gridwars;
- import cern.ais.gridwars.bot.PlayerBot;
- import cern.ais.gridwars.command.MovementCommand;
- import java.util.ArrayList;
- import java.util.List;
- public class SPSUbot implements PlayerBot
- {
- private final static short MIN_TO_GROW = 5;
- Coordinates correct (UniverseView universe, int x, int y)
- {
- if (x < 0)
- x = GameConstants.UNIVERSE_SIZE + x - 1;
- if (y < 0)
- y = GameConstants.UNIVERSE_SIZE + y - 1;
- return universe.getCoordinates(x, y);
- }
- private int myMass (UniverseView universe)
- {
- int mass = 0;
- for (Coordinates a : universe.getMyCells())
- {
- mass += universe.getPopulation(a);
- }
- return mass;
- }
- private int hisMass (UniverseView universe)
- {
- int mass = 0;
- for (int x = 0; x < GameConstants.UNIVERSE_SIZE; x ++)
- {
- for (int y = 0; y < GameConstants.UNIVERSE_SIZE; y ++)
- {
- if (!universe.belongsToMe(x, y))
- {
- mass += universe.getPopulation(x, y);
- }
- }
- }
- return mass;
- }
- List<MovementCommand.Direction> getEmptyNeighbours (UniverseView universe, Coordinates Y)
- {
- List<MovementCommand.Direction> result = new ArrayList<MovementCommand.Direction> ();
- if (universe.isEmpty(correct(universe, Y.getX(), Y.getY()-1)))
- result.add(MovementCommand.Direction.LEFT);
- if (universe.isEmpty(correct(universe, Y.getX(), Y.getY()+1)))
- result.add(MovementCommand.Direction.RIGHT);
- if (universe.isEmpty(correct(universe, Y.getX()+1, Y.getY())))
- result.add(MovementCommand.Direction.UP);
- if (universe.isEmpty(correct(universe, Y.getX()-1, Y.getY())))
- result.add(MovementCommand.Direction.DOWN);
- return result;
- }
- void firstStrategy (UniverseView universe, List<MovementCommand> move)
- {
- List<Coordinates> myList = universe.getMyCells();
- List<MovementCommand.Direction> Neighbours;
- long weight = 0;
- int amount = 0;
- int pace = 5;
- long howMuch [] = {0, 0, 0, 0};
- for (Coordinates temp : myList)
- {
- pace = 5;
- for (int i = 0; i < 4; i ++)
- howMuch [i] = 0;
- weight = universe.getPopulation(temp);
- Neighbours = getEmptyNeighbours (universe, temp);
- amount = Neighbours.size();
- if (amount > 0)
- {
- for (int i = 0; (weight > pace) && (i < Neighbours.size()); i ++)
- {
- howMuch[i] += pace;
- weight -= pace;
- if (i == Neighbours.size()-1)
- {
- pace = 10;
- i = 0;
- }
- }
- if ((weight % 10 < 5) && (weight % 10 != 0))
- {
- for (int i = 0; (weight > MIN_TO_GROW) && (i < Neighbours.size()); i ++)
- {
- howMuch[i] ++;
- weight --;
- if (i == Neighbours.size()-1)
- {
- i = 0;
- }
- }
- }
- }
- else
- {
- if ((((weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY()))) > 10)) && universe.belongsToMe(correct(universe, temp.getX()-1, temp.getY())))
- {
- Neighbours.add(MovementCommand.Direction.LEFT);
- howMuch[0] += (weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY())))/2;
- weight -= (weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY())))/2;
- }
- if (((weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY()))) > 10) && (universe.belongsToMe(correct(universe, temp.getX()+1, temp.getY()))))
- {
- Neighbours.add(MovementCommand.Direction.RIGHT);
- howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY())))/2;
- weight -= (weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY())))/2;
- }
- if (((weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1))) > 10) && (universe.belongsToMe(correct(universe, temp.getX(), temp.getY()+1))))
- {
- Neighbours.add(MovementCommand.Direction.UP);
- howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1)))/2;
- weight -= (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1)))/2;
- }
- if (((weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1))) > 10) && (universe.belongsToMe(correct(universe, temp.getX(), temp.getY()-1))))
- {
- Neighbours.add(MovementCommand.Direction.DOWN);
- howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1)))/2;
- weight -= (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1)))/2;
- }
- }
- for (int i = 0; i < Neighbours.size(); i ++)
- {
- move.add(new MovementCommand (temp, Neighbours.get(i), howMuch[i]));
- }
- }
- }
- void secondStrategy (UniverseView universe, List<MovementCommand> move)
- {
- for (Coordinates a : universe.getMyCells())
- {
- move.add(new MovementCommand(a, MovementCommand.Direction.UP, universe.getPopulation(a)));
- }
- }
- @Override public void getNextCommands(UniverseView universeView, List<MovementCommand> movementCommands)
- {
- if (myMass (universeView) / hisMass (universeView) < 1.1)
- {
- firstStrategy (universeView, movementCommands);
- }
- else
- {
- secondStrategy (universeView, movementCommands);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment