{{{ #!html
Games::PerlWar - A Perl variant of the classic Corewar game
This is a sparring program, similar to the programmed reality of the Matrix.
It has the same basic rules, rules like gravity. What you must learn is that
these rules are no different than the rules of a computer system. Some of
them can be bent, others can be broken. - Morpheus
PerlWar is inspired by the classic Corewar game. In this game, players pit snippets of Perl code (called 'agents') against each other in order to gain control of the vicious virtual battlefield known as... the Array.
A turn of the game is made up of the following steps:
Each player has the opportunity to submit a new agent to be introduced into the Array. Insertions are treated in order of submission time. The cell into which an entrant agent lands is picked randomly amongst the empty positions of the Array.
If there are no empty positions left, an entrant agent replaces one of the already-present agents owned by the player, chosen randomly. If the player doesn't have any agents already present in the Array, the new agent is discarded.
A player is eliminated from the game if he doesn't have any agents present in the Array at the end of the introduction of new agents.
Each cell of the Array is visited sequentially. If a cell contains an agent, it is executed (agents exceeding the permitted length segfault on initialization).
If the agent segfaults, it is erased and the cell ownership is cleared.
If the agent executes without segfault'ing, the changes made to $_ (that is, on the snippet itself) are brought to the Array. E.g., the snippet
$turn = 13; s/\d+/$&+1/e;
once executed, will become
$turn = 14; s/\d+/$&+1/e;
In addition, the agent can return an instruction (see below). If it returns a value that does not match any valid instruction, it is treated as a no-op.
An agent can return one instruction of the set below. All positions are are relative to the position of the executing agent.
Example:
# crawler - copy itself to the next position
return ":1"
Example:
# berserker
return '!'.1+int(rand(@_))
Example:
# drive neighbor to suicide
$_[1] =~ s//return "!"/;
return "~1";
Example:
# crawling borg
$pos = 1;
s/\d+/$&+1/e;
return "^$pos";
The game ends once the final round is played, or until all but one player have been eliminated. The winner of the game is the player with the most agents still alive in the Array.