select(STDOUT); $| = 1; # DO NOT REMOVE # The code below will read all the game information for you. # On each game turn, information will be available on the standard input, you will be sent: # -> the total number of visible enemies # -> for each enemy, its name and distance from you # The system will wait for you to write an enemy name on the standard output. # Once you have designated a target: # -> the cannon will shoot # -> the enemies will move # -> new info will be available for you to read on the standard input. # game loop while (1) { chomp($count = ); # The number of current enemy ships within range print STDERR "COUNT:".$count ."\n"; @enemies = (); for(my $i=0; $i<$count; $i++) { # enemy: The name of this enemy # dist: The distance to your cannon of this enemy chomp($tokens=); ($enemy, $dist) = split(/ /,$tokens); print STDERR "name:".$enemy."\tdist:".$dist ."\n"; my $e->{name}=$enemy; $e->{dist}=$dist; push @enemies,$e; } $min_e = $enemy; $min_d = $dist; print STDERR "enemies:".$#enemies . "\n"; for my $href (@enemies) { print STDERR "x:". $href->{name} . "\n"; $name = $href->{name}; $dist = $href->{dist}; if($min_d > $dist){ $min_d = $dist; $min_e = $name; } } print $min_e."\n"; # Write an action using print # To debug: print STDERR "Debug messages...\n"; #print "HotDroid\n"; # The name of the most threatening enemy (HotDroid is just one example) }