![fd8b2391da2df9614da1c7924e0795a5.png](https://i-blog.csdnimg.cn/blog_migrate/1045fd79dd060e229a7bb29b9b171805.jpeg)
混用Java/Kotlin/Scala玩Robocode坦克大战
创建robots项目
直接使用robocode自带的SourceEditor来创建:Robot -> Source Editor -> File -> New -> Robot,然后按照提示输入包结构和Robot名称,最后一定记得需要Ctrl+S保存一下项目才算真正将创建的项目保存下来,紧接着就可以在robocode安装目录下的robots中找到创建项目
自带的Source Editor太挫,这里选择使用IDEA打开,稍微调整一下结构后得到如下:
![6737ba80c37052906579901c5969b20c.png](https://i-blog.csdnimg.cn/blog_migrate/07f5812e932caa39ff95f14045fd95f5.png)
- src目录下放的是自定义robot源码,上面的例子中共有三种版本:java、kotlin和scala
- cn/chph/robots/xxx放的是编译后的robot字节码文件
要注意的是:robot字节码文件所在的目录结构要和robot的package结构相同,例如上面三个Robot源码都是在cn.chph.robots包下,所以生成的字节码也必须放到对应的目录结构cn/chph/robots下,否则在启动Battle时会抛出类似下面的异常:
java Got an error with out.production.robots.cn.chph.robots.KotlinRobot: java.lang.ClassNotFoundException: cn/chph/robots/KotlinRobot (wrong name: out/production/robots/cn/chph/robots/KotlinRobot) Got an error with out.production.robots.cn.chph.robots.JavaRobot: java.lang.ClassNotFoundException: cn/chph/robots/JavaRobot (wrong name: out/production/robots/cn/chph/robots/JavaRobot) Got an error with out.production.robots.cn.chph.robots.ScalaRobot: java.lang.ClassNotFoundException: cn/chph/robots/ScalaRobot (wrong name: out/production/robots/cn/chph/robots/ScalaRobot)
添加依赖
因为官方的robocode只支持java,因此为了使得kotlin/scala编写的robot能够顺利进入战斗,我们需要添加kotlin/scala对应的语言标jar准包,直接修改启动脚本robocode.sh:
![b9d84fdf185a03d5aa5077065dcc395b.png](https://i-blog.csdnimg.cn/blog_migrate/b72674cf45e145750ff340a3ca9e49e5.jpeg)
启动
紧接着按照正常的步骤进入游戏,robocode就能够正确识别并允许我们编译后的不同语言版本的robot
![2da1d2f550ee6f3e9deeb25d5f5c055a.png](https://i-blog.csdnimg.cn/blog_migrate/153b4594909deafa66ba22ae3d9805cf.jpeg)
![449365762706749c366c54d65120f9a4.png](https://i-blog.csdnimg.cn/blog_migrate/11d896e87bd3d12514502f7c802c37fc.jpeg)