This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
/
RandomBot.pm
98 lines (69 loc) · 2.07 KB
/
RandomBot.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package MyBot;
use strict;
use warnings;
use base 'AIAnts::BotHash';
=head1 NAME
Example RandomBot bot for L<AIAnts> game.
=head1 SYNOPSIS
Google AI Challenge 2011 "AI Ants" game Perl bot.
=head1 METHODS
=head2 setup
Setup.
=cut
sub setup {
my $self = shift;
$self->SUPER::setup( @_ );
}
=head2 turn_body
Main part of turn processing. Should call 'add_order' method during processing.
=cut
sub turn_body {
my ( $self, $turn_num, $turn_data, $turn_diff ) = @_;
my $dirs = [ 'N', 'E', 'S', 'W' ];
$self->log( "turn $turn_num\n" ) if $self->{log};
#$self->log( $self->{m}->dump(1) . "\n\n" ) if $self->{log};
#$self->dump( $turn_data ) if $self->{log};
my $used = $self->get_initial_used( $turn_data );
foreach my $data ( values %{$turn_data->{m_ant}} ) {
my ( $x, $y ) = @$data;
my $ant = $self->{pos2ant}{"$x,$y"};
my $dir;
my ( $Dx, $Dy, $Nx, $Ny );
my $dir_num = int rand 4;
my $attemt = 1;
RANDOM: while ( 1 ) {
$dir = $dirs->[ ($dir_num+$attemt) % 4 ];
if ( $dir eq 'N' ) {
$Dx = -1;
$Dy = 0;
} elsif ( $dir eq 'E' ) {
$Dx = 0;
$Dy = 1;
} elsif ( $dir eq 'S' ) {
$Dx = 1;
$Dy = 0;
} elsif ( $dir eq 'W' ) {
$Dx = 0;
$Dy = -1;
}
( $Nx, $Ny ) = $self->{m}->pos_plus( $x, $y, $Dx, $Dy );
if ( $self->{m}->valid_not_used_pos( $Nx, $Ny, $used ) ) {
$self->add_order( $ant, $x, $y, $dir, $Nx, $Ny );
delete $used->{"$x,$y"};
$used->{"$Nx,$Ny"} = 2;
last RANDOM;
}
last RANDOM if $attemt == 4;
$attemt++;
}
}
$self->log("\n") if $self->{log};
return 1;
}
=head1 AUTHOR
Michal Jurosz, mj@mj41.cz
=head1 LICENSE
This is free software; you can redistribute it and/or modify it under the same
terms as the Perl 5 programming language system itself.
=cut
1;