Advertisement
Guest User

Formatted for you.

a guest
Jul 25th, 2014
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. <!-- language: lang-java -->
  2.     package player;
  3.    
  4.     import java.lang.Math.*;
  5.     import java.util.Set;
  6.     import java.util.HashSet;
  7.     import zombie.*;
  8.     import static zombie.Constants.*;
  9.    
  10.     public class Coward implements Player {
  11.    
  12.         private static final Set<PlayerId> killed = new HashSet<>();
  13.         private static final Set<PlayerId> looted = new HashSet<>();
  14.    
  15.         @Override
  16.         public Action doTurn(PlayerContext context) {
  17.    
  18.             PlayerId[][] field = context.getPlayField();
  19.    
  20.             // Panic and shoot
  21.             if (context.getBullets() > 0) {
  22.                 int distEnemy = VISION_WIDTH;
  23.                 int distZombie = VISION_WIDTH;
  24.                 PlayerId targetEnemy = null;
  25.                 PlayerId targetZombie = null;
  26.                 for (int x = CENTRE_OF_VISION - SHOOT_RANGE; x <= CENTRE_OF_VISION + SHOOT_RANGE; x++) {
  27.                     for (int y = CENTRE_OF_VISION - SHOOT_RANGE; y <= CENTRE_OF_VISION + SHOOT_RANGE; y++) {
  28.                         PlayerId player = field[x][y];
  29.                         if (player != null && !killed.contains(player)) {
  30.                             int dist = getDistance(x, y);
  31.                             if (player.getName().equals("Zombie")) {
  32.                                 if( dist < distZombie ) {
  33.                                     distZombie = dist;
  34.                                     targetZombie = player;
  35.                                 }
  36.                             } else if (isEnemy(player.getName()) && dist <= distEnemy ) {
  37.                                 distEnemy = dist;
  38.                                 targetEnemy = field[x][y];
  39.                             }
  40.                         }
  41.                     }
  42.                 }
  43.    
  44.                 if (targetZombie != null && distZombie <= 3) {
  45.                     killed.add(targetZombie);
  46.                     return new Shoot( targetZombie );
  47.                 } else if (targetEnemy != null && distEnemy <= 5 ) {
  48.                     killed.add(targetEnemy);
  49.                     return new Shoot( targetEnemy );
  50.                 }
  51.             }
  52.    
  53.             // Run away
  54.             int bestScore = -10000000;
  55.             Move bestMove = Move.randomMove();
  56.    
  57.             for( int x = -1; x <= 1; x++ ) {
  58.                 for( int y = -1; y <= 1; y++ ) {
  59.                     PlayerId center = field[CENTRE_OF_VISION+x][CENTRE_OF_VISION+y];
  60.                     if( center != null && !looted.contains(center) && center.getName().equals("DeadBody")) {
  61.                         looted.add(center);
  62.                     }
  63.                     if( center == null ) {
  64.                         int thisScore = 0;
  65.                         for( int xx = CENTRE_OF_VISION+x-VISION_RANGE+1; xx < CENTRE_OF_VISION+x+VISION_RANGE; xx++ ) {
  66.                             for( int yy = CENTRE_OF_VISION+y-VISION_RANGE+1; yy < CENTRE_OF_VISION+y+VISION_RANGE; yy++ ) {
  67.                                 PlayerId player = field[xx][yy];
  68.                                 if( player != null) {
  69.                                     int dist = getDistance(xx-x,yy-y);
  70.                                    
  71.                                     if( player.getName().equals("Coward")) { // Prefer lose groups
  72.                                         if( dist >= 3 && dist <= 6 ) {
  73.                                             thisScore += 32;
  74.                                         } else if( dist > 3 ) {
  75.                                             thisScore += 16;
  76.                                         }
  77.                                     } else if( player.getName().equals("DeadBody")) { // Visit dead bodies on the route
  78.                                         if( !looted.contains(player)) {
  79.                                             thisScore += 32*(VISION_RANGE-dist);
  80.                                         }
  81.                                     } else if( player.getName().equals("Zombie")) { // Avoid zombies
  82.                                         if( dist <= 2 ) {
  83.                                             thisScore -= 10000;
  84.                                         } else if( dist <= 3 ) {
  85.                                             thisScore -= 1000;
  86.                                         } else if( dist <= 4 ) {
  87.                                             thisScore -= 100;
  88.                                         }
  89.                                     } else if( isEnemy(player.getName())) { // Avoid strangers
  90.                                         if( dist == 7 ) {
  91.                                             thisScore -= 100;
  92.                                         } else if( dist <= 6 ) {
  93.                                             thisScore -= 1000;
  94.                                         }
  95.                                     }
  96.                                 }
  97.                             }
  98.                         }
  99.                         if( thisScore > bestScore ) {
  100.                             bestScore = thisScore;
  101.                             bestMove = Move.inDirection( x, y );
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.    
  107.             return bestMove;
  108.         }
  109.    
  110.         private boolean isEnemy(String name) {
  111.             switch (name) {
  112.                 case "Coward":
  113.                 case "DeadBody":
  114.                 case "GordonFreeman":
  115.                 case "EmoWolfWithAGun":
  116.                 case "HuddleWolf":
  117.                 case "ThePriest":
  118.                 case "Shotguneer":
  119.                     return false;
  120.                 default:
  121.                     return true;
  122.             }
  123.         }
  124.    
  125.         private int getDistance(int x, int y) {
  126.             return Math.max(Math.abs(CENTRE_OF_VISION - x), Math.abs(CENTRE_OF_VISION - y));
  127.         }
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement