Guest User

Untitled

a guest
Oct 28th, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.35 KB | None | 0 0
  1. /**
  2.  * TODO:
  3.  *   Procházení skrz okraje
  4.  *   Closest i diagonálně
  5. */
  6.  
  7.  
  8. // created using DMD 2.054 64-bit
  9. module MyBot;
  10.  
  11. import ants;
  12. import std.stdio : stdout;
  13. import std.algorithm : remove;
  14. import std.string : indexOf;
  15.  
  16.  
  17. bool notIn(Loc strange_ant, const(Loc)[] ants){
  18.     foreach(ant; ants){
  19.         if (strange_ant == ant)
  20.             return false;
  21.     }
  22.    
  23.     return true;
  24. }
  25.  
  26.  
  27. class MyBot : IBot {
  28.     private Ants engine;
  29.     public Loc[] planed_steps;
  30.     public Direction[Loc] last_direction;
  31.    
  32.     /// vrati nejblizsi prvek z pole
  33.     Loc findClosestDest(Loc ant, const(Loc)[] locations){
  34.         Loc cl;
  35.         foreach(l; locations){
  36.             if (l == Loc()){
  37.                 cl = l;
  38.                 continue;
  39.             }
  40.        
  41.             if (engine.distance(l, ant) < engine.distance(cl, ant))
  42.                 cl = l;
  43.         }
  44.    
  45.         return cl;
  46.     }
  47.    
  48.     /// vrati nejblizsi jidlo ke kteremu ma mravenec cisty pristup
  49.     Loc findClosestClear(Loc ant, const(Loc)[] where){
  50.         Loc[] cf;
  51.         foreach(f; where){
  52.             if (hasClearRoad(ant, f))
  53.                 cf ~= f;
  54.         }
  55.        
  56.         return findClosestDest(ant, cf);
  57.     }
  58.     Loc findClosestEnemyClear(Loc ant, const(Loc)[] where){
  59.         Loc[] cf;
  60.         foreach(f; where){
  61.             if (hasClearRoad(ant, f))
  62.                 cf ~= f;
  63.         }
  64.        
  65.         return findClosestDest(ant, cf);
  66.     }
  67.    
  68.     bool hasClearRoad(Loc ant, Loc dest){
  69.         if (ant == dest)
  70.             return true;
  71.            
  72.         auto d = closestDirection(ant, dest);
  73.         Loc next = engine.destination(ant, d);
  74.        
  75.         return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && hasClearRoad(next, dest));
  76.     }
  77.     bool hasEnemyClearRoad(Loc ant, Loc dest){
  78.         if (ant == dest)
  79.             return true;
  80.            
  81.         auto d = closestDirection(ant, dest);
  82.         Loc next = engine.destination(ant, d);
  83.        
  84.         bool no_enemy_around = true;
  85.         Loc around;
  86.         foreach(delta_row; [-1, 0, 1]){
  87.             around.row = next.row + delta_row;
  88.             foreach(delta_col; [-1, 0, 1]){
  89.                 around.col = next.col + delta_col;
  90.                 no_enemy_around = no_enemy_around && notIn(around, this.engine.enemyAnts());
  91.                 if (!no_enemy_around)
  92.                     break;
  93.             }
  94.             if (!no_enemy_around)
  95.                     break;
  96.         }
  97.        
  98.         return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && no_enemy_around && hasClearRoad(next, dest));
  99.     }
  100.    
  101.     void moveTo(Loc ant, Direction d){
  102.         Loc next = engine.destination(ant, d);
  103.        
  104.         if (!hasClearRoad(ant, next))
  105.             return;
  106.            
  107.         engine.issueOrder(ant, d);
  108.         planed_steps ~= next;
  109.        
  110.         last_direction.remove(ant);
  111.         last_direction[next] = d;
  112.     }
  113.    
  114.     Direction closestDirection(const(Loc) ant, const(Loc) dest){
  115.         struct smer{
  116.             float l;
  117.             Direction d;
  118.         }
  119.        
  120.         smer s;
  121.         foreach(d; engine.direction(ant, dest)){
  122.             auto destination = engine.destination(ant, d);
  123.            
  124.             if (s == smer()){
  125.                 s.l =  engine.distance(ant, destination);
  126.                 s.d = d;
  127.                 continue;
  128.             }
  129.            
  130.             if (s.l > engine.distance(ant, destination)){
  131.                 s.l = engine.distance(ant, destination);
  132.                 s.d = d;
  133.             }
  134.         }
  135.        
  136.         return s.d;
  137.     }
  138.    
  139.     const(Loc)[] hillsToLoc(const(Hill)[] hills){
  140.         const(Loc)[] l_hills;
  141.        
  142.         foreach(h; hills){
  143.             l_hills ~= h.loc;
  144.         }
  145.        
  146.         return l_hills;
  147.     }
  148.    
  149.     void doTurn(Ants engine) {
  150.         this.engine = engine;
  151.         this.planed_steps = [];
  152.        
  153.         foreach(ant; engine.myAnts){
  154.             Loc food  = findClosestClear(ant, engine.food());
  155.             Loc enemy = findClosestClear(ant, engine.enemyAnts());
  156.             Loc enemyHill = findClosestEnemyClear(ant, hillsToLoc(engine.enemyHills));
  157.            
  158.             if (enemyHill != Loc()){
  159.                 this.moveTo(ant, closestDirection(ant, enemyHill));
  160.             }else if (food != Loc()){
  161.                 this.moveTo(ant, closestDirection(ant, food)); // mozna predelat na fci pro nejkratsi cestu?
  162.             }else if (engine.myAnts().length > engine.enemyAnts().length + 3 && engine.myAnts().length > 20 && enemy != Loc()){
  163.                 this.moveTo(ant, closestDirection(ant, enemy)); // mozna predelat na fci pro nejkratsi cestu?
  164.             }else{
  165.                 Direction new_d;
  166.                 if (ant in last_direction)
  167.                     new_d = last_direction[ant];
  168.                 else
  169.                     new_d = AIM[0];
  170.                    
  171.                 Loc antGoto = engine.destination(ant, new_d);
  172.                 int cnt = 0;
  173.                 while(!hasClearRoad(ant, antGoto) && cnt++ <= 4){
  174.                     new_d = AIM["wnes".indexOf(new_d.key)];
  175.                     antGoto = engine.destination(ant, new_d);
  176.                 }
  177.                
  178.                 moveTo(ant, new_d);
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184.  
  185.  
  186. void main() {
  187.     version(unittest) {
  188.         // We don't run the bot or wait for input in this case
  189.     } else {
  190.         Ants.run(new MyBot());
  191.     }
  192. }
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment