共享一下自己的AI代码(其实是有BUG的。。。
var lastPosition = null var fmap = null var result = null var flag = 1 var fgo = 0 var 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, enemy, game) { /*要是上次的寻路得的指令是前进但位置没变 往前开一炮*/ if(lastPosition !== null) { if(lastPosition[0] == me.tank.position[0] && lastPosition[1] == me.tank.position[1] && fgo == 1){ fgo = 0 me.fire() print(‘蒙一发看看?‘) return } } /*进入常规开火判断*/ var ffire = shouldfire(me,enemy,game.map) if(ffire !== false){ if(ffire == "fire"){ if(me.bullet == null){ me.fire() print(‘来一发!‘) } return }else{ turnto(me, ffire) return } } /*一个很简单的炮弹回避。。。躲开 往前走两格 有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.tank.position.slice() print("我闪!") 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("我闪!") lastPosition = me.tank.position.slice() return; } } /*初始化标志地图 用于记录寻路禁区还有寻路用*/ init(me, game.map, enemy, game) //print(enemy.tank) /*如果没有星星又看不见对面 就将寻路目标定为草丛*/ if(game.star === null && enemy.tank === null){ if (lastPosition !== null && lastPosition[0] === me.tank.position[0] && lastPosition[1] === me.tank.position[1]) { //var turn = [‘left‘, ‘right‘][new Date().getTime() % 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] == forest0[0] && me.tank.position[1] == forest0[1]){ flag = 0 } if(me.tank.position[0] == forest1[0] && me.tank.position[1] == forest1[1]){ flag = 1 } //game.star = [1,12] } /*要是能看见对面 看不见星星就躲着对面走*/ 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 } } /*寻路*/ bfs(game.map, game.star) getresult() if(result.length < 2)return /*恢复寻路破坏的禁区标记*/ init(me, game.map, enemy, game) /*转弯还是前进*/ if(result[result.length - 2][2] == me.tank.direction){ if(fmap[result[result.length - 2][0]][result[result.length - 2][1]] !== ‘x‘){ //不能往禁区前进 fgo = 1 me.go(); print(‘前进!!!‘) } else return }else{ turnto(me, result[result.length - 2][2]) } lastPosition = me.tank.position.slice() //while(1){} // me.go() //print(‘bug?‘) } /*初始化禁区标记 禁区:炮弹前 敌方四周*/ 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 < 400 ;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 < 400 ;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 < 400 ;i++){ 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 < 400 ;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.position[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[handle.x][handle.y-1] !== ‘x‘){ last.next = { pre : handle, dir : "up", x : handle.x, y : handle.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 } last = 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 = handle.pre } } /*将转弯的相对方向封装成绝对方向*/ 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("left") } 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") } } } /*常规开火判断 正对侧对就转过去打 背对就闪*/ 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 && 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.position[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,‘left‘); }else{ return "left" } }else if((enemy.tank.position[0] - me.tank.position[0]) > 0 && canfire(map, 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 } /*要是开火路径上有墙就不打了*/ 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‘)return 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 }
时间: 2024-10-09 18:08:00