function findTank(me) {
  var goalHexes = new Set();
  var dangerHexes = new Set();
  for (var i = 0, target; (target = tanks[i]); i++) {
    if (target === me || target.disposed) {
      continue;
    }
    // Enemy tank is a good goal.
    var hex = target.getHex();
    goalHexes.add(hex);
    // In front of an enemy tank is a dangerous place.
    var lookX = hex.hexX;
    var lookY = hex.hexY;
    var dxy = dirToDelta(target.direction);
    while (true) {
      lookX += dxy.x;
      lookY += dxy.y;
      hex = grid[lookY][lookX];
      if (hex.isWall || hex.getTank()) {
        break;
      }
      dangerHexes.add(hex);
    }
  }

  function record(newHex, newDir) {
    var newDesc = makeDesc(newHex, newDir);
    if (!cameFrom.has(newDesc)) {
      frontier.push([newHex, newDir]);
      cameFrom.set(newDesc, [currentHex, currentDir]);
    }
  }

  function makeDesc(hex, direction) {
    return hex.hexX + ',' + hex.hexY + ',' + direction;
  }

  var startHex = me.getHex();
  var startDir = me.direction;
  var frontier = [[startHex, startDir]];
  var cameFrom = new Map();
  while (frontier.length) {
    var current = frontier.shift();
    var currentHex = current[0];
    var currentDir = current[1];
    if (currentHex !== startHex && goalHexes.has(currentHex)) {
      // Found an enemy tank.  Return the first direction to step to.
      var returnDir = null;
      while (currentHex !== startHex || currentDir !== startDir) {
        returnDir = currentDir;
        current = cameFrom.get(makeDesc(currentHex, currentDir));
        currentHex = current[0];
        currentDir = current[1];
      }
      return returnDir;
    }
    var newHex, newDir;
    // Explore moving forwards.
    var dxy = dirToDelta(currentDir);
    newHex = grid[currentHex.hexY + dxy.y][currentHex.hexX + dxy.x];
    newDir = currentDir;
    if (!newHex.isWall && !dangerHexes.has(newHex)) {
      record(newHex, newDir);
    }
    newHex = currentHex;
    // Explore turning right.
    newDir = (currentDir + 1) % 6;
    record(newHex, newDir);
    // Explore turning left.
    newDir = (currentDir + 5) % 6;
    record(newHex, newDir);
  }
  return null;
}
// Return the initial direction to move to head to the tank.
Return the initial direction to move to head to the tank.
Return the initial direction to move to head to the tank .
function findTank(me) {
  var goalHexes = new Set();
  var dangerHexes = new Set();
  for (var i = 0, target; (target = tanks[i]); i++) {
    if (target === me || target.disposed) {
      continue;
    }
    var hex = target.getHex();
    goalHexes.add(hex);
    var lookX = hex.hexX;
    var lookY = hex.hexY;
    var dxy = dirToDelta(target.direction);
    while (true) {
      lookX += dxy.x;
      lookY += dxy.y;
      hex = grid[lookY][lookX];
      if (hex.isWall || hex.getTank()) {
        break;
      }
      dangerHexes.add(hex);
    }
  }
  function record(newHex, newDir) {
    var newDesc = makeDesc(newHex, newDir);
    if (!cameFrom.has(newDesc)) {
      frontier.push([newHex, newDir]);
      cameFrom.set(newDesc, [currentHex, currentDir]);
    }
  }
  function makeDesc(hex, direction) {
    return hex.hexX + ',' + hex.hexY + ',' + direction;
  }
  var startHex = me.getHex();
  var startDir = me.direction;
  var frontier = [[startHex, startDir]];
  var cameFrom = new Map();
  while (frontier.length) {
    var current = frontier.shift();
    var currentHex = current[0];
    var currentDir = current[1];
    if (currentHex !== startHex && goalHexes.has(currentHex)) {
      var returnDir = null;
      while (currentHex !== startHex || currentDir !== startDir) {
        returnDir = currentDir;
        current = cameFrom.get(makeDesc(currentHex, currentDir));
        currentHex = current[0];
        currentDir = current[1];
      }
      return returnDir;
    }
    var newHex, newDir;
    var dxy = dirToDelta(currentDir);
    newHex = grid[currentHex.hexY + dxy.y][currentHex.hexX + dxy.x];
    newDir = currentDir;
    if (!newHex.isWall && !dangerHexes.has(newHex)) {
      record(newHex, newDir);
    }
    newHex = currentHex;
    newDir = (currentDir + 1) % 6;
    record(newHex, newDir);
    newDir = (currentDir + 5) % 6;
    record(newHex, newDir);
  }
  return null;
}
function findTank ( me ) { var goalHexes = new Set ( ) ; var dangerHexes = new Set ( ) ; for ( var i = 0 , target ; ( target = tanks [ i ] ) ; i ++ ) { if ( target === me || target . disposed ) { continue ; } var hex = target . getHex ( ) ; goalHexes . add ( hex ) ; var lookX = hex . hexX ; var lookY = hex . hexY ; var dxy = dirToDelta ( target . direction ) ; while ( true ) { lookX += dxy . x ; lookY += dxy . y ; hex = grid [ lookY ] [ lookX ] ; if ( hex . isWall || hex . getTank ( ) ) { break ; } dangerHexes . add ( hex ) ; } } function record ( newHex , newDir ) { var newDesc = makeDesc ( newHex , newDir ) ; if ( ! cameFrom . has ( newDesc ) ) { frontier . push ( [ newHex , newDir ] ) ; cameFrom . set ( newDesc , [ currentHex , currentDir ] ) ; } } function makeDesc ( hex , direction ) { return hex . hexX + ',' + hex . hexY + ',' + direction ; } var startHex = me . getHex ( ) ; var startDir = me . direction ; var frontier = [ [ startHex , startDir ] ] ; var cameFrom = new Map ( ) ; while ( frontier . length ) { var current = frontier . shift ( ) ; var currentHex = current [ 0 ] ; var currentDir = current [ 1 ] ; if ( currentHex !== startHex && goalHexes . has ( currentHex ) ) { var returnDir = null ; while ( currentHex !== startHex || currentDir !== startDir ) { returnDir = currentDir ; current = cameFrom . get ( makeDesc ( currentHex , currentDir ) ) ; currentHex = current [ 0 ] ; currentDir = current [ 1 ] ; } return returnDir ; } var newHex , newDir ; var dxy = dirToDelta ( currentDir ) ; newHex = grid [ currentHex . hexY + dxy . y ] [ currentHex . hexX + dxy . x ] ; newDir = currentDir ; if ( ! newHex . isWall && ! dangerHexes . has ( newHex ) ) { record ( newHex , newDir ) ; } newHex = currentHex ; newDir = ( currentDir + 1 ) % 6 ; record ( newHex , newDir ) ; newDir = ( currentDir + 5 ) % 6 ; record ( newHex , newDir ) ; } return null ; }
Return the initial direction to move to head to the tank.
Return the initial direction to move to head to the tank .
// Enemy tank is a good goal. // In front of an enemy tank is a dangerous place. // Found an enemy tank.  Return the first direction to step to. // Explore moving forwards. // Explore turning right. // Explore turning left.