专门给程序员玩的游戏Robocode现在可以用.net玩啦

来源:互联网 发布:便宜好用的口红知乎 编辑:程序博客网 时间:2024/04/30 05:18

     Robocode是一个程序员竞技游戏,继承官方自带的坦克基类,然后给这个坦克加入更强大的AI,与其它人的坦克进行单挑或者团战、混战皆可.最初目地是让初学者学习java更有兴趣,后来发展为不管你程序什么水平,都可以在里面一展才华,最近Robocode又出现了支持.net系列语言的插件,会C#,VB,托管C++等.net语言的人也可以杀入这个战场与java程序员们写的坦克大战一翻,还是满有意思的。

    

    首先为了运行robocode还是得要安装java的jre运行库环境,然后从官网http://robocode.sourceforge.net/下载robocode,在目录下会有robocode.dotnet-xxx-xxxsetup.jar的文件,这个就是.net插件,双击安装它,然后请保证你的VS版本是2008。

 

     接下来创建一个工程,以C#为例,创建class library项目,就是生成.dll类型的工程。

 

    然后在References中添加robocode的dll库,目录在libs/robocode.dll.

   

    其中Robot类就是我们要进行改写的坦克基类,继承robot类,然后处理各种事件以及AI行为。

 

    下面是最简单的一个坦克AI,别小看它,它可以打胜软件自带的所有例子坦克。。。。

 

    // Access to standard .NET System
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Access to the public Robocode API
using Robocode;

// The namespace with your initials, in this case FNL is the initials
namespace FNL
{
     // The name of your robot is MyRobot, and the robot type is Robot
     class MyRobot : Robot
     {
         // The main method of your robot containing robot logics
         public override void Run()
         {
             // -- Initialization of the robot --
            
             // Here we turn the robot to point upwards, and move the gun 90 degrees
             TurnLeft(Heading - 90);
             TurnGunRight(90);
      
             // Infinite loop making sure this robot runs till the end of the battle round
             while (true)
             {
                 // -- Commands that are repeated forever --
        
                 // Move our robot 5000 pixels ahead
                 Ahead(5000);
                
                 // Turn the robot 90 degrees
                 TurnRight(90);
                
                 // Our robot will move along the borders of the battle field
                 // by repeating the above two statements.
             }
         }
        
         // Robot event handler, when the robot sees another robot
         public override void OnScannedRobot(ScannedRobotEvent e)
         {
             // We fire the gun with bullet power = 1
             Fire(1);
         }
     }
}

 

      编好这段代码好,编译整个工程会编出一个xx.dll的文件,把这个文件地址拷贝一下,打开robocode,选择Options -> Preferences -> Development Options.将路径添加到下面,之后就可以用你的坦克和别人对战啦。双击软件目录下的roborumble.bat,还可以更新最新的世界级排名的坦克,和这些坦克打,我编的坦克连北都找不着,太牛B了,由其有个叫shadow3.48的坦克,貌似现在世界第一。

    

      有想一起玩的朋友,加我QQ:184107415

原创粉丝点击