tolord

Bot1

Nov 3rd, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 KB | None | 0 0
  1. package cern.ais.gridwars;
  2.  
  3. import cern.ais.gridwars.bot.PlayerBot;
  4. import cern.ais.gridwars.command.MovementCommand;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class SPSUbot implements PlayerBot
  10. {
  11.     private final static short MIN_TO_GROW = 5;
  12.  
  13.     Coordinates correct (UniverseView universe, int x, int y)
  14.     {
  15.         if (x < 0)
  16.             x = GameConstants.UNIVERSE_SIZE + x - 1;
  17.         if (y < 0)
  18.             y = GameConstants.UNIVERSE_SIZE + y - 1;
  19.         return universe.getCoordinates(x, y);
  20.     }
  21.    
  22.     private int myMass (UniverseView universe)
  23.     {
  24.         int mass = 0;
  25.         for (Coordinates a : universe.getMyCells())
  26.         {
  27.             mass += universe.getPopulation(a);
  28.         }
  29.         return mass;
  30.     }
  31.    
  32.     private int hisMass (UniverseView universe)
  33.     {
  34.         int mass = 0;
  35.         for (int x = 0; x < GameConstants.UNIVERSE_SIZE; x ++)
  36.         {
  37.             for (int y = 0; y < GameConstants.UNIVERSE_SIZE; y ++)
  38.             {
  39.                 if (!universe.belongsToMe(x, y))
  40.                 {
  41.                     mass += universe.getPopulation(x, y);
  42.                 }
  43.             }
  44.         }
  45.         return mass;
  46.     }
  47.    
  48.     List<MovementCommand.Direction> getEmptyNeighbours (UniverseView universe, Coordinates Y)
  49.     {
  50.         List<MovementCommand.Direction> result = new ArrayList<MovementCommand.Direction> ();
  51.         if (universe.isEmpty(correct(universe, Y.getX(), Y.getY()-1)))
  52.             result.add(MovementCommand.Direction.LEFT);
  53.         if (universe.isEmpty(correct(universe, Y.getX(), Y.getY()+1)))
  54.             result.add(MovementCommand.Direction.RIGHT);
  55.         if (universe.isEmpty(correct(universe, Y.getX()+1, Y.getY())))
  56.             result.add(MovementCommand.Direction.UP);
  57.         if (universe.isEmpty(correct(universe, Y.getX()-1, Y.getY())))
  58.             result.add(MovementCommand.Direction.DOWN);
  59.         return result;
  60.     }
  61.        
  62.     void firstStrategy (UniverseView universe, List<MovementCommand> move)
  63.     {
  64.         List<Coordinates> myList = universe.getMyCells();
  65.         List<MovementCommand.Direction> Neighbours;
  66.         long weight = 0;
  67.         int amount = 0;
  68.         int pace = 5;
  69.         long howMuch [] = {0, 0, 0, 0};
  70.         for (Coordinates temp : myList)
  71.         {
  72.             pace = 5;
  73.             for (int i = 0; i < 4; i ++)
  74.                 howMuch [i] = 0;
  75.             weight = universe.getPopulation(temp);
  76.             Neighbours = getEmptyNeighbours (universe, temp);
  77.             amount = Neighbours.size();
  78.             if (amount > 0)
  79.             {
  80.                 for (int i = 0; (weight > pace) && (i < Neighbours.size()); i ++)
  81.                
  82.                 {
  83.                     howMuch[i] += pace;
  84.                     weight -= pace;
  85.                     if (i == Neighbours.size()-1)
  86.                     {
  87.                         pace = 10;
  88.                         i = 0;
  89.                     }
  90.                 }
  91.                 if ((weight % 10 < 5) && (weight % 10 != 0))
  92.                 {
  93.                     for (int i = 0; (weight > MIN_TO_GROW) && (i < Neighbours.size()); i ++)
  94.                     {
  95.                         howMuch[i] ++;
  96.                         weight --;
  97.                         if (i == Neighbours.size()-1)
  98.                         {
  99.                             i = 0;
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.             else
  105.             {
  106.                 if ((((weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY()))) > 10)) && universe.belongsToMe(correct(universe, temp.getX()-1, temp.getY())))
  107.                 {
  108.                     Neighbours.add(MovementCommand.Direction.LEFT);
  109.                     howMuch[0] += (weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY())))/2;
  110.                     weight -= (weight - universe.getPopulation(correct(universe, temp.getX()-1, temp.getY())))/2;
  111.                 }
  112.                 if (((weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY()))) > 10) && (universe.belongsToMe(correct(universe, temp.getX()+1, temp.getY()))))
  113.                 {
  114.                     Neighbours.add(MovementCommand.Direction.RIGHT);
  115.                     howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY())))/2;
  116.                     weight -= (weight - universe.getPopulation(correct(universe, temp.getX()+1, temp.getY())))/2;
  117.                 }
  118.                 if (((weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1))) > 10) && (universe.belongsToMe(correct(universe, temp.getX(), temp.getY()+1))))
  119.                 {
  120.                     Neighbours.add(MovementCommand.Direction.UP);
  121.                     howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1)))/2;
  122.                     weight -= (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()+1)))/2;
  123.                 }
  124.                 if (((weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1))) > 10) && (universe.belongsToMe(correct(universe, temp.getX(), temp.getY()-1))))
  125.                 {
  126.                     Neighbours.add(MovementCommand.Direction.DOWN);
  127.                     howMuch[Neighbours.size()] += (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1)))/2;
  128.                     weight -= (weight - universe.getPopulation(correct(universe, temp.getX(), temp.getY()-1)))/2;
  129.                 }
  130.             }
  131.             for (int i = 0; i < Neighbours.size(); i ++)
  132.             {
  133.                 move.add(new MovementCommand (temp, Neighbours.get(i), howMuch[i]));
  134.             }
  135.         }
  136.        
  137.     }
  138.    
  139.     void secondStrategy (UniverseView universe, List<MovementCommand> move)
  140.     {
  141.         for (Coordinates a : universe.getMyCells())
  142.         {
  143.             move.add(new MovementCommand(a, MovementCommand.Direction.UP, universe.getPopulation(a)));
  144.         }
  145.     }
  146.    
  147.     @Override public void getNextCommands(UniverseView universeView, List<MovementCommand> movementCommands)
  148.     {
  149.         if (myMass (universeView) / hisMass (universeView) < 1.1)
  150.         {
  151.             firstStrategy (universeView, movementCommands);
  152.         }
  153.         else
  154.         {
  155.             secondStrategy (universeView, movementCommands);
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment