Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.37 KB | None | 0 0
  1. package net.codestrikes.adapter;
  2. import net.codestrikes.sdk.Area;
  3. import net.codestrikes.sdk.BotBase;
  4. import net.codestrikes.sdk.Move;
  5. import net.codestrikes.sdk.MoveCollection;
  6. import net.codestrikes.sdk.ReadonlyMoveCollection;
  7. import net.codestrikes.sdk.RoundContext;
  8. import java.util.Arrays;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Random;
  12.  
  13.  
  14. public class PlayerBot extends BotBase {
  15. private static Map<Area, Integer> attackBlockedByEnemyCount;
  16.  
  17.     static {
  18.         attackBlockedByEnemyCount = new HashMap<>();
  19.         attackBlockedByEnemyCount.put(Area.HookKick, 0);
  20.         attackBlockedByEnemyCount.put(Area.UppercutPunch, 0);
  21.         attackBlockedByEnemyCount.put(Area.LowKick, 0);
  22.         attackBlockedByEnemyCount.put(Area.HookPunch, 0);
  23.     }
  24.  
  25.     private static Map<Area, Integer> attacksReceivedCount;
  26.  
  27.     static {
  28.         attacksReceivedCount = new HashMap<>();
  29.         attacksReceivedCount.put(Area.HookKick, 0);
  30.         attacksReceivedCount.put(Area.UppercutPunch, 0);
  31.         attacksReceivedCount.put(Area.LowKick, 0);
  32.         attacksReceivedCount.put(Area.HookPunch, 0);
  33.     }
  34.  
  35.     private static int enemyHp = 200;
  36.     private static int playerHp = 200;
  37.  
  38.     private static int turnCount = 1;
  39.     private static boolean aggressiveStance = true;
  40.  
  41.     private static MoveCollection myLastMoves;
  42.  
  43.     public MoveCollection nextMove(RoundContext context) {
  44.  
  45.         if (turnCount == 1 || (context.getLastOpponentMoves() == null && playerHp != 200)) {
  46.             if (context.getLastOpponentMoves() == null && playerHp != 200) {
  47.                 enemyHp = 0;
  48.                 playerHp = 0;
  49.                 context.setMoves(chooseNextMoves());
  50.                 updateStatistics(context);
  51.                 checkStats(context);
  52.  
  53.             } else {
  54.  
  55.                 context.setMoves(chooseNextMoves());
  56.                 updateStatistics(context);
  57.                 checkStats(context);
  58.             }
  59.         } else {
  60.             updateStatistics(context);
  61.             checkStats(context);
  62.             context.setMoves(chooseNextMoves());
  63.         }
  64.  
  65.         if (playerHp < 50) aggressiveStance = false;
  66.         turnCount++;
  67.  
  68.         myLastMoves = context.getMyMoves();
  69.         return context.getMyMoves();
  70.  
  71.     }
  72.  
  73.     private Area generateRandomArea() {
  74.         if (aggressiveStance) {
  75.             int random = new Random().nextInt(3);
  76.             Area[] areas = {Area.HookPunch, Area.UppercutPunch, Area.HookKick};
  77.             return areas[random];
  78.         }
  79.         else{
  80.             int random = new Random().nextInt(2);
  81.             Area[] areas = {Area.HookPunch, Area.UppercutPunch};
  82.             return areas[random];
  83.         }
  84.     }
  85.  
  86.     private MoveCollection chooseNextMoves() {
  87.         MoveCollection moves;
  88.  
  89.         if (!aggressiveStance) {
  90.             if (turnCount % 2 == 1) {
  91.                 moves = getAttacksCombo(generateRandomArea())
  92.                         .addDefence(generateRandomArea())
  93.                         .addDefence(Area.HookKick);
  94.             } else {
  95.                 moves = calculateNextAttacks()
  96.                         .addDefence(calculateNextDefence())
  97.                         .addDefence(Area.HookKick);
  98.             }
  99.         } else {
  100.             if (turnCount % 2 == 1) {
  101.                 moves = getDoubleAttacksCombo(generateRandomArea())
  102.                         .addDefence(calculateNextDefence());
  103.             } else {
  104.                 moves = calculateNextAttacks()
  105.                         .addDefence(calculateNextDefence());
  106.             }
  107.         }
  108.         return moves;
  109.     }
  110.  
  111.  
  112.     private void updateStatistics(RoundContext context) {
  113.  
  114.         if (context.getLastOpponentMoves() != null && myLastMoves != null) {
  115.  
  116.             for (Move move : myLastMoves.getAttacks()) {
  117.                 if (Arrays.stream(context.getLastOpponentMoves().getDefences()).anyMatch(x -> x.getArea().equals(move.getArea()))) {
  118.                     Integer lastValue = attackBlockedByEnemyCount.get(move.getArea());
  119.                     attackBlockedByEnemyCount.replace(move.getArea(), lastValue, lastValue + 1);
  120.                 }
  121.             }
  122.  
  123.             for (Move move : context.getLastOpponentMoves().getAttacks()) {
  124.                 if (Arrays.stream(myLastMoves.getDefences()).noneMatch(x -> x.getArea().equals(move.getArea()))) {
  125.                     Integer lastValue = attacksReceivedCount.get(move.getArea());
  126.                     attacksReceivedCount.replace(move.getArea(), lastValue, lastValue + 1);
  127.                 }
  128.             }
  129.  
  130.         }
  131.     }
  132.  
  133.     private void checkStats(RoundContext context) {
  134.         enemyHp = enemyHp - context.getMyDamage();
  135.         playerHp = playerHp - context.getOpponentDamage();
  136.  
  137.         if (enemyHp <= 0 || playerHp <= 0) {
  138.             for (Map.Entry<Area, Integer> entry : attackBlockedByEnemyCount.entrySet()) {
  139.                 entry.setValue(0);
  140.             }
  141.  
  142.             for (Map.Entry<Area, Integer> entry : attacksReceivedCount.entrySet()) {
  143.                 entry.setValue(0);
  144.             }
  145.  
  146.             aggressiveStance = true;
  147.             enemyHp = 200;
  148.             playerHp = 200;
  149.             turnCount = 1;
  150.         }
  151.     }
  152.  
  153.     private MoveCollection getAttacksCombo(Area nextArea) {
  154.         MoveCollection moveCollection = new MoveCollection();
  155.         switch (nextArea) {
  156.             case HookPunch:
  157.                 return moveCollection.addAttack(Area.HookPunch).addAttack(Area.LowKick);
  158.             case UppercutPunch:
  159.                 return moveCollection.addAttack(Area.UppercutPunch).addAttack(Area.UppercutPunch);
  160.             default:
  161.                 return moveCollection.addAttack(Area.HookKick);
  162.         }
  163.     }
  164.  
  165.     private MoveCollection getDoubleAttacksCombo(Area nextArea) {
  166.         MoveCollection moveCollection = new MoveCollection();
  167.         switch (nextArea) {
  168.             case LowKick:
  169.                 return moveCollection.addAttack(Area.HookKick).addAttack(Area.LowKick).addAttack(Area.HookPunch);
  170.             case HookPunch:
  171.                 return moveCollection.addAttack(Area.HookPunch).addAttack(Area.LowKick).addAttack(Area.HookPunch).addAttack(Area.LowKick);
  172.             case UppercutPunch:
  173.                 return moveCollection.addAttack(Area.UppercutPunch).addAttack(Area.UppercutPunch).addAttack(Area.UppercutPunch).addAttack(Area.UppercutPunch);
  174.             default:
  175.                 return moveCollection.addAttack(Area.HookKick).addAttack(Area.HookKick);
  176.         }
  177.     }
  178.  
  179.     private MoveCollection calculateNextAttacks() {
  180.         int min = 200;
  181.         Area nextArea = Area.HookKick;
  182.  
  183.         for (Map.Entry<Area, Integer> entry : attackBlockedByEnemyCount.entrySet()) {
  184.             if (entry.getValue() < min) {
  185.                 min = entry.getValue();
  186.                 nextArea = entry.getKey();
  187.             }
  188.         }
  189.         if (!aggressiveStance) return getAttacksCombo(nextArea);
  190.         return getDoubleAttacksCombo(nextArea);
  191.     }
  192.  
  193.     private Area calculateNextDefence() {
  194.         int max = attacksReceivedCount.get(Area.HookKick);
  195.         Area nextArea = Area.HookKick;
  196.  
  197.         for (Map.Entry<Area, Integer> entry : attacksReceivedCount.entrySet()) {
  198.             if (entry.getValue() > max) {
  199.                 max = entry.getValue();
  200.                 nextArea = entry.getKey();
  201.             }
  202.         }
  203.  
  204.         if (nextArea != Area.LowKick) return nextArea;
  205.         return Area.HookKick;
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement