Advertisement
321igor

Untitled

Mar 19th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.70 KB | None | 0 0
  1. package bots;
  2. import java.util.*;
  3.  
  4. import pirates.game.*;
  5.  
  6. public class MyBot implements PirateBot
  7. {
  8.     //Global declarations
  9.     List<Location> occupied = new ArrayList<>();
  10.     public final int maxMoves = 6;
  11.     public int movesLeft;
  12.     public int shahidIndex = -1;
  13.  
  14.     public class Move
  15.     {
  16.         boolean moved = false;
  17.         Pirate pirate;
  18.         Location location;
  19.         int moves;
  20.         PirateGame game;
  21.         //check in the constractor if we can move to this location
  22.         public Move(PirateGame game,Pirate pirate, Location loc, int moves)
  23.         {
  24.             this.game = game;
  25.             this.pirate = pirate;
  26.             this.location = chosePath(loc, moves);
  27.             if (this.location != null) {
  28.                 this.moves = moves;
  29.                 movesLeft -= moves;
  30.                 occupied.add(this.location);
  31.             }
  32.             else
  33.             {
  34.                 moved = true;   //Setting this flag to make to not move the ship
  35.             }
  36.         }
  37.         public Location chosePath(Location finalDestination, int plannedMoves) {
  38.             // get a list of possible locations for the pirate ship after assigning it a number of steps ("moves") towards the destination
  39.             List<Location> possibleLocations = game.getSailOptions(pirate, finalDestination, plannedMoves);
  40.             printLocations(possibleLocations, game);
  41.             prioritizeLocations(possibleLocations, game, pirate);   //Sorting the locations by priority
  42.             printLocations(possibleLocations, game);
  43.             //printOccupied(game);
  44.             for(Location tempLoc : possibleLocations)
  45.             {
  46.                 if(!occupied.contains(tempLoc))
  47.                     return tempLoc;
  48.             }
  49.             return null;
  50.         }
  51.  
  52.         public void run()
  53.         {
  54.             if(moved || moves <= 0)
  55.                 return;
  56.             game.setSail(pirate, location);
  57.             //printOccupied(game);
  58.  
  59.         }
  60.  
  61.     }
  62.  
  63.  
  64.  
  65.     @Override
  66.     public void doTurn(PirateGame game)
  67.     {
  68.  
  69.         movesLeft = maxMoves;
  70.         List<Pirate> pirates = game.mySoberPirates();
  71.         if (pirates.size() == 0)
  72.             return;
  73.  
  74.         List<Pirate> Ships = new ArrayList<>();   //Ships to move this turn
  75.         List<Move> moves = new ArrayList<>();
  76.         Pirate shaid = null;    //Kills himself
  77.  
  78.         //If we want a shahid
  79.         chooseAShahid(pirates, game);
  80.         if (shahidIndex != -1)
  81.             shaid = game.allMyPirates().get(shahidIndex);
  82.  
  83.         //cehck which pirates are moved
  84.         for(int i = 0; i < pirates.size(); i++)
  85.         {
  86.             if (pirates.get(i).getId() == shahidIndex)
  87.                 continue;
  88.             //We fire or move
  89.             if(!fire(pirates.get(i), game)) {
  90.                 Ships.add(pirates.get(i));
  91.             }
  92.         }
  93.  
  94.         Ships = prioritizePirates(Ships);
  95.  
  96.         //avoid ships
  97.         avoidShips(game, pirates);
  98.  
  99.  
  100.         //Move all the ships if there are ships to move
  101.         if (Ships.size() != 0)
  102.             moves.addAll(generateMoves(game,Ships));
  103.  
  104.         //Move the shahid with the remaining turns
  105.         if (shahidIndex != -1) {
  106.             if (!fire(shaid, game)) {
  107.                 moves.add(shahidMove(shaid, game));
  108.             }
  109.         }
  110.  
  111.  
  112.  
  113.  
  114.         for(int i = 0; i < moves.size(); i++)
  115.         {
  116.             moves.get(i).run();
  117.         }
  118.  
  119.         occupied = new ArrayList<>();
  120.     }
  121.  
  122.     public void avoidShips(PirateGame game, List<Pirate> pirates)
  123.     {
  124.         avoidTheDrunk(game, game.myDrunkPirates());   //Avoids drunk ships
  125.  
  126.         //dont move to current position of either of our ships, needs to be updated
  127.         for(int i = 0; i < pirates.size(); i++)
  128.         {
  129.             occupied.add(pirates.get(i).getLocation());
  130.         }
  131.         List<Pirate> myLost = game.myLostPirates();
  132.         for (int i = 0; i < myLost.size(); i++) {
  133.             occupied.add(myLost.get(i).getInitialLocation());
  134.  
  135.         }
  136.         List<Pirate> enemyLost = game.enemyLostPirates();
  137.         for (int i = 0; i < enemyLost.size(); i++) {
  138.             occupied.add(enemyLost.get(i).getInitialLocation());
  139.  
  140.         }
  141.         //printOccupied(game);
  142.  
  143.     }
  144.  
  145.     public void avoidTheDrunk(PirateGame game, List<Pirate> pirates) {
  146.         List<Pirate> enemies = game.enemyDrunkPirates();
  147.  
  148.         for (int i = 0; i < enemies.size(); i++) {
  149.             occupied.add(enemies.get(i).getLocation());
  150.         }
  151.         for (int i = 0; i < pirates.size(); i++) {
  152.             occupied.add(pirates.get(i).getLocation());
  153.  
  154.         }
  155.     }
  156.  
  157.     void chooseAShahid(List<Pirate> pirates, PirateGame game) {
  158.         if(game.treasures().size() == 1)
  159.             return;
  160.         if (pirates.size() > maxMoves || pirates.size() > 1) {
  161.             if (shahidIndex != -1 && !game.myLostPirates().contains(game.allMyPirates().get(shahidIndex)))
  162.                 return;
  163.             shahidIndex = -1;
  164.             int worseIndex = -1;
  165.             for (Pirate temp : pirates) {
  166.                 if (!temp.hasTreasure()) {
  167.                     worseIndex = temp.getId();
  168.                     if (temp.getReloadTurns() == 0) {
  169.                         shahidIndex = temp.getId();
  170.                     }
  171.                 }
  172.             }
  173.             if (shahidIndex == -1)
  174.                 shahidIndex = worseIndex;
  175.         }
  176.         else if (game.enemyPiratesWithoutTreasures().size() != 0)
  177.             shahidIndex = -1;
  178.     }
  179.  
  180.     //fire function
  181.     public boolean fire(Pirate pirate, PirateGame game) {
  182.         //Cant fire if have treasure
  183.         if (pirate.hasTreasure())
  184.             return false;
  185.         //reloading
  186.         if(pirate.getReloadTurns() != 0)
  187.             return false;
  188.         List<Pirate> enemy = game.enemySoberPirates();
  189.         //index of enemy in range
  190.         int canFire = -1;
  191.         //Fires if can. Prefers treasure
  192.         for (int i = 0; i < enemy.size(); i++) {
  193.             Pirate enemyTemp = enemy.get(i);
  194.             if (game.inRange(pirate, enemyTemp)) {
  195.                 //If in range and has treasure fuck this bitch
  196.                 if (enemyTemp.hasTreasure()) {
  197.                     game.attack(pirate, enemyTemp);
  198.                     return true;
  199.                 }
  200.                 else
  201.                     canFire = i;
  202.             }
  203.         }
  204.         if(canFire != -1)
  205.         {
  206.             game.attack(pirate, enemy.get(canFire));
  207.             return true;
  208.         }
  209.         else
  210.             return false;
  211.     }
  212.  
  213.     List<Pirate> prioritizePirates(List<Pirate> ships) {
  214.         List<Pirate> hasTreasure = new ArrayList<>();
  215.         List<Pirate> withoutTreasure = new ArrayList<>();
  216.         for (int i = 0; i < ships.size(); i++) {
  217.             if (ships.get(i).hasTreasure())
  218.                 hasTreasure.add(ships.get(i));
  219.             else
  220.                 withoutTreasure.add(ships.get(i));
  221.         }
  222.         hasTreasure.addAll(withoutTreasure);
  223.         return hasTreasure;
  224.     }
  225.  
  226.     //Sorts the possible routs by priority (0 is safest way, arr.length is most dangerous)
  227.     void prioritizeLocations(List<Location> unsorted, PirateGame game, Pirate pirate) {
  228.         int[] values = new int[unsorted.size()];
  229.         int minDist;    //Minimum distance
  230.         int currentDist;    //Temporary variable
  231.         for (int i = 0; i < unsorted.size(); i++) {
  232.             minDist = Integer.MAX_VALUE;
  233.             for (Pirate enemy : game.enemySoberPirates()) {
  234.                 currentDist = game.distance(enemy, unsorted.get(i));
  235.                 if (currentDist < minDist)
  236.                     minDist = currentDist;
  237.             }
  238.             values[i] = minDist;    //Rating a location by the distance from the closest enemy ship
  239.         }
  240.         quickSort(values, 0, values.length - 1, unsorted);  //Sorts safe first
  241.         boolean treasure = pirate.hasTreasure();
  242.         if (treasure)
  243.             game.debug("We have a treasure, ID: " + pirate.getId());
  244.         boolean booze = pirate.getReloadTurns() <= 0;
  245.         if (booze)
  246.             game.debug("We have booze, ID: " + pirate.getId());
  247.         if (!pirate.hasTreasure() && pirate.getReloadTurns() <= 0) {    //Go dangerous if got no treasure but have booze
  248.             Collections.reverse(unsorted);  //Dangerous road
  249.             game.debug("We went the dangerous road! ID: " + pirate.getId());
  250.         }
  251.         else
  252.             game.debug("We went safe. ID: " + pirate.getId());
  253.     }
  254.  
  255.  
  256.     //A part of quickSort
  257.     int partition(int arr[], int left, int right, List<Location> toSort)
  258.     {
  259.         int i = left, j = right;
  260.         int tmp;
  261.         Location tmpLocation;
  262.         int pivot = arr[(left + right) / 2];
  263.         while (i <= j) {
  264.             while (arr[i] < pivot)
  265.                 i++;
  266.             while (arr[j] > pivot)
  267.                 j--;
  268.             if (i <= j) {
  269.                 tmp = arr[i];
  270.                 tmpLocation = toSort.get(i);
  271.                 arr[i] = arr[j];
  272.                 toSort.add(i, toSort.get(j));
  273.                 toSort.remove(i);
  274.                 arr[j] = tmp;
  275.                 toSort.add(j, tmpLocation);
  276.                 toSort.remove(j);
  277.                 i++;
  278.                 j--;
  279.             }
  280.         }
  281.         return i;
  282.     }
  283.  
  284.     //Sorts the given Location list by the values in arr (arr[i] holds the value for toSort.get(i))
  285.     void quickSort(int arr[], int left, int right, List<Location> toSort) {
  286.         int index = partition(arr, left, right, toSort);
  287.         if (left < index - 1)
  288.             quickSort(arr, left, index - 1, toSort);
  289.         if (index < right)
  290.             quickSort(arr, index, right, toSort);
  291.     }
  292.  
  293.     //shaid moves
  294.     Move shahidMove(Pirate pirate, PirateGame game) {
  295.         List<Pirate> victims = game.enemyPiratesWithTreasures();
  296.         //if no pirates with treasuser
  297.         if(victims.isEmpty())
  298.         {
  299.             Location loc = game.enemySoberPirates().get(0).getInitialLocation();
  300.             return new Move(game, pirate, loc, movesLeft);
  301.         }
  302.         int minDist = Integer.MAX_VALUE;
  303.         int bestVictim = 0;
  304.         //finds best jew to murder
  305.         for (int i = 0; i < victims.size(); i++) {
  306.             int dist = game.distance(victims.get(i).getLocation(), victims.get(i).getInitialLocation());
  307.             if (dist < minDist) {
  308.                 minDist = dist;
  309.                 bestVictim = i;
  310.             }
  311.         }
  312.  
  313.         Location loc = victims.get(bestVictim).getInitialLocation();
  314.         return new Move(game, pirate, loc, movesLeft);
  315.     }
  316.  
  317.  
  318.     boolean treasureCollision(PirateGame game, Pirate pirate, Treasure treasure) {
  319.         if (pirate.getReloadTurns() > 0)
  320.             return false;
  321.         if (game.distance(pirate.getLocation(), treasure.getLocation()) <= maxMoves) {
  322.             List<Pirate> enemies = game.enemySoberPirates();
  323.             for (int i = 0; i < enemies.size(); i++) {
  324.                 if (game.distance(enemies.get(i).getLocation(), treasure.getLocation()) <= maxMoves) {
  325.  
  326.                     return true;
  327.                 }
  328.             }
  329.         }
  330.         return false;
  331.     }
  332.  
  333.     //get the closest pirate treasure
  334.     public List<Move> generateMoves(PirateGame game, List<Pirate> pirates)
  335.     {
  336.         List<Treasure> treasures = game.treasures();
  337.         List<Move> moves = new ArrayList<>();
  338.  
  339.         //no treasures
  340.         if(treasures.isEmpty())
  341.         {
  342.             //find ship with treasure and return to base
  343.             for(int i = 0 ; i <pirates.size(); i++)
  344.             {
  345.                 Pirate pirate = pirates.get(i);
  346.                 if(pirate.hasTreasure())
  347.                 {
  348.                     moves.add( new Move(game, pirate,pirate.getInitialLocation(), 1));
  349.                 }
  350.             }
  351.             return moves;
  352.         }
  353.  
  354.  
  355.  
  356.         //treasures
  357.         int [] ids = new int[2];
  358.         int minPath = Integer.MAX_VALUE;
  359.         int tempPath;
  360.         for(int i = 0; i < pirates.size(); i++)
  361.         {
  362.             Pirate pirate = pirates.get(i);
  363.             //check if ship with treauser
  364.             if(pirate.hasTreasure())
  365.             {
  366.                 moves.add(new Move(game, pirate, pirate.getInitialLocation(), 1));
  367.             }
  368.             else
  369.             {
  370.  
  371.                 //IF NOT THEN FIND PAIR
  372.                 for(int j = 0; j < treasures.size(); j++)
  373.                 {
  374.                     tempPath = game.distance(pirates.get(i), treasures.get(j));
  375.                     if( tempPath < minPath)
  376.                     {
  377.                         minPath = tempPath;
  378.                         ids[0] = i;
  379.                         ids[1] = j;
  380.                     }
  381.                 }
  382.             }
  383.         }
  384.         int tempMove;
  385.  
  386.         //If we gonna collide avoid it, we subtract 2 because the enemy with the treasure collides with us after we shoot at it.
  387.         if (treasureCollision(game, pirates.get(ids[0]), treasures.get(ids[1]))) {
  388.             if (movesLeft > 2)
  389.                 tempMove = movesLeft - 2;
  390.             else
  391.                 tempMove = 0;
  392.         }
  393.         else {
  394.             tempMove = movesLeft;
  395.         }
  396.  
  397.         if(pirates.get(ids[0]).hasTreasure())
  398.             return moves;
  399.         moves.add(new Move(game, pirates.get(ids[0]), treasures.get(ids[1]).getLocation(), tempMove));
  400.         return moves;
  401.     }
  402.  
  403.     //Helper function printing all the occupied positions on this turn
  404.     public void printOccupied(PirateGame game)
  405.     {
  406.         for(Location loc : occupied)
  407.             game.debug(loc.row + ", " + loc.col);
  408.     }
  409.  
  410.     //Helper function to present locations in debug
  411.     void printLocations(List<Location> locations, PirateGame game) {
  412.         for (Location temp : locations) {
  413.             game.debug(temp.row + ", " + temp.col);
  414.         }
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement