CatCoding

Ruby Robot AI

2012-11-22

最近看到一个RRobot,这是一个用 Ruby 来实现的坦克对战平台。感觉挺好玩的,周三在公司也顺带和同事分享了一下。有时间的同学可以尝试尝试,用 Ruby 来写坦克的 AI。另外这个不到 1000 行的程序也比较好读,这种 Robot AI 平台以前也有 C++/Java 版本的,不过都要比这个实现得复杂一点吧。

tank-aitank-ai

每个你控制的 robot 的 api 是这些,注意雷达扫描到的目标只包含距离信息,没有 x 和 y,如果雷达扫描得越快所得到的目标位置准确率越低。自己摸索着写,找一些别人写好的策略来对战一把吧。

 battlefield_height  #the height of the battlefield
 battlefield_width   #the width of the battlefield
 energy              #your remaining energy (if this drops below 0 you are dead)
 gun_heading         #the heading of your gun, 0 pointing east, 90 pointing 
                     #north, 180 pointing west, 270 pointing south
 gun_heat            #your gun heat, if this is above 0 you can't shoot
 heading             #your robots heading, 0 pointing east, 90 pointing north,
                     #180 pointing west, 270 pointing south
 size                #your robots radius, if x <= size you hit the left wall
 radar_heading       #the heading of your radar, 0 pointing east, 
                     #90 pointing north, 180 pointing west, 270 pointing south
 time                #ticks since match start
 speed               #your speed (-8/8)
 x                   #your x coordinate, 0...battlefield_width
 y                   #your y coordinate, 0...battlefield_height
 accelerate(param)   #accelerate (max speed is 8, max accelerate is 1/-1, 
                     #negativ speed means moving backwards)
 stop                #accelerates negativ if moving forward (and vice versa), 
                     #may take 8 ticks to stop (and you have to call it every tick)
 fire(power)         #fires a bullet in the direction of your gun, 
                     #power is 0.1 - 3, this power will heat your gun
 turn(degrees)       #turns the robot (and the gun and the radar), 
                     #max 10 degrees per tick
 turn_gun(degrees)   #turns the gun (and the radar), max 30 degrees per tick
 turn_radar(degrees) #turns the radar, max 60 degrees per tick
 dead                #true if you are dead
 say(msg)            #shows msg above the robot on screen
 broadcast(msg)      #broadcasts msg to all bots (they recieve 'broadcasts'
                     #events with the msg and rough direction)

最近关注 Ruby 比较多,平时工作中也会用 Ruby 来写一些脚本 (渐渐代替了 Python)。有两个原因,Ruby 的语法更符合口味 (不喜欢用 Python 的 indent 约束),
Ruby 也更 Lisp 化,Ruby 的开源气氛非常好。

公号同步更新,欢迎关注👻