If anyone out there likes to program, or wants a fun way to learn, I just downloaded two cool games off the Internet:::

ROBOCODE ::: This game was developed by IBM with the ulterior motive of teaching the people that play it how to program in Java. Java is simpler than C, and has the advantage of being entirely ObjectOriented (if you don’t know what that means, don’t worry about it….just suffice to say its cool). It also has the disadvantage that its programs run slow relative to C, but it’s still cool.

Any ways, in RoboCode, your task is to program the AI of a robot tank. It can be as simple or complex as you want. My first robot has hardly bright, it would drive forward 100 inches, swivel its cannon 360 degrees, stopping to shoot if it saw another tank, drive backwards 100 inches, swivel its cannon 360, drive forward 100 inches….

My latest robot spends most of its time driving in a circle around its enemies and strafing them. It has collision detection, evasive maneuvers, and can make decisions about how much power to use in a given shot (firing temporarily takes away health) based on the chances of hitting the enemy and the current health situation. My next improvement to the TZ-Bomber will be to teach it to lead its target (right now it just fires at where they are at, which hardly work for a moving enemy).

It can be downloaded at http://robocode.alphaworks.ibm.com/home/home.html

Some sample code?

Here ya go:


(% highlight java %) public class MyFirstRobot extends Robot {

// COMMENT : MyFirstRobot’s run method - Seesaw

public void run() {

while (true) { ahead(100); // COMMENT : Move ahead 100 turnGunRight(360); // COMMENT : Spin gun and radar around back(100); // COMMENT : Move back 100 turnGunRight(360); // COMMENT : Spin gun and radar around } }

// COMMENT : Fire when we see a robot

public void onScannedRobot(ScannedRobotEvent e) { fire(1); } (% endhighlight %)


Won’t win many fights, but it can move and shoot!

COREWAR ::: This one is a bit harder, because it is more abstract and difficult to win, but extremely satisfying when you can design a warrior that works.

COREWAR creates a simulation of a computer system’s memory.

Your purpose is to write a virus, in an assembly-like language called RedCode, that will crash the other programs in memory (competing virii).

Some sample code?

Here ya go? This is the simplest RedCode virus, the Imp:

MOV 0, 1 END

That’s it. But its a little more sophisticated than it looks….I’ll show you:

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | END 2 | NULL

That is the memory when the program starts. After the first tic, our program runs the command at line 0, MOV 0,1, which copies the data/command at location 0 to location 1…so now it looks like this::

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | NULL

Notice that the END goes away….it was there to make it compatible with the compiler…but once in memory, who needs it….I mean, seriously, when’s the last time you had a virus that just decided to stop running on its own accord?

That’s what I thought. :)

Now, addresses are relative….each command sees itself residing at 0. To compare it to English, 0 is always “HERE” and everything else is always “THERE”.

So the command “MOV 0,1” could also be read as “Move me from here (0) to over there (1).”

So after the next tic, the program goes to run the next command in memory…originally that was your END, but now:

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 (remember, all commands see themselves as being at zero…so this copies itself to 2 (which it sees as 1))) 2 | MOV 0,1 3 | NULL

And the next tic :

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | MOV 0,1 3 | MOV 0,1 4 | NULL

And so on, until it fills the memory, or is destroyed by another program.

So how do you destroy it?

The problem with an Imp is it is actually pretty easy to trash using the Bomber, which looks something like this:

MOV 2, -1 JMP -1 DAT 0 END

JMP is a loop…JuMP -1 will jump execution one step backwards…so it goes back to MOV. Why?

Because of DAT. DAT is DATA, all it does it stores a number, or a character, or something in memory. What is a program, though, tries to “run” the “command” data? You can’t “run” data anymore than you can “run” a text file on your desktop…you have to open it with another program.

Any program that tries to run data, crashes. :)

Lets look at the memory watch what happens, and remember that even though I am keeping the addresses absolute for clarity, each command (mov, jmp, dat) sees itself as existing at address 0, which is always “here”::

(( Follow the * to see which command is executing.))

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | END 2 | NULL 3 | NULL 4 | MOV 2,-1 5 | JMP -1 6 | DAT 0 7 | END

TIC (player one):

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 * 1 | MOV 0,1 2 | NULL 3 | NULL 4 | MOV 2,-1 5 | JMP -1 6 | DAT 0 7 | END

TIC (player two):

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | NULL 3 | DAT 0 4 | MOV 2,-1 * 5 | JMP -1 6 | DAT 0 7 | END

TIC (player one):

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 * 2 | MOV 0,1 3 | DAT 0 4 | MOV 2,-1 5 | JMP -1 6 | DAT 0 7 | END

TIC (player two):

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | MOV 0,1 3 | DAT 0 4 | MOV 2,-1 5 | JMP -1 * (performs the jump back to 4) 6 | DAT 0 7 | END

TIC (player one) :

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | MOV 0,1 * 3 | MOV 0,1 4 | MOV 2,-1 5 | JMP -1 6 | DAT 0 7 | END

TIC (player two) ADDRESS | DATA ————————– -1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | MOV 0,1 3 | DAT 0 4 | MOV 2,-1 * 5 | JMP -1 6 | DAT 0 7 | END

TIC (player one)

ADDRESS | DATA

-1 | NULL 0 | MOV 0,1 1 | MOV 0,1 2 | MOV 0,1 3 | DAT 0 * 4 | MOV 2,-1 5 | JMP -1 6 | DAT 0 7 | END

At this point, you can see Player one tries to run the command DAT, which is illegal…DAT isn’t a command, its just data. This crashes player one, the IMP, and BOMBER wins the day.


That turned out to be a hell of a lot longer than I thought it would be…..

Of course, the game itself shows you a cool, color coded graphical representation of this battle going on, and does it at about a 500 commands a second, so battle end pretty quick.



blog comments powered by Disqus

Published

18 August 2004

Category

Personal

Tags