Games-Perlwar

 view release on metacpan or  search on metacpan

lib/Games/Perlwar.pm  view on Meta::CPAN

        and $target->get_owner ne $agent->get_owner )
    {
        $self->log( "\tagent in cell $dest_index already owned by ".
                    $target->get_owner );
        return;
    }

    $self->log( "\tagent of cell $source_index copied into cell $dest_index" );
    $target->copy( $agent );
    $target->set_operational( 0 );
}

sub _noop_operation {
    $_[0]->log( "\tno-op" );
}

sub readCell {
	my( $self, $cellId ) = @_;
	return undef unless $self->{theArray}[$cellId];
	return ( $self->{theArray}[$cellId]{owner}, $self->{theArray}[$cellId]{code}  );
}

sub writeCell {
	my( $self, $pos, $owner, $code ) = @_;
	$self->{theArray}[$pos] = { owner => $owner, code => $code };
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub array {
    return $_[0]->{theArray};
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


sub agent_census {
    my( $self ) = @_;

    my %player = %{$self->{conf}{player}};

    my %census = $self->{theArray}->census;

    for my $p ( keys %player ) {
        $player{$p}{agents} = $census{$p} || 0;
    }

    return %census;
}


=begin notes

my $pw = new Games::Perlwar;

$pw->{interactive} = 1;
$pw->{theArray} = [ { owner => 'Yanick', name => 'Neo', code => 'print "Hello world!"' },
                    { owner => '1337 h4ck3r', name => 'crash me', code => 'exit' },
                    { owner => '1337 h4ck3r', name => 'readdir me', code => 'opendir DIR, "."; return readdir DIR;' },
                    { owner => '1337 h4ck3r', name => 'infinite loop', code => '1 while 1' },
                    { owner => '1337 h4ck3r', name => 'backticks', code => '`ls`' },
                    { owner => '1337 h4ck3r', name => 'kill next', code => '"!1"' },
                    { owner => '1337 h4ck3r', name => 'must die', code => '"I am still alive?"' },
                    { owner => 'Yanick', name => 'good boy', code => '1' },
                    { owner => 'Yanick', name => 'owner', code => '"~-1"' },
                    { owner => 'Yanick', name => 'too big', code => 'a' x 200 },
                    ];
$pw->{config}{arraySize} = @{ $pw->{theArray} };
$pw->{config}{maxSnippetSize} = 100;

$pw->runSlot( $_ ) for 0..9;

=end notes

=cut

1;

__END__

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#  Module Documentation
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

=head1 NAME

Games::Perlwar - A Perl variant of the classic Corewar game

=head1 DESCRIPTION

For the rules of Perlwar, please refers to the Games::Perlwar::Rules manpage.

=head1 HOW TO START AND MANAGE A PW GAME (THE SHORT AND SKINNY)

Use the script I<pwcreate> to create a new game. 

    $ pwcreate [ <game_directory> ]

pwcreate will create I<game_directory> and populate it with the everything
the new game will need. If I<game_directory> is not provided, I<pwcreate> will
create a sub-directory called 'game'. 

Once the game is created, 
the script I<pwupload> can be used to submit the agents to
be introduced into the Array:

    $ pwupload <game_directory> <player> 

I<pwupload> takes two arguments: the game directory and the name of
the agent's owner. The script then reads the script from STDIN. 
E.g.:

    $ pwupload /home/perlwar/myWar yanick < borg.pl

Finally, I<pwround> executes an iteration of the game:

    $ pwround <game_directory>

I<pwround> isn't interactive and can easily be called from a cron job.

=head1 BUGS AND LIMITATIONS

I<pwupload> currently only works for local games. It will be



( run in 1.639 second using v1.01-cache-2.11-cpan-99c4e6809bf )