Tank Wars: A Perl Battle


Required reading
Rule set below on Tank Wars.

Necessary files to complete this exercise
To download the files to your system, just press the Shift key while you left click on the blue link. Follow the instructions.
gandhi.pl A non-violent (and therefore losing) way of resolving the conflict
insane.pl An insane gunman that shot in a random direction
drunk.pl A drunk cowboy moving randomly, shooting at obstacles, falling asleep when exhausted (inspired by New Years Eve).
ClientServer.pm A necessary package to include in your tank, see above implementations
You can play around with these files as much as you like. If you change or destroy them, just download them again.

Remember to write #!/usr/bin/perl on the first line of your programs.

To print in the K-bar use: lpr -Pkps1 <filename> or lpr -Pkps2 <filename>

Rule set of Tank Wars
Every participant makes a program which communicates with the battle field server, as demonstrated in gandhi.pl and insane.pl. Every tank starts in a random position in a grid of semi-random size. The object of the game is to shoot the other tanks and be the only participant left (the winner). Every tank/program gives a command to the battle field server in a round-robin fashion, and gets immediately an answer back. A round is when all tanks have had the chance to give a command, and the server starts over again querying the tanks for commands. Note that all commands in a round are NOT executed simultaneously, but directly as a tank is queried, so the answer comes right back after the query. The game will run for at most 100 rounds, any tanks left will make a draw.

All tanks start out with 100 energy point (called Epoints hereafter). Every active command/action cost a number of Epoints, and if you don't have enough Epoints your command/action will not performed. However, you gain 5 Epoints every round, no matter what command you execute. You can never gain more Epoints than the maximum which is 100.

Your tank must have a name, which you use to identify yourself to the battle field server. It consists of letters, numbers and underscores, no spaces or funny chars are allowed. This is your <ID>, see below.

There are 8 directions in a grid, and they can be written with numbers or letters. T is for tank. If you use letters as direction to a command, then the answer will use letters, too (if relevant for the command). Same with numbers. When <Direction> is used below, it refers to one of these numbers/letters.
N  NE  E  SE  S  SW  W  NW
1  2   3  4   5  6   7  8

There are six (6) commands to control your tank. They will be described in detail below, but they are SHOOT, MOVE, SCAN, POSITION, PARTICIPANTS and WAIT. Every command must be given in uppercase letters, every answer is in uppercase letters.

Syntax: SHOOT <ID> <Direction>
Cost: 25 Epoints
Shoot a laser in the given direction until it hits a wall or an enemy tank. Distance is not a problem. If you hit a tank, you get full energy.
Possible answers are:
HIT TANK - You hit a tank and got full energy
HIT WALL - You hit a wall

Syntax: MOVE <ID> <Direction>
Cost: 10 Epoints
Moves the tank one step in the given direction
Possible answers are:
MOVE SUCCESS - Your move succeeded
MOVE FAILURE - You couldn't move in that direction because of some obstacle (tank or wall)

Syntax: SCAN <ID> <Direction> ...
Cost: 5 Epoints per direction
Scans in one or more directions. The nearest obstacle in the given direction(s) is reported.
Possible answers are:
SCAN <Direction> <Obstacle> <Distance> ...
An example command: SCAN MyName 1 3 S W
Corresponding answer: SCAN 1 WALL 4 3 WALL 6 S TANK 2 W WALL 8

Cost: 20 Epoints
Returns the absolute position (xy-coordinates) of the tank in the grid. Possible answers are:
POSITION <x> <y>

Cost: 20 Epoints
Returns the names of all still "living" tanks in the game. Your name will be among them.
Possible answers are:
PARTICIPANTS <name1> <name2> ...

Syntax: WAIT
Cost: 0 Epoints
Simply waits this round, doing nothing. This is the only way to gain Epoints. However, even if you try to perform an active command when you are exhausted, you will gain Epoints.
Possible answers are:
ENERGY <number> - You now have <number> Epoints

Other answers
There are three general answers that can come from every command. They are:
EXHAUSTED <number> - You didn't have enough energy to do the command, now you have <number> Epoints
GIGO - When you make an error in the syntax of the command and the battlefield server dosen't understand you (Garbage In, Garbage Out)
STOP <Reason> - When you get this very important answer, you must stop the program. No further commands can be sent to the battle field server. It will not listen to you anymore. The reason is you have been KILLED (most likely), or you are a WINNER, or you fought to a DRAW.

This page was last updated         by Peter Wad Sackett, pws@cbs.dtu.dk