• 我昨天晚上改代码的时候很瞌睡,最重要的防止被阴的部分没写完就睡着了,因为我的坦克会自动寻路追着人打,所以很容易被对面躲起来阴,就因为这个吃大亏了。

    不过无所谓 重在参与了,大家可以改进我的代码,加上防止蹲点肯定很厉害,哈哈。

    var aStarGrid = null;
    var sleepTime = 0;
    var lastPosition = null;
    var lastEnemyPosition = null;
    var isSelfStop = false;
    var enemy_block_count=0;
    var map;
    function onIdle(me, enemy, game) {
      if(!aStarGrid){
        createGridMap(game.map);
        map = game.map;
        print('你竟然遇到了我,不好意思你输了!');
      }
    
      if(enemy.tank){
    
        if (lastEnemyPosition !== null &&
          lastEnemyPosition[0] === enemy.tank.position[0] &&
          lastEnemyPosition[1] === enemy.tank.position[1]){
          if(enemy_block_count==1){
            if (!me.bullet)
            {
              me.fire();
              me.fire();
              print('想阴我?你还太年轻');
              enemy_block_count=0;
            }
          }
          enemy_block_count++;
        }else{
          lastEnemyPosition = enemy.tank.position;
          enemy_block_count=0;
        }
      }
      //var dc = null;
      // if(enemy.tank){
      //   //将敌方面前一格设置为墙,避免撞脸
      //   if(enemy.tank.direction =="up"){
      //     dc = "up";
      //     setWall(enemy.tank.position[0],enemy.tank.position[1] - 1);
      //   }else if(enemy.tank.direction =="down"){
      //     dc = "down";
      //     setWall(enemy.tank.position[0],enemy.tank.position[1] + 1);
      //   }else if(enemy.tank.direction =="left"){
      //     setWall(enemy.tank.position[0] - 1,enemy.tank.position[1]);
      //     dc = "left";
      //   }else{
      //     setWall(enemy.tank.position[0] + 1,enemy.tank.position[1]);
      //     dc = "right";
      //   }
      // }
      if (lastPosition !== null &&
          lastPosition[0] === me.tank.position[0] &&
          lastPosition[1] === me.tank.position[1]) 
      {
        if(!isSelfStop)
        {
          if (!me.bullet)
          {
            //me.fire();
            print('挡我者,射');
            isSelfStop == true;
            //return;
          }
        }
      } 
      else 
      {
        lastPosition = me.tank.position;
      }
      if(sleepTime > 0){
        print("休息一会");
        if(!enemy.bullet){
          sleepTime = sleepTime - 2;
        }
        isSelfStop == true;
      }else if(checkBulletdanger(me, enemy) == 2){
        print("检测到子弹,开始躲避。");
        awayBullet(me,enemy,game);
        isSelfStop == true;
      }else if((checkEnemyShootable(me,enemy) === 1) && (!me.bullet)){
        me.fire();
        isSelfStop == true;
      }else if(checkBulletdanger(me, enemy) == 1){
        print("小样,还想阴我!");
        me.fire();
        isSelfStop == true;
      }else if(checkEnemywillShoot(me, enemy) == 2){
        print("菊花危险!还是离开吧!");
        isSelfStop == true;
        awayEnemy(me,enemy,game);
      }else if(game.star){
        var d = findWay(me.tank.position[0],me.tank.position[1],game.star[0],game.star[1]);
        if(d == "go"){
          me.go();
          isSelfStop == false;
        }else if(me.tank.direction == d){
          me.go();
          isSelfStop == false;
        }else if(me.tank.direction == "up"){
          isSelfStop == true;
          if(d == "down"){
            me.turn("left");
          }else{
            me.turn(d);
          }
        }else if(me.tank.direction == "down"){
          isSelfStop == true;
          if(d == "right"){
            me.turn("left");
          }else if(d == "up"){
            me.turn("left");
          }else if(d == "left"){
            me.turn("right");
          }
        }else if(me.tank.direction == "left"){
          isSelfStop == true;
          if(d == "right"){
            me.turn("left");
          }else if(d == "up"){
            me.turn("right");
          }else if(d == "down"){
            me.turn("left");
          }
        }else if(me.tank.direction == "right"){
          isSelfStop == true;
          if(d == "left"){
            me.turn("right");
          }else if(d == "up"){
            me.turn("left");
          }else if(d == "down"){
            me.turn("right");
          }
        }
      //}else if(me.stars > enemy.stars){
        // me.turn("left");
        // me.fire();
      }else if(enemy.tank != null){
        var d = findWay(me.tank.position[0],me.tank.position[1],enemy.tank.position[0],enemy.tank.position[1]);
        if(d == "go"){
          me.go();
          isSelfStop == false;
        }else if(me.tank.direction == d){
          me.go();
          isSelfStop == false;
        }else if(me.tank.direction == "up"){
          isSelfStop == true;
          if(d == "down"){
            me.turn("left");
          }else{
            me.turn(d);
          }
        }else if(me.tank.direction == "down"){
          isSelfStop == true;
          if(d == "right"){
            me.turn("left");
          }else if(d == "up"){
            me.turn("left");
          }else if(d == "left"){
            me.turn("right");
          }
        }else if(me.tank.direction == "left"){
          isSelfStop == true;
          if(d == "right"){
            me.turn("left");
          }else if(d == "up"){
            me.turn("right");
          }else if(d == "down"){
            me.turn("left");
          }
        }else if(me.tank.direction == "right"){
          isSelfStop == true;
          if(d == "left"){
            me.turn("right");
          }else if(d == "up"){
            me.turn("left");
          }else if(d == "down"){
            me.turn("right");
          }
        }
      }else{
        if(!enemy.tank){
          print("no tank");
          me.turn("left");
          me.fire();
          return 0;
        }else{
          if(!me.bullet){
            print("fucking hide");
            isSelfStop == true;
            me.fire();
          }else{
            print("bullet exits");
            return 0;
          }
        }
      }
    
      // if(enemy.tank){
      //   //清除刚刚设置的墙
      //   if(dc =="up"){
      //      setWall(enemy.tank.position[0],enemy.tank.position[1] - 1);
      //   }else if(dc =="down"){
      //     setWall(enemy.tank.position[0],enemy.tank.position[1] + 1);
      //   }else if(dc =="left"){
      //     setWall(enemy.tank.position[0] - 1,enemy.tank.position[1]);
      //   }else{
      //     setWall(enemy.tank.position[0] + 1,enemy.tank.position[1]);
      //   }
      // }
    }
    
    function isBlock(me, direction ,game){
      var x = me.tank.position[0];
      var y = me.tank.position[1];
      if(direction == "up"){
        if(game.map[x][y - 1] == "x"){
          return true;
        }else{
          return false;
        }
      }else if(direction == "down"){
        if(game.map[x][y + 1] == "x"){
          return true;
        }else{
          return false;
        }
      }else if(direction == "left"){
        if(game.map[x - 1][y] == "x"){
          return true;
        }else{
          return false;
        }
      }else{
        if(game.map[x + 1][y] == "x"){
          return true;
        }else{
          return false;
        }
      }
    }
    
    function checkSneakAttack(){
    
    }
    function awayBullet(me,enemy,game){
      if(enemy.bullet.direction == "up" || enemy.bullet.direction == "down"){
        if(me.tank.direction == "left"){
          if(isBlock(me,"left",game)){
            print('这!怎么可能!我不会输的!');
            me.turn("right");
            me.turn("right");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "right"){
          if(isBlock(me,"right",game)){
            print('这!怎么可能!我不会输的!');
            me.turn("left");
            me.turn("left");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "up" || me.tank.direction == "down"){
          if(isBlock(me,"right",game)){
            me.turn("left");
            me.go();
          }else{
            me.turn("right");
            me.go();
            isSelfStop == false;
          }
        }
      }else{
        if(me.tank.direction == "up"){
          if(isBlock(me,"up",game)){
            print('这!怎么可能!我不会输的!');
            me.turn("right");
            me.turn("right");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "down"){
          if(isBlock(me,"down",game)){
            print('这!怎么可能!我不会输的!');
            me.turn("left");
            me.turn("left");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "left" || me.tank.direction == "right"){
          if(isBlock(me,"right",game)){
            me.turn("left");
            me.go();
          }else{
            me.turn("right");
            me.go();
          }
        }
      }
    }
    
    function awayEnemy(me,enemy,game){
      if(enemy.tank.direction == "up" || enemy.tank.direction == "down"){
        if(me.tank.direction == "left"){
          if(isBlock(me,"left",game)){
            me.turn("right");
            me.turn("right");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "right"){
          if(isBlock(me,"right",game)){
            me.turn("left");
            me.turn("left");
            me.go();
          }else{
            me.go();
          }
        }else if(me.tank.direction == "up" || me.tank.direction == "down"){
          if(isBlock(me,"right",game)){
            me.turn("left");
            me.go();
          }else{
            me.turn("right");
            me.go();
          }
        }
      }else{
        if(me.tank.direction == "up"){
          if(isBlock(me,"up",game)){
            me.turn("right");
            me.turn("right");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "down"){
          if(isBlock(me,"down",game)){
            me.turn("left");
            me.turn("left");
            me.go();
          }else{
            me.go();
            isSelfStop == false;
          }
        }else if(me.tank.direction == "left" || me.tank.direction == "right"){
          if(isBlock(me,"right",game)){
            me.turn("left");
            me.go();
          }else{
            me.turn("right");
            me.go();
          }
        }
      }
    }
    
    function checkEnemyShootable(me,enemy){
      if(!enemy.tank){
        return 0;
      }
       if(enemy.tank.position[0] == me.tank.position[0] || enemy.tank.position[0] == me.tank.position[0]-1 || enemy.tank.position[0] == me.tank.position[0]+1){ //判断是否同一条竖线
        //判断两方先后
        var p = enemy.tank.position[1] - me.tank.position[1];
        if(p > 0){ //敌方在下边
    
          if(checkShootisWalled(me,enemy,"down") == 1){
    
            print("路径阻挡,不能射");
            return 0;
          }
    
          if(me.tank.direction == "down"){ 
            print("射他一脸!");
            return 1;
          }else if(me.tank.direction == "left" && enemy.tank.position[0] == me.tank.position[0]){
            if(enemy.tank.direction == "up"){
              me.go();
              isSelfStop == false;
              return 0;
            }else{
              print("转过来射他一脸!");
              me.turn("left");
              return 1;
            }
          }else if(me.tank.direction == "right" && enemy.tank.position[0] == me.tank.position[0]){
            if(enemy.tank.direction == "up"){
              me.go();
              isSelfStop == false;
              return 0;
            }else{
              print("转过来射他一脸!");
              me.turn("right");
              return 1;
            }
          }else{
            return 0;
          }
        }else{ //敌方在上边
          if(checkShootisWalled(me,enemy,"up") == 1){
            print("路径阻挡,不能射");
            return 0;
          }
          if(me.tank.direction == "up"){
            print("射他一脸!");
            return 1;
          }else if(me.tank.direction == "left" && enemy.tank.position[0] == me.tank.position[0]){
            if(enemy.tank.direction == "down"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过来射他一脸!");
              me.turn("right");
              return 1;
            }
          }else if(me.tank.direction == "right" && enemy.tank.position[0] == me.tank.position[0]){
            if(enemy.tank.direction == "down"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过来射他一脸!");
              me.turn("left");
              return 1;
            }
          }else{
            return 0;
          }
        }
      }else if(enemy.tank.position[1] == me.tank.position[1] || enemy.tank.position[1] == me.tank.position[1]-1 || enemy.tank.position[1] == me.tank.position[1]+1){//判断是否同一条横线
        var p = enemy.tank.position[0] - me.tank.position[0];
        if(p > 0){ //敌方在右边
          if(checkShootisWalled(me,enemy,"right") == 1){
            print("路径阻挡,不能射");
            return 0;
          }
          if(me.tank.direction == "right"){
            print("射他一脸!");
            return 1;
          }else if(me.tank.direction == "up" && enemy.tank.position[1] == me.tank.position[1]){
            if(enemy.tank.direction == "left"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过来右边来射他一脸!");
              me.turn("right");
              return 1;
            }
          }else if(me.tank.direction == "down" && enemy.tank.position[1] == me.tank.position[1]){
            if(enemy.tank.direction == "left"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过左边来射他一脸!");
              me.turn("left");
              return 1;
            }
          }else{
            return 0;
          }
        }else{ //敌方在左边
          if(checkShootisWalled(me,enemy,"left") == 1){
            print("路径阻挡,不能射");
            return 0;
          }
          if(me.tank.direction == "left"){
            print("射他一脸!");
            return 1;
          }else if(me.tank.direction == "up" && enemy.tank.position[1] == me.tank.position[1]){
            if(enemy.tank.direction == "right"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过左边来射他一脸!");
              me.turn("left");
              return 1;
            }
          }else if(me.tank.direction == "down" && enemy.tank.position[1] == me.tank.position[1]){
            if(enemy.tank.direction == "right"){
                me.go();
                isSelfStop == false;
                return 0;
            }else{
              print("转过右边来射他一脸!");
              me.turn("right");
              return 1;
            }
          }else{
            return 0;
          }
        }
      }else{
        return 0;
      }
    }
    function checkEnemywillShoot(me, enemy){
      if(!enemy.tank){
        print("敌人不可见");
        return 0;
      }
      if(enemy.tank.position[0] == me.tank.position[0] || enemy.tank.position[0] == me.tank.position[0]-1 || enemy.tank.position[0] == me.tank.position[0]+1){ //判断是否同一条竖线
        //判断两方先后
        var p = enemy.tank.position[1] - me.tank.position[1];
        if(p > 0){ //敌方在下边
            if(enemy.tank.direction == "up"){
              return 2;
            }else{
              return 0;
            }
        }else{ //敌方在上边
          if(enemy.tank.direction == "down"){
            print("敌人在上面且当前面向下,赶紧跑吧!");
              return 2;
            }else{
              return 0;
            }
        }
      }else if(enemy.tank.position[1] == me.tank.position[1] || enemy.tank.position[1] == me.tank.position[1]-1 || enemy.tank.position[1] == me.tank.position[1]+1){//判断是否同一条竖线
        var p = enemy.tank.position[0] - me.tank.position[0];
        if(p > 0){ //敌方在右边
            if(enemy.tank.direction == "left"){
              return 2;
            }else{
              return 0;
            }
        }else{ //敌方在左边
          if(enemy.tank.direction == "right"){
              return 2;
            }else{
              return 0;
            }
        }
      }else{
        return 0;
      }
    }
    
    
    function checkBulletdanger(me, enemy){
      if(!enemy.bullet){
        return 0; //子弹不可见
      }
      if(enemy.bullet.position[0] == me.tank.position[0] || enemy.bullet.position[0] == me.tank.position[0]-1 || enemy.bullet.position[0] == me.tank.position[0]+1 ){ //判断是否同一条竖线
        //判断两方先后
        var p = enemy.bullet.position[1] - me.tank.position[1];
        if(p > 0){ //子弹在下边
            if(enemy.bullet.direction == "up"){
              sleep(p);
              return 2;
            }else{
              sleep(p);
              return 1;
            }
        }else{ //子弹在上边
          if(enemy.bullet.direction == "down"){
              sleep(p);
              return 2;
            }else{
              sleep(p);
              return 1;
            }
        }
      }else if(enemy.bullet.position[1] == me.tank.position[1] || enemy.bullet.position[1] == me.tank.position[1]-1 || enemy.bullet.position[1] == me.tank.position[1]+1 ){//判断是否同一条横线
        var p = enemy.bullet.position[0] - me.tank.position[0];
        if(p > 0){ //子弹在右边
            if(enemy.bullet.direction == "left"){
              sleep(p);
              return 2;
            }else{
              sleep(p);
              return 1;
            }
        }else{ //子弹在左边
          if(enemy.bullet.direction == "right"){
              sleep(p);
              return 2;
            }else{
              sleep(p);
              return 1;
            }
        }
      }else{
        return 0;
      }
    }
    
    function sleep(p){
      sleepTime = Math.ceil(Math.abs(p) / 2);
    }
    function setWall(x,y){
      aStarGrid.nodes[y][x].type = 0;
    }
    function clearWall(x,y){
      aStarGrid.nodes[y][x].type = 1;
    }
    function findWay(startx,starty,endx,endy){
      var start = aStarGrid.nodes[starty][startx];
      var end = aStarGrid.nodes[endy][endx];
      var way = astar.search(aStarGrid.nodes,start,end);
      if(!way[0]){
        return "go";
      }
      if(way[0].x - starty == 1){
        return "down";
      }else if(way[0].x - starty == -1){
        return "up";
      }else if(way[0].y - startx == 1){
        return "right";
      }else{
        return "left";
      }
    }
    
    function checkShootisWalled(me,enemy,t){
      var EX = enemy.tank.position[0],
          EY = enemy.tank.position[1],
          MX = me.tank.position[0],
          MY = me.tank.position[1];
    
      if(t == "up"){
        for(var i = EY;i < MY;i++){
          if(map[MX][i] == "x"){
            return 1;
          }
        }
      }else if(t == "down"){
        for(var i = MY;i < EY;i++){
          if(map[MX][i] == "x"){
            return 1;
          }
        }
      }else if(t == "left"){
        for(var i = EX;i < MX;i++){
          if(map[i][MY] == "x"){
            return 1;
          }
        }
      }else if(t == "right"){
        for(var i = MX;i < EX;i++){
          if(map[i][MY] == "x"){
            return 1;
          }
        }
      }
    
      return 0;
    }
    function createGridMap(map){
      var binmap = [];
      for(var i = 0; i < map[0].length;i++){
        binmap[i] = [];
        for(var v = 0; v < map.length;v++){
          binmap[i][v] = parseInt(map[v][i].replace(".",1).replace("x",0).replace("o",1));
        }
      }
    
    
      aStarGrid = new Graph(binmap);
      //创建aStar Graph
    }
    
    var astar={init:function(c){for(var a=0,f=c.length;a<f;a++){for(var e=0,b=c[a].length;e<b;e++){var d=c[a][e];d.f=0;d.g=0;d.h=0;d.cost=1;d.visited=false;d.closed=false;d.parent=null}}},heap:function(){return new BinaryHeap(function(a){return a.f})},search:function(a,c,f,e,j){astar.init(a);j=j||astar.manhattan;e=!!e;var l=astar.heap();l.push(c);while(l.size()>0){var d=l.pop();if(d===f){var p=d;var h=[];while(p.parent){h.push(p);p=p.parent}return h.reverse()}d.closed=true;var o=astar.neighbors(a,d,e);for(var g=0,m=o.length;g<m;g++){var n=o[g];if(n.closed||n.isWall()){continue}var k=d.g+n.cost;var b=n.visited;if(!b||k<n.g){n.visited=true;n.parent=d;n.h=n.h||j(n.pos,f.pos);n.g=k;n.f=n.g+n.h;if(!b){l.push(n)}else{l.rescoreElement(n)}}}}return[]},manhattan:function(c,a){var d=Math.abs(a.x-c.x);var b=Math.abs(a.y-c.y);return d+b},neighbors:function(d,e,c){var b=[];var a=e.x;var f=e.y;if(d[a-1]&&d[a-1][f]){b.push(d[a-1][f])}if(d[a+1]&&d[a+1][f]){b.push(d[a+1][f])}if(d[a]&&d[a][f-1]){b.push(d[a][f-1])}if(d[a]&&d[a][f+1]){b.push(d[a][f+1])}if(c){if(d[a-1]&&d[a-1][f-1]){b.push(d[a-1][f-1])}if(d[a+1]&&d[a+1][f-1]){b.push(d[a+1][f-1])}if(d[a-1]&&d[a-1][f+1]){b.push(d[a-1][f+1])}if(d[a+1]&&d[a+1][f+1]){b.push(d[a+1][f+1])}}return b}};
    function BinaryHeap(a){this.content=[];this.scoreFunction=a}BinaryHeap.prototype={push:function(a){this.content.push(a);this.sinkDown(this.content.length-1)},pop:function(){var a=this.content[0];var b=this.content.pop();if(this.content.length>0){this.content[0]=b;this.bubbleUp(0)}return a},remove:function(c){var b=this.content.indexOf(c);var a=this.content.pop();if(b!==this.content.length-1){this.content[b]=a;if(this.scoreFunction(a)<this.scoreFunction(c)){this.sinkDown(b)}else{this.bubbleUp(b)}}},size:function(){return this.content.length},rescoreElement:function(a){this.sinkDown(this.content.indexOf(a))},sinkDown:function(d){var a=this.content[d];while(d>0){var c=((d+1)>>1)-1,b=this.content[c];if(this.scoreFunction(a)<this.scoreFunction(b)){this.content[c]=a;this.content[d]=b;d=c}else{break}}},bubbleUp:function(b){var a=this.content.length,f=this.content[b],h=this.scoreFunction(f);while(true){var e=(b+1)<<1,j=e-1;var c=null;var k;if(j<a){var i=this.content[j];k=this.scoreFunction(i);if(k<h){c=j}}if(e<a){var g=this.content[e],d=this.scoreFunction(g);if(d<(c===null?h:k)){c=e}}if(c!==null){this.content[b]=this.content[c];this.content[c]=f;b=c}else{break}}}};function Graph(c){var b=[];for(var a=0;a<c.length;a++){b[a]=[];for(var e=0,d=c[a];e<d.length;e++){b[a][e]=new GraphNode(a,e,d[e])}}this.input=c;this.nodes=b}Graph.prototype.toString=function(){var e="\n";var d=this.nodes;var g,f,h,c;for(var b=0,a=d.length;b<a;b++){g="";f=d[b];for(h=0,c=f.length;h<c;h++){g+=f[h].type+" "}e=e+g+"\n"}return e};function GraphNode(a,c,b){this.data={};this.x=a;this.y=c;this.pos={x:a,y:c};this.type=b}GraphNode.prototype.toString=function(){return"["+this.x+" "+this.y+"]"};GraphNode.prototype.isWall=function(){return this.type===0};