Tuesday, September 20, 2011

Robocode!

For this week's program we've been dabbling with a little something called robocode, and as the name implies, yes, there are robots involved! Unfortunately however, it's all within a computer simulation, so my hopes of pitting customized roomba bots against each other in a fight to the death will have to wait for now.
Simply put, robocode is a game where you can program custom robots and then put them in a virtual arena where they do battle with other robots until the last one stands. Robots have three basic functions; movement, firing, and detection of other robots, and although that may not seem like a lot, you have the whole java arsenal at your disposal to customize how a robot moves, how it reacts to other enemies, and a multitude of other things. From dancing in sine curves around your enemy to doing a little victory dance when you've destroyed an opponent, the sky's the limit with how detailed and intricate you can get. Robots can be programmed in either java or .NET, but for the purposes of this project java will be exclusively used.  For those of you who wish for a more detailed explanation of the wonderful world of robocode, here you go.

I must say that it was a bit difficult at first to get my homemade robots actually working, but after looking over many tutorials and reading through the robowiki, I was able to make a bot that could move around, and eventually destroy other bots.  The great thing about robocode is that there is a pretty sizable community, with a multitude of resources including tutorials, explanations, forums and whatnot, something for people of all skill-levels.

Programming knowledge aside however, honestly I'd say the most difficult part of robocode is using trigonometry. Of course, it is by no means mandated that you use trig in order to move around your robot, but without it your code is going to be a lot longer and you're going to have find a lot of workarounds in order to get your robot to do what you want it to do. For example, when locking on to a coordinate, you have to take into account the current angle the robot is facing and the position of the coordinate relative to that robot, calculate the slope and convert that to degrees, then get the robot to face that position and then calculate the distance to determine how far to send the robot. Doing that without trigonometry, you're gonna have a hard time finding those calculations.  I personally spent a good 3 to 4 hours trying to come up with a sufficient formula to calculate angles, but not being the trig-whiz I once was in high school, I was unable to figure it out myself.  Fortunately however, with help from my good friend the internet, I was able to find a method from a bot that did the exact same thing, and with some slight modifications to the code I was able to implement the function within my robot (properly cited in my code, of course). Right there is another great thing about robocode, you are able to take things you like from other robots and add them to your own, and for the most part it's easily compatible, because all robots are an extension of just one of two classes, Robot or AdvancedRobot.

We were tasked with making a grand total of 13 robots, each with their own specific function. From doing nothing, to travelling in a spiral, to following other robots, to shooting all robots, in all they all pretty much cover the basics of robot movement and action.  I was able to complete 12 of the 13 robots, the only one left undone being a bot that tracks another robot with it's gun but does not fire.

Having gotten my feet wet with robocode, I feel that I'm a bit more familiar with the way these robots function. It's easy to pick up, but underneath that simplicity there's just so much depth and so much you're able to do with these robots, that in order to master it, it's gonna take a while. It was really helpful that the project was divided into bots with incremental steps in complexity, so that when you completed one robot you gained insight as to how to program other robots. I'm better off than I was when I started this project, but of course, there's still a long way to go in order to "build the best, destroy the rest!"

Seeing as how the next project involving robocode will be to create a competitive robot and facing off against classmates, I have a feeling that it's definitely going to be harder than fighting with practice bots that just stay still and don't fire at you. It's really going to be important to be mindful of a bunch of factors in order to build a robot that can survive in the arena, things such as ammo depletion, turning direction, trajectory and so on. I know I already have some ideas for my robot (none of which I shall disclose at this time), and am looking forward to programming it in to my robot and pitting it up against my classmates in the upcoming weeks.

Well that just about sums up my experiences with robocode thus far, and I'm sure there's plenty more to come down the line. For those of you who are interested in robots, or just improving your programming skills, I would definitely recommend dabbling in robocode, just to see how far and how skilled you can become.


No comments:

Post a Comment