VbRoboCode: Programming guidelines

The first thing to do is create a new Visual Studio project and make a reference to the Math and VbRoboCode assemblies. The Math assembly is a feature that sets VbRoboCode apart from the original Robocode; all the grunt work of figuring out how to do things like calculate the best turn to a target, adding vectors, etc is all in there for you to use.

Next, you’ll want to create a class. It is mandatory for the class name to be the same as the assembly filename (less the .dll suffix). This class must inherit from ScottThomason.VbRoboCode.RobotPublic. You’ll need to make it serializable, and you’ll probably want to import the ScottThomason.VbRoboCode and ScottThomason.VbRoboCode.Math namespaces, too.

Now the fun part begins. Let’s discuss the programming for robot Sample1. This robot just sits still, continually scanning the arena with its radar. When it sees another robot, it turns the gun to point at it, and when the gun is done turning, it fires. Let’s take it step-by-step.

First, let’s build our constructor. We’ll use it to set the name of our robot, which gets printed just above it.

  Public Sub New()
    Me.Name = "Sample1"
  End Sub

To move the radar in a continuous circle, 45 degrees per step, override PlanMove like this:

  Public Overrides Sub PlanMove(ByVal pi As PlanInfo)
    RadarTurnLeft(45)
  End Sub

When the radar passes over another robot, your robot’s OnRobotScanned subroutine will be called. It is passed a RobotScannedEventArgs object. This class contains a property called TheirState, which is a clone of the scanned robot’s RobotPublic object at the time of the scan. This allows you to find out all kinds of things about your enemy as it was when the scan occurred. We’ll use this info to turn our gun toward the target, like this:

  Public Overrides Sub OnRobotScanned(ByVal e As RobotScannedEventArgs)
    If Not IsGunTurning Then
      GunTurnRight(ShortestTurn(GunAngle, Angle(At, e.TheirState.At)))
    End If
  End Sub

First, we use the IsGunTurning property to find out if the gun is already turning towards another target. The gun only rotates a certain amount per step; it may take more than one step to finish the turn you planned for it. By doing nothing if the gun is already in a turn, we avoid “wagging” the gun from target to target.

If the gun isn’t in the middle of a turn, we want to turn it toward the enemy. The first thing we need to know is what angle the enemy is at relative to us. We can use the Math.Angle function for this, specify our position and the enemy’s position. Next, we need to know which way and how far to turn the gun. The Math.ShortestTurn function figures that out for us, taking the current angle and the target angle as parameters. ShortestTurn will return a positive angle for clockwise turns, and a negative angle for counter-clockwise turns, so it can be fed right into GunTurnRight.

Finally, we need to know when our gun has finished turning toward the target so we can fire it. This is accomplished by overriding the OnGunTurnComplete subroutine. We check to see if the gun is cool enough to fire, and fire a bullet of power 1.01. Note that the IsGunCool check isn’t really necessary, as the simulator won’t fire your gun anyway if it is too hot.

  Public Overrides Sub OnGunTurnComplete(ByVal e As GunTurnCompleteEventArgs)
    If IsGunCool Then
      Fire(1.01)
    End If
  End Sub

That’s all for sample robot #1. Check out sample robots 2 thru 7 for other techniques, and when you’re really up to speed, check out AntiGravBot (the first in the series to use an anti-gravity movement strategy), Attila, Fury, and Wrath.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati

Leave a Reply