Guest User

Untitled

a guest
Feb 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.38 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.    
  59.     bool hasClearRoad(Loc ant, Loc dest){
  60.         if (ant == dest)
  61.             return true;
  62.            
  63.         auto d = closestDirection(ant, dest);
  64.         Loc next = engine.destination(ant, d);
  65.        
  66.         return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && hasClearRoad(next, dest);
  67.     }
  68.    
  69.     void moveTo(Loc ant, Direction d){
  70.         engine.issueOrder(ant, d);
  71.        
  72.         Loc next = engine.destination(ant, d);
  73.         planed_steps ~= next;
  74.        
  75.         last_direction.remove(ant);
  76.         last_direction[next] = d;
  77.     }
  78.    
  79.     Direction closestDirection(const(Loc) ant, const(Loc) dest){
  80.         struct smer{
  81.             float l;
  82.             Direction d;
  83.         }
  84.        
  85.         smer s;
  86.         foreach(d; engine.direction(ant, dest)){
  87.             auto destination = engine.destination(ant, d);
  88.            
  89.             if (s == smer()){
  90.                 s.l =  engine.distance(ant, destination);
  91.                 s.d = d;
  92.                 continue;
  93.             }
  94.            
  95.             if (s.l > engine.distance(ant, destination)){
  96.                 s.l = engine.distance(ant, destination);
  97.                 s.d = d;
  98.             }
  99.         }
  100.        
  101.         return s.d;
  102.     }
  103.    
  104.     const(Loc)[] hillsToLoc(const(Hill)[] hills){
  105.         const(Loc)[] l_hills;
  106.        
  107.         foreach(h; hills){
  108.             l_hills ~= h.loc;
  109.         }
  110.        
  111.         return l_hills;
  112.     }
  113.    
  114.     void doTurn(Ants engine) {
  115.         this.engine = engine;
  116.         this.planed_steps = [];
  117.        
  118.         foreach(ant; engine.myAnts){
  119.             Loc food  = findClosestClear(ant, engine.food());
  120.             Loc enemy = findClosestClear(ant, engine.enemyAnts());
  121.             if (engine.myAnts().length > engine.enemyAnts().length + 20 && enemy != Loc()){
  122.                 if (engine.enemyAnts().length < 20 && hillsToLoc(engine.enemyHills).length > 0)
  123.                     enemy = findClosestClear(ant, hillsToLoc(engine.enemyHills));
  124.                 this.moveTo(ant, closestDirection(ant, enemy)); // mozna predelat na fci pro nejkratsi cestu?
  125.             }else if (food != Loc()){
  126.                 this.moveTo(ant, closestDirection(ant, food)); // mozna predelat na fci pro nejkratsi cestu?
  127.             }else{
  128.                 Direction new_d;
  129.                 if (ant in last_direction)
  130.                     new_d = last_direction[ant];
  131.                 else
  132.                     new_d = AIM[0];
  133.                    
  134.                 Loc antGoto = engine.destination(ant, new_d);
  135.                 int cnt = 0;
  136.                 while(!hasClearRoad(ant, antGoto) && cnt++ <= 4){
  137.                     new_d = AIM["wnes".indexOf(new_d.key)];
  138.                     antGoto = engine.destination(ant, new_d);
  139.                 }
  140.                
  141.                 moveTo(ant, new_d);
  142.             }
  143.         }
  144.     }
  145. }
  146.  
  147.  
  148.  
  149. void main() {
  150.     version(unittest) {
  151.         // We don't run the bot or wait for input in this case
  152.     } else {
  153.         Ants.run(new MyBot());
  154.     }
  155. }
Add Comment
Please, Sign In to add comment