From 4c1b54c993c70b921ce16456e7fe1c792fbed41a Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 23 Dec 2011 18:33:16 +0100 Subject: [PATCH] Example client: Implement pheromone processing --- client/example.pl | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/client/example.pl b/client/example.pl index 2eebd89..d6df997 100755 --- a/client/example.pl +++ b/client/example.pl @@ -26,6 +26,9 @@ $/ = "\r\n"; # visual => [ # CHAR[2], ... # ] (type-agent character pairs for perceived tiles) +# pheromones => [ +# { PHID => VALUE, ... }, ... +# ] (pheromone spectrum for perceived tiles) # } @@ -64,6 +67,15 @@ sub tick($$) { } elsif ($type eq 'visual') { $value =~ /^([^ ][^ ] )+([^ ][^ ])$/ or die "[ee] type visual wrong value ($value)\n"; $state->{visual} = [ split(" ", $value) ]; + + } elsif ($type eq 'pheromones') { + $value =~ /^(\((\d+:[0-9.]+,?)*\) ?)+$/ or die "[ee] type pheromones wrong value ($value)\n"; + # Best to read the following from the outside inwards: + $state->{pheromones} = [ + map { { + map { split(":", $_) } split(",", $_) + } } map { y/()//d; $_ } split(" ", $value) + ]; } } } @@ -191,7 +203,18 @@ while (1) { tick($socket, $state); # Debug print print $state->{energy} . "\n"; - print "[", join('], [', @{$state->{visual}}), "]\n"; + print "visual [", join('], [', @{$state->{visual}}), "]\n"; + # The following could be written more succintly, but it is here + # to show possible iteration over pheromones: + print "pherom "; + for my $i (0..$#{$state->{pheromones}}) { + print "["; + for my $pi (keys %{$state->{pheromones}->[$i]}) { + print "$pi=>".$state->{pheromones}->[$i]->{$pi}." "; + } + print "] "; + } + print "\n"; take_action($socket, $state); }