Codegame AI Tank War first cup AI code archive

Source: Internet
Author: User

Share your own AI code (in fact, there is a bug ...)

var lastposition = Nullvar Fmap = Nullvar result = Nullvar Flag = 1var Fgo = 0var forest0 = [12,3]var forest1 = [12,3]var Head = {pre:null, dir:null, x:0, y:0, next:null}var handle = Head;var last = Head;function OnIdle (Me, Enem        Y, game) {/* If the last pathfinding instruction is forward but the position is not changed forward a cannon */if (lastposition!== null) {if (lastposition[0] = me.tank.position[0] && lastposition[1] = = Me.tank.position[1] && fgo = 1) {fgo = 0 me.fire () print (' Monte A look? ') return}}/* Enter conventional fire judgment */var Ffire = Shouldfire (Me,enemy,game.map) if (Ffire!== false) {if (Ffire = = ")      Fire ") {if (Me.bullet = = null) {Me.fire () print (' Come on! ') } return}else{Turnto (Me, Ffire) return}}/* A very simple shell avoidance ... Avoid moving forward two-lattice has the bug ...  */if (Enemy.bullet!== null) {if (me.tank.position[0] = = Enemy.bullet.position[0]) {if (me.tank.direction = = ' Left ' || Me.tank.direction = = ' right ') {me.go ()}else{if (game.map[mE.TANK.POSITION[0]+1][ME.TANK.POSITION[1]]!== ' x ') {Turnto (Me, ' right ') Me.go () Turnto (Me, ' Down            ') Me.go (2) if (Me.bullet = = null) me.fire ()}else{Turnto (Me, ' left ') me.go () Turnto (Me, ' Up ') me.go (2) if (Me.bullet = = null) Me.fire ()}} lastposition = Me.tan      K.position.slice () print ("I'm flashing!")    Return } if (me.tank.position[1] = = Enemy.bullet.position[1]) {if (me.tank.direction = = ' Up ' | | me.tank.direction = = ' Down ' {me.go ()}else{if (game.map[me.tank.position[0]][me.tank.position[1]+1]!== ' x ') {Turnto (Me, "Down") Me.go () Turnto (Me, ' left ') me.go (2) if (Me.bullet = = null) Me.fire ()}else {Turnto (Me, ' Up ') Me.go () Turnto (Me, ' right ') me.go (2) if (Me.bullet = = null)      Me.fire ()}} print ("I'm flashing!") Lastposition = Me.tank.position.slice () return; }}/* Initialize the flag map for logging the pathfinding area and pathfinding */init (Me, Game.map, enemy, game)//print (Enemy.tank)/* If there are no stars and can't see the opposite, set the seek target to the grass * * if (g Ame.star = = = NULL && Enemy.tank = = null) {if (lastposition!== null && lastposition[0] = = = Me.tan K.position[0] && lastposition[1] = = = Me.tank.position[1]) {//var turn = [' Left ', ' right '][new Date (). GE Ttime ()% 2]//me.turn (Turn)//me.go ()//if (Me.bullet = null) Me.fire ()}else{if (Me.bullet = = NULL ) Me.fire ()} if (flag = = 1) Game.star = Forest0 if (flag = = 0) Game.star = Forest1 if (me.tank.position[0] = = f Orest0[0] && me.tank.position[1] = = Forest0[1]) {flag = 0} if (me.tank.position[0] = = Forest1[0] && Amp ME.TANK.POSITION[1] = = Forest1[1]) {flag = 1}//game.star = [1,12]}/* If you can see the stars across from the opposite side walk * * * if (Game.star = = = null) {Game.star = [0,0] game.star[0] = enemy.tank.position[0]+1 game.star[1] = 5 if (Game.map[game.star[0]] [GAME.STAR[1]] = = ' x ') {Game.star = Forest1}}/* pathfinding */BFS (Game.map, Game.star) GetResult () if (Result.length & Lt 2) return//To restore the forbidden area tag for pathfinding damage */init (Me, Game.map, enemy, game)/* Turn or forward */if (result[result.length-2][2] = = Me.tank.directi ON) {if (fmap[result[result.length-2][0]][result[result.length-2][1]]!== ' x ') {//cannot go forward to the forbidden area Fgo = 1 Me.go (        );    Print (' Forward!!! ') } else return}else{Turnto (Me, result[result.length-2][2])} lastposition = Me.tank.position.slice ()//while ( 1) {}//Me.go ()//print (' Bug? ')}  /* Initialize the restricted area mark: four weeks before the enemy */function Init (me,map,enemy,game) {head.x = me.tank.position[0];  Head.y = me.tank.position[1];  Head.dir = me.tank.direction;  Head.next = null;  handle = head;  last = head;    Fmap = new Array (map.length) for (i = 0; i < map.length; i++) {Fmap[i] = new Array (map[i].length)//print (FMAP)    for (j = 0; J < Map[i].length; J + +) {Fmap[i][j] = ' a '; }//print (Fmap)} for (i = 0; i < map.length/2;        i++) {for (j = 0; J < Map[i].length/2; J + +) {if (map[i][j] = = ' O ') {forest0[0] = i forest0[1] = j      Break    }} if (J < MAP[I].LENGTH/2) break; Print (FMAP)} for (i = map.length-1, i > map.length/2; i--) {for (j = map[i].length-1; J > map[i].length/ 2;      j--) {if (map[i][j] = = ' O ') {forest1[0] = i forest1[1] = J break;    }} if (J > Map[i].length/2) break;        Print (FMAP)} if (Enemy.bullet!== null) {if (enemy.bullet.direction = = ' up ') {for (i = 0; i <; i++) {        if (Enemy.bullet.position[1]-i < 0) break;      Fmap[enemy.bullet.position[0]][enemy.bullet.position[1]-i] = ' x '} if (enemy.bullet.direction = = ' down ') {        for (i = 0; i <, i++) {if (enemy.bullet.position[1]+i >= map[0].length) break;      Fmap[enemy.bullet.position[0]][enemy.bullet.position[1]+i] = ' x '} if (enemy.bullet.direction = = ' Left ') { for (i = 0; i < 4i++) {if (Enemy.bullet.position[0]-i < 0) break;      FMAP[ENEMY.BULLET.POSITION[0]-I][ENEMY.BULLET.POSITION[1]] = ' x '} if (enemy.bullet.direction = = ' right ') {        for (i = 0; i <, i++) {if (enemy.bullet.position[0]+i >= map.length) break; FMAP[ENEMY.BULLET.POSITION[0]+I][ENEMY.BULLET.POSITION[1]] = ' x '} if (Game.star!=null) if (Fmap[game.star [0]] [Game.star[1]] = = ' x ') Game.star = null} if (Enemy.tank! = null) {fmap[enemy.tank.position[0]+1][enemy.tank.position[1 ]] = ' x ' fmap[enemy.tank.position[0]-1][enemy.tank.position[1]] = ' x ' Fmap[enemy.tank.position[0]][enemy.tank.positi  ON[1]+1] = ' x ' fmap[enemy.tank.position[0]][enemy.tank.position[1]-1] = ' x '}}function BFS (map, star) {//print (star) Count = 0 if (map[star[0]][star[1]] = = ' x ') return if (fmap[star[0]][star[1]] = = ' x ') return//print (Star) while (handle! = null) {//print (count++)//up ex//print (handle.x+ "\ T" +handle.y) if (fmap[handle.x][hANDLE.Y] = = ' x ') {handle = Handle.next continue} if (Map[handle.x][handle.y-1]!== ' x ' && Fmap[hand LE.X][HANDLE.Y-1]!== ' x ') {Last.next = {pre:handle, dir: "Up", x:handle.x, Y:han    Dle.y-1, next:null} last = Last.next; }//down ex if (map[handle.x][handle.y+1]!== ' x ' && fmap[handle.x][handle.y+1]!== ' x ') {Last.next      = {Pre:handle, dir: "Down", x:handle.x, Y:handle.y + 1, next:null}    last = Last.next; }//left ex if (Map[handle.x-1][handle.y]!== ' x ' && fmap[handle.x-1][handle.y]!== ' x ') {Last.next = {Pre:handle, dir: "Left", X:handle.x-1, Y:handle.y, next:null} l    AST = Last.next;  }//right ex if (Map[handle.x+1][handle.y]!== ' x ' && fmap[handle.x+1][handle.y]!== ' x ') {Last.next = {Pre:handle, dir: "RIght ", X:handle.x+1, Y:handle.y, next:null} last = Last.next;    } Fmap[handle.x][handle.y] = ' x ';    if (star[0] = = handle.x && star[1] = = handle.y) break;    handle = Handle.next;  Print (handle)}}function GetResult () {delete result;  result = new Array ()//result.clear (); while (handle!== null) {//print (handle.x+ "\ T" +handle.y) Result.push ([Handle.x,handle.y,handle.dir]) handle = Hand      Le.pre}}/* wraps the opposite direction of the turn into an absolute direction */function Turnto (ME,TRG) {fgo = 0 if (me.tank.direction = = "Up") {if (TRG = = ' down ') {    Me.turn ("left")} if (Trg = = "Right") {Me.turn ("right")} if (TRG = = "Left") {Me.turn ("left")}} if (me.tank.direction = = "Right") {if (TRG = = "Down") {Me.turn ("right")} if (TRG = = "Up") {Me.turn ("le      FT ")} if (TRG = =" Left ") {Me.turn (" left ")}} if (me.tank.direction = =" Down ") {if (TRG = =" Left ") { Me.turn ("right")} if (Trg = = "Right") {me.Turn ("left")} if (TRG = = "Up") {Me.turn ("left")}} if (me.tank.direction = = "Left") {if (TRG = = "Up") {    Me.turn ("right")} if (TRG = = "Down") {Me.turn ("left")} if (Trg = = "Right") {Me.turn ("left")  }}}/* conventional fire judgement is opposite to the opposite side of the turn to the back of the Flash */function shouldfire (Me, Enemy, map) {if (Enemy.tank = null) return false;  if (Me.bullet!== null) return false; if (enemy.tank.position[0] = = = Me.tank.position[0]) {if ((enemy.tank.position[1)-me.tank.position[1]) < 0 &&amp ; Canfire (map, Me, enemy, "up")) {if (me.tank.direction = = ' up ') {return ' fire '}else if (me.tank.direction =            = "Down") {if (map[me.tank.position[0]+1][me.tank.position[1]]!== ' x ') {Turnto (Me, ' right ')}else{      Turnto (Me, ' left ')} me.go () Turnto (Me, ' up ');  }else{return "Up"}}else if ((Enemy.tank.position[1]-me.tank.position[1]) > 0 && canfire (map, Me, enemy, "down")) {if (Me.tank.direction = = "Down") {return ' fire '}else if (me.tank.direction = = "Up") {if (map[me.tank.position[0]+1][me.         TANK.POSITION[1]]!== ' x ') {Turnto (Me, ' right ')}else{Turnto (Me, ' left ')} me.go ()      Turnto (Me, ' down '); }else{return "Down"}} if (enemy.tank.position[1] = = = Me.tank.position[1]) {if (Enemy.tank.positi        ON[0]-me.tank.position[0]) < 0 && Canfire (map, Me, enemy, "left") {if (me.tank.direction = = "Left") { Return "Fire"}else if (me.tank.direction = = "Right") {if (map[me.tank.position[0]][me.tank.position[1]+1]! = = ' x ') {Turnto (Me, ' down ')}else{turnto (Me, ' Up ')} me.go () Turnto (Me, ' lef      T '); }else{return "left"}}else if ((Enemy.tank.position[0]-me.tank.position[0]) > 0 && canfire (ma P, me, Enemy, "right") {if (me.tank.direction = = "Right") {return ' fire '}else if (me.taNk.direction = = "Left") {if (map[me.tank.position[0]][me.tank.position[1]+1]!== ' x ') {Turnto (Me, ' down ')      }else{Turnto (Me, ' Up ')} me.go () Turnto (Me, ' right '); }else{return "Right"}}} return false}/* if there is a wall on the fire path, it will not hit */function Canfire (Map,me,enemy,dir) {if (dir = = "Up") {for (i = me.tank.position[1]; i > enemy.tank.position[1]; i--) {if (map[me.tank.position[0]][i] = = ' x ') ret    Urn false; } return True} if (dir = = ' down ') {for (i = me.tank.position[1]; i < enemy.tank.position[1]; i++) {if (map [Me.tank.position[0]]      [i] = = ' x ') return false;  Print (me.tank.position[0]+ "\ t" +i+ "\ T" +map[me.tank.position[0]][i])//print (map)} return True} if (dir = = "Left") {for (i = me.tank.position[0]; i > Enemy.tank.position[0]; i--) {if (map[i][me.tank.position[1]] = = ' x ')    return false; } return True} if (dir = = "Right") {for (i = me.tank.position[0]; I < Enemy.tanK.POSITION[0];    i++) {if (map[i][me.tank.position[1]] = = ' x ') return false; } return True} return false}

Codegame AI Tank Wars first cup AI code archive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.